Introduction to Linux signals
Summary
Introduction to Linux signals, software interrupts used for inter-process communication and event handling. The article covers sending, receiving, and managing signals, including examples like using Ctrl+c
for SIGINT and blocking signals with a signal mask.
Introduction #
Linux signals provide a mechanism for processes to communicate with each other (inter-process communication mechanism) and with the operating system. A signal is a software-generated interrupt.
Signals allow asynchronous notifications between processes or between the kernel and a process. A process can send a signal to another process, and the receiving process can decide how to handle it.
Signals are used for various purposes, such as terminating processes, handling errors, or performing custom actions in response to specific events.
Full list of Linux signals #
The following table lists all possible Linux signals:
Signal | # | Send Signal | Action | Description | Type | POSIX |
---|---|---|---|---|---|---|
SIGHUP | 1 | kill -SIGHUP <pid> | Terminate | Hangup detected | Signal | Yes |
SIGINT | 2 | Ctrl+C or kill -SIGINT <pid> | Terminate | Interrupt from keyboard | Interrupt | Yes |
SIGQUIT | 3 | Ctrl+\ or kill -SIGQUIT <pid> | Core Dump | Quit from keyboard | Signal | Yes |
SIGILL | 4 | kill -SIGILL <pid> | Core Dump | Illegal instruction | Signal | Yes |
SIGTRAP | 5 | kill -SIGTRAP <pid> | Core Dump | Trace/breakpoint trap | Signal | Yes |
SIGABRT | 6 | abort() or kill -SIGABRT <pid> | Core Dump | Abnormal termination | Signal | Yes |
SIGBUS | 7 | kill -SIGBUS <pid> | Core Dump | Bus error | Signal | Yes |
SIGFPE | 8 | kill -SIGFPE <pid> | Core Dump | Floating-point exception | Signal | Yes |
SIGKILL | 9 | kill -SIGKILL <pid> | Terminate | Kill signal | Signal | Yes |
SIGUSR1 | 10 | kill -SIGUSR1 <pid> | Terminate | User-defined signal 1 | Signal | Yes |
SIGSEGV | 11 | kill -SIGSEGV <pid> | Core Dump | Segmentation fault | Signal | Yes |
SIGUSR2 | 12 | kill -SIGUSR2 <pid> | Terminate | User-defined signal 2 | Signal | Yes |
SIGPIPE | 13 | kill -SIGPIPE <pid> | Terminate | Broken pipe | Signal | Yes |
SIGALRM | 14 | kill -SIGALRM <pid> | Terminate | Alarm clock | Signal | Yes |
SIGTERM | 15 | kill -SIGTERM <pid> | Terminate | Termination signal | Signal | Yes |
SIGSTKFLT | 16 | kill -SIGSTKFLT <pid> | Terminate | Stack fault | Signal | No |
SIGCHLD | 17 | kill -SIGCHLD <pid> | Ignore | Child process terminated | Signal | Yes |
SIGCONT | 18 | kill -SIGCONT <pid> | Continue | Continue process | Signal | Yes |
SIGSTOP | 19 | kill -SIGSTOP <pid> | Stop | Stop process | Signal | Yes |
SIGTSTP | 20 | Ctrl+Z or kill -SIGTSTP <pid> | Stop | Terminal stop | Signal | Yes |
SIGTTIN | 21 | kill -SIGTTIN <pid> | Stop | Background process reading from terminal | Signal | Yes |
SIGTTOU | 22 | kill -SIGTTOU <pid> | Stop | Background process writing to terminal | Signal | Yes |
SIGURG | 23 | kill -SIGURG <pid> | Ignore | Urgent socket condition | Signal | Yes |
SIGXCPU | 24 | kill -SIGXCPU <pid> | Core Dump | CPU time limit exceeded | Signal | Yes |
SIGXFSZ | 25 | kill -SIGXFSZ <pid> | Core Dump | File size limit exceeded | Signal | Yes |
SIGVTALRM | 26 | kill -SIGVTALRM <pid> | Terminate | Virtual alarm clock | Signal | Yes |
SIGPROF | 27 | kill -SIGPROF <pid> | Terminate | Profiling timer expired | Signal | Yes |
SIGWINCH | 28 | kill -SIGWINCH <pid> | Ignore | Window size change | Signal | Yes |
SIGIO | 29 | kill -SIGIO <pid> | Terminate | I/O now possible | Signal | Yes |
SIGPWR | 30 | kill -SIGPWR <pid> | Terminate | Power failure | Signal | No |
SIGSYS | 31 | kill -SIGSYS <pid> | Core Dump | Bad system call | Signal | Yes |
What happens when a process receives a signal? #
When a process receives a signal, it can:
- Ignore it (if allowed).
- Catch and handle it using a signal handler.
- Perform the default action assigned to the signal.
Examples #
- When you press
Ctrl+C
in a terminal, the shell sends aSIGINT
signal to the foreground process. If the process does not handle it, it terminates. - A segmentation fault (
SIGSEGV
) typically results in the process being terminated with a core dump.
How a process sends a signal #
A process can send a signal using:
- The
kill
command:kill -SIGTERM <pid>
. - The
kill()
system call in a C program.
Examples #
- Using
kill
:kill -SIGKILL 1234
terminates process with PID 1234. - Using
raise()
in C:raise(SIGTERM);
sendsSIGTERM
to itself.
Signal mask #
A signal mask is a mechanism that allows a process to block certain signals from being delivered until it is ready to handle them. It is useful when a process wants to ensure atomic execution of critical sections without interruptions.
- Signals can be blocked using
sigprocmask()
in C. - The
sigpending()
function can check for pending signals. - Once unblocked, pending signals are delivered to the process.
Example #
In a C program:
#include <signal.h>
#include <stdio.h>
#include <unistd.h>
int main() {
sigset_t mask;
sigemptyset(&mask);
sigaddset(&mask, SIGINT);
sigprocmask(SIG_BLOCK, &mask, NULL);
printf("SIGINT is now blocked. Press Ctrl+C, but it will not terminate.\n");
sleep(10);
sigprocmask(SIG_UNBLOCK, &mask, NULL);
printf("SIGINT is now unblocked.\n");
return 0;
}
This program blocks SIGINT
temporarily and unblocks it after 10 seconds.
Further readings #
Sources and recommended, further resources on the topic:
- Arch Linux: Signal manual page
- POSIX signal definitions
- GNU C Library: Signal Handling
- The Linux Kernel: Inter-Process Communication
- Kernel documentation on signals
License
Introduction to Linux signals by Jonas Jared Jacek is licensed under CC BY-SA 4.0.
This license requires that reusers give credit to the creator. It allows reusers to distribute, remix, adapt, and build upon the material in any medium or format, for noncommercial purposes only. To give credit, provide a link back to the original source, the author, and the license e.g. like this:
<p xmlns:cc="http://creativecommons.org/ns#" xmlns:dct="http://purl.org/dc/terms/"><a property="dct:title" rel="cc:attributionURL" href="https://www.ditig.com/introduction-to-linux-signals">Introduction to Linux signals</a> by <a rel="cc:attributionURL dct:creator" property="cc:attributionName" href="https://www.j15k.com/">Jonas Jared Jacek</a> is licensed under <a href="https://creativecommons.org/licenses/by-sa/4.0/" target="_blank" rel="license noopener noreferrer">CC BY-SA 4.0</a>.</p>
For more information see the Ditig legal page.