What are signals, and how are they delivered and handled?
Short answer: A signal is an asynchronous notification sent to a process by the kernel, another process (kill()), or the process itself. Each signal has a default action (terminate, terminate with core dump, ignore, stop, continue). A process can change the action for most signals by installing a handler with sigaction() or ignoring them, but SIGKILL and SIGSTOP can never be caught, blocked or ignored. The kernel delivers a pending signal when the process next returns from the kernel to user mode, by making it jump to the handler on its own stack.
Common signals
| Signal | Default | Typical cause | How often |
|---|---|---|---|
SIGINT (2) |
terminate | Ctrl-C | common |
SIGTERM (15) |
terminate | polite shutdown request (kill, systemd stop) |
common |
SIGKILL (9) |
terminate (uncatchable) | kill -9, OOM killer |
when things go wrong |
SIGSEGV (11) |
core dump | invalid memory access | bugs |
SIGBUS |
core dump | unaligned/invalid physical access, truncated mmap file | rare |
SIGILL |
core dump | illegal instruction | rare |
SIGFPE |
core dump | integer divide by zero | rare |
SIGCHLD |
ignore | child exited or stopped | very frequent in shells/servers |
SIGPIPE |
terminate | write to a pipe/socket with no reader | frequent in network code |
SIGALRM |
terminate | alarm() timer |
depends |
SIGHUP |
terminate | terminal closed; by convention "reload config" for daemons | occasional |
SIGSTOP/SIGTSTP, SIGCONT |
stop / continue | Ctrl-Z, fg |
interactive use |
SIGUSR1/SIGUSR2 |
terminate | application-defined | as designed |
Installing a handler correctly
#include <signal.h>
#include <string.h>
#include <unistd.h>
static volatile sig_atomic_t stop;
static void on_term(int sig)
{
(void)sig;
stop = 1; /* only set a flag: handler must be async-signal-safe */
}
int main(void)
{
struct sigaction sa;
memset(&sa, 0, sizeof sa);
sa.sa_handler = on_term;
sigemptyset(&sa.sa_mask);
sigaction(SIGTERM, &sa, NULL);
sigaction(SIGINT, &sa, NULL);
signal(SIGPIPE, SIG_IGN); /* get EPIPE from write() instead of dying */
while (!stop) {
/* main loop work */
pause();
}
/* clean shutdown: flush files, close sockets */
return 0;
}
Rules for handlers
- The handler can run between any two instructions of the main program. Only call async-signal-safe functions (
write,_exit,sem_post, setting avolatile sig_atomic_t).printf,mallocand most library functions are not safe: if the main code was insidemallocholding its lock, callingmallocin the handler deadlocks. - Prefer
sigactionoversignal(), whose semantics differ between systems. - Use
SA_RESTARTso slow system calls are restarted instead of failing withEINTR, or handleEINTRexplicitly.
Better designs for real programs: block the signals and receive them as file descriptor events with signalfd() (fits into an epoll loop), or use the self-pipe trick (the handler writes a byte to a pipe watched by the main loop).
When to write a custom handler: graceful shutdown on SIGTERM (save state, close files), reloading configuration on SIGHUP, reaping children on SIGCHLD, and ignoring SIGPIPE in network servers. Catching SIGSEGV to "recover" is almost always wrong; at most log a backtrace and exit.