Linux System Programming · beginner · ~8 min
Understand what a signal is, who sends them, and what happens by default when one arrives.
A signal is a software interrupt delivered by the kernel to a process. It is essentially a small notification: a number that says "something happened."
Signals come from three sources:
kill() system callEvery long-running C program needs to handle SIGINT and SIGTERM cleanly. If it does not, ending the program can leave behind locked files, stale network sockets, and lost data.
Signals are also how the Linux kernel reports trouble. When your program crashes, the kernel tells it so by sending a signal.
Signal An integer identifier. The standard signals are numbered 1 to 31. There are also real-time (RT) signals above that range.
Default action What happens when a signal arrives and the program has not arranged otherwise. The default depends on the signal and is one of: terminate, terminate and write a core dump, ignore, stop, or continue.
Catching
Replacing the default action with your own code, by installing a handler. You do this with signal() or sigaction() (covered in later lessons).
Uncatchable signals Two signals can never be caught, blocked, or ignored: SIGKILL (9) and SIGSTOP (19).
Standard vs. real-time signals Standard signals coalesce: if several copies arrive before the program reacts, they collapse into a single delivery. Real-time signals (SIGRTMIN to SIGRTMAX) queue, so each one is delivered separately.
A signal is a software interrupt that the kernel delivers to a process.
It is the simplest form of inter-process communication (IPC) that Unix offers. There is no shared memory and no socket. There is just a number, such as SIGINT = 2 or SIGTERM = 15, plus the event of delivering it.
Signals have three sources:
Ctrl+C sends SIGINT. Ctrl+\ sends SIGQUIT.kill(pid, signo) or the shell's kill command.SIGSEGV. An integer divide-by-zero triggers SIGFPE.Each signal has a default action. Most of the time it is "terminate the process." Sometimes it is "terminate and dump core," and sometimes it is "ignore."
A process can override the default by installing a signal handler (see the next lesson).
Two signals, SIGKILL and SIGSTOP, cannot be caught, blocked, or ignored. They are the kernel's escape hatch for forcing a process to stop no matter what.
/* Run this, then press Ctrl+C. The default SIGINT action terminates. */
#include <stdio.h>
#include <unistd.h>
int main(void) {
for (int i = 0; i < 30; i++) {
printf("tick %d\n", i);
sleep(1);
}
return 0;
}
SIGINT are in flight at once, they collapse into a single delivery. If you need true queuing, use real-time signals (SIGRTMIN to SIGRTMAX).SIGKILL and SIGSTOP are uncatchable. No handler will ever run for them.kill -USR1 <pid>.kill) are the polite "please stop" pair; SIGKILL cannot be refused.