A common concept of Linux systems programming is the idea of sending and receiving signals. You can think of a signal as a software interrupt that’s used to announce some sort of event, asynchronously, to a process.
We do not define our own signals. Instead, they are defined by the kernel. Each signal has a number, but they also have a name that begins with SIG. We typically refer to a single by its name rather than its number.
As an example, signal 11 is called SIGSEGV and is the signal that is sent to a process when it experiences a segmentation fault.
The Standard Linux Signals
The first 31 signals are standardized on Linux. Some of these are from the POSIX standard, but not all of them.
# | Signal Name | Default Action | Description | From POSIX? |
---|---|---|---|---|
1 | SIGHUP | Terminate | Historically, indicates that a terminal connection has been "hung up". May be used differently on modern systems | Yes |
2 | SIGINT | Terminate | Indicates an interrupt from the keyboard, such as issuing Ctrl-C . |
Yes |
3 | SIGQUIT | Dump | Indicates that the receiving process should be terminated and a core dump generated (if enabled). Triggered by Ctrl-\ on most terminals. |
Yes |
4 | SIGILL | Dump | Indicates an illegal instruction. | Yes |
5 | SIGTRAP | Dump | Indicates that a debugging breakpoint was hit. | No |
6 | SIGABRT | Dump | Indicates an abnormal termination. | Yes |
6 | SIGIOT | Dump | Functionally equivalent to SIGABRT. | No |
7 | SIGBUS | Dump | Indicates a bus error. | No |
8 | SIGFPE | Dump | Indicates a floating-point exception. | Yes |
9 | SIGKILL | Terminate | Indicates that the process has been force terminated. | Yes |
10 | SIGUSR1 | Terminate | No pre-defined meaning. Available for the process to use. | Yes |
11 | SIGSEGV | Dump | Indicates an invalid memory reference (segfault). | Yes |
12 | SIGUSR2 | Terminate | Similar to SIGUSR1, has no pre-defined meaning. Available for the process to use. | Yes |
13 | SIGPIPE | Terminate | Indicates that the process attempted to write to a pipe or socket that has been closed by the reader. | Yes |
14 | SIGALRM | Terminate | Sent by the kernel when a timer set by the alarm() syscall expires. Used to implement timeouts and periodic actions in programs. |
Yes |
15 | SIGTERM | Terminate | Indicates process termination. | Yes |
16 | SIGSTKFLT | Terminate | Indicates a stack error from a coprocessor. | No |
17 | SIGCHLD | Ignore | Indicates that a child process has stopped, been terminated, or continued after being stopped. | Yes |
18 | SIGCONT | Continue | Resume execution, if stopped. | Yes |
19 | SIGSTOP | Stop | Indicates that a process should stop execution. Triggered by Ctrl-Z on most terminals. |
Yes |
20 | SIGTSTP | Stop | Stop process issued from a TTY. | Yes |
21 | SIGTTIN | Stop | Indicates that a background process requires input. | Yes |
22 | SIGTTOU | Stop | Indicates that a background process requires output. | Yes |
23 | SIGURG | Ignore | Indicates that there is an urgent condition on a socket. | No |
24 | SIGXCPU | Dump | Indicates that a CPU time limit has been exceeded. | No |
25 | SIGXFSZ | Dump | Sent when a process attempts to write beyond the maximum file size limit. | No |
26 | SIGVTALRM | Terminate | Indicates that a process has consumed a certain amount of CPU time. Unlike SIGALRM (which uses the real/wall-clock time), SIGVTALRM tracks the time that the process has been executing on the CPU. It only counts the time when the process is running. | No |
27 | SIGPROG | Terminate | Sent when the profiling timer expires. | No |
28 | SIGWINCH | Ignore | Sent when the process's controlling terminal window changes size. | No |
29 | SIGIO | Terminate | Indicates that I/O is now possible. | No |
29 | SIGPOLL | Terminate | Functionally equivalent to SIGIO. SIGPOLL and SIGIO are actually the same signal. | No |
30 | SIGPWR | Terminate | Indicates a power supply failure. | No |
31 | SIGSYS | Dump | Indicates a bad system call. | No |
What Happens When a Signal is Sent?
When the kernel sends a signal to a process, it must be handled. There are three possible ways to do this:
- Ignore It. Many of the signals can be safely ignored (but not all of them). For example, you cannot ignore a
SIGKILL
signal. - Catch and Handle it. The process can listen for a signal and handle it itself. The program may use this to terminate things gracefully or even handle it without terminating the program.
- Use the Default Action. Every signal has a default action that the process can use.