Signals
Overview
Signals are a higher-level software form of exceptional control flow that allows processes and the kernel to interrupt other processes. They exist to expose low-level hardware exceptions that would not normally be visible to user processes.
The transfer of a signal to a destination process occurs in two distinct steps:
- Sending a signal. The kernel sends a signal to a destination process by updating some state in the context of the destination process.
- Receiving a signal. A destination process receives a signal when it is forced by the kernel to react in some way to the delivery of the signal. The process can ignore the signal, terminate, or catch the signal.
A signal that has been sent but not yet received is called a pending signal. At any point in time, there can be at most one pending signal of a particular type. A process can block the receipt of certain signals. The signal can be sent but will not be received until the process unblocks the signal, i.e. it remains pending.
Common Signals
Some of the more common signals include:
| Number | Name | Default Action | Corresponding Event |
|---|---|---|---|
| 1 | SIGHUP | Terminate | Terminal line hangup |
| 2 | SIGINT | Terminate | Interrupt from keyboard |
| 3 | SIGQUIT | Core Dump | Interrupt from keyboard |
| 4 | SIGILL | Terminate | Illegal instruction |
| 6 | SIGABRT | Core Dump | Abort signal from abort function |
| 7 | SIGBUS | Terminate | Bus error |
| 8 | SIGFPE | Core Dump | Floating-point exception |
| 9 | SIGKILL | Terminate | Kill program |
| 11 | SIGSEGV | Core Dump | Invalid memory reference (seg fault) |
| 15 | SIGTERM | Terminate | Software termination signal |
| 18 | SIGCONT | Ignore | Continue process if stopped |
| 19 | SIGSTOP | Stop until SIGCONT | Stop signal not from terminal |
| 20 | SIGTSTP | Stop until SIGCONT | Stop signal from terminal |
| 21 | SIGTTIN | Stop | Background job tries reading from TTY |
| 22 | SIGTTOU | Stop | Background job tries writing to a TTY |
| 28 | SIGWINCH | Ignore | TTY indicates a terminal size change |
SIGHUP
Has signal number 1 and terminates by default.
A process receives a SIGHUP signal when the terminal it is attached to goes away before it finishes executing.
SIGINT
Has signal number 2 and terminates by default.
Indicates the process was interrupted by the user. Happens when pressing Ctrl-C from the controlling terminal.
SIGQUIT
Has signal number 3 and core dumps by default.
Indicates the process was interrupted by the user. Happens when pressing Ctrl-\ from the controlling terminal.
SIGILL
Has signal number 4 and terminates by default.
Indicates the CPU encountered an instruction it does not understand or does not have permission to execute. Often implies corruption in some way or perhaps an attempt to execute data instead of code.
SIGABRT
Has signal number 6 and core dumps by default.
Indicates an "emergency stop". Usually emitted when the process itself invokes abort().
SIGBUS
Has signal number 7 and terminates by default.
Indicates the CPU encountered an instruction to access memory it cannot physically address, i.e. an invalid address for the address bus.
SIGFPE
Has signal number 8 and core dumps by default.
Indicates the CPU performed some fatal arithmetic error. Division by zero, overflows, underflows, etc. all trigger this signal.
SIGKILL
Has signal number 9 and terminates by default.
Indicates a process should be killed. This signal cannot be ignored nor caught.
SIGSEGV
Has signal number 11 and core dumps by default.
Indicates an instruction performed a memory access violation, trying to read from or write to a memory area the current process does not have access to.
SIGTERM
Has signal number 15 and terminates by default.
Indicates the process should terminate.
SIGCHLD
Has signal number 17 and ignores by default.
Indicates a child process has stopped or terminated.
SIGCONT
Has signal number 18 and continues by default.
Indicates the process should resume if it was suspended.
SIGSTOP
Has signal number 19 and stops by default.
Indicates the process should be stopped, i.e. suspended.
SIGTSTP
Has signal number 20 and stops by default.
Indicates the process was stopped by the user. Happens when pressing Ctrl-Z from the controlling terminal.
SIGTTIN
Has signal number 21 and stops by default.
Sent by a TTY to a background job trying to read from it.
SIGTTOU
Has signal number 22 and stops by default.
Sent by a TTY to a background job trying to write to it.
SIGWINCH
Has signal number 28 and ignores by default.