Introduction to Signals
- Signals
- A signal is a small message that notifies a process that an event of some type occurred. Signals are often sent by the kernel, but they can be sent from other processes as well.
- A signal handler is a function that executes in response to the arrival and consumption of a signal.
- You're already familiar with some signals, even if you've not referred to them by that name before:
- You haven't really programmed in C before unless you've dereferenced a NULL pointer. When that happens, the kernel delivers a signal of type SIGSEGV, informally known as a segmentation fault (or a SEGmentation Violation, or SIGSEGV for short). Unless you install a custom signal handler to manage the signal differently, a SIGSEGV terminates the program and generates a core dump.
- Whenever a process commits an integer-divide-by-zero (and, in some cases, a floating-point divide by zero), the kernel hollers and issues a SIGFPE signal to the offending process. (By default, the program terminates with a message of the SIGFPE and a core dump is produced).
- When you type ctrl-c, the kernel issues a SIGINT signal to the foreground process (and by default, the program is terminated).
- When you type ctrl-z, the kernel issues a SIGTSTP signal to the foreground process (and by default, the process is suspended until a subsequent SIGCONT signal instructs it to resume).
- Whenever a child process exits (either normally or abnormally), the kernel potentially delivers a SIGCHLD signal to the parent process.
By default, the signal is ignored (and in fact, by default the kernel doesn't even deliver it unless the parent process registers an interest in receiving them). This particular signal type is instrumental to allowing forked child processes to run in the background while the parent process moves on to do its own work without blocking on a waitpid call. The parent process, however, is not relieved of its obligation to reap child process zombies, so the parent process will typically register code to be invoked whenever a child process terminates. Doing so prompts the kernel to begin issuing SIGCHLD signals so that the registered SIGCHLD handler can reap the zombies via waitpid.