Chapter 20 covers the foundational concepts of Linux signals β the software interrupt mechanism that lets the kernel, other processes, and a process itself notify a running program that an event has occurred. From understanding what a signal is, through the complete API for sending, catching, blocking, and waiting for signals, this chapter is the essential foundation for reliable systems programming.
Signals appear heavily in Linux interview questions for embedded, systems, and kernel roles. This index links to all 19 dedicated topic pages, each with code examples and interview Q&A.
01 Β· What is a Signal? β definition, sources, lifecycle 02 Β· Signal Generation & Delivery β async vs sync, standard vs realtime 03 Β· Pending Signals β the concept, between generation and delivery 04 Β· Signal Mask & Blocking β blocking signals, the signal mask 05 Β· Signal Dispositions β SIG_DFL, SIG_IGN, handlers, default actions 06 Β· Signal Handlers β writing handlers, volatile flag pattern 07 Β· Signal Types & Default Actions β full reference table (30 signals) 08 Β· The signal() API β prototype, sighandler_t, save/restore 09 Β· Sending Signals: kill() β pid rules, permissions, EPERM/ESRCH 10 Β· Null Signal & Process Existence Check β kill(pid, 0) 11 Β· raise() & killpg() β self-signal, process group signal 12 Β· strsignal(), psignal(), sys_siglist β signal descriptions 13 Β· Signal Sets (sigset_t) β sigemptyset, sigfillset, sigaddset, sigismember 14 Β· sigprocmask() β SIG_BLOCK, SIG_UNBLOCK, SIG_SETMASK, critical sections 15 Β· sigpending() β checking pending signals, /proc/PID/status 16 Β· Signals Are Not Queued β bitmask pending set, sig_sender/sig_receiver 17 Β· sigaction() β struct sigaction, sa_mask, SA_RESTART, SA_NODEFER 18 Β· pause() β suspend until signal, EINTR, race condition warning 19 Β· Chapter Summary & Interview Master List (this page)
| Function | Purpose | Returns |
|---|---|---|
| kill(pid, sig) | Send signal to process(es) | 0 or -1 |
| raise(sig) | Send signal to self | 0 or nonzero |
| killpg(pgid, sig) | Send signal to process group | 0 or -1 |
| signal(sig, handler) | Set signal disposition (legacy) | old handler or SIG_ERR |
| sigaction(sig, act, old) | Set/get signal disposition (POSIX) | 0 or -1 |
| sigprocmask(how, set, old) | Examine/change signal mask | 0 or -1 |
| sigpending(set) | Get set of pending signals | 0 or -1 |
| pause() | Wait until any signal arrives | -1 (EINTR) |
| sigemptyset(set) | Initialize set to empty | 0 or -1 |
| sigfillset(set) | Initialize set to all signals | 0 or -1 |
| sigaddset(set, sig) | Add signal to set | 0 or -1 |
| sigdelset(set, sig) | Remove signal from set | 0 or -1 |
| sigismember(set, sig) | Test if signal is in set | 1 (yes), 0 (no), -1 (error) |
| strsignal(sig) | Return signal description string | char* (not thread-safe) |
kill(pid, 0) β the null signal. If it returns 0, the process exists and you have permission to send signals. If it returns -1 with errno=ESRCH, the process does not exist. If errno=EPERM, the process exists but you lack permission. Watch out for zombie processes and PID recycling.while (!flag) pause(); and how is it fixed?sigsuspend(&empty_mask), which atomically unblocks signals and suspends β eliminating the window between checking the flag and sleeping.volatile prevents the compiler from caching the variable in a register and optimizing away re-reads (a while loop may otherwise become an infinite loop because the compiler assumes the variable never changes). sig_atomic_t is the only integer type guaranteed by the C standard to be read and written atomically on the target architecture, avoiding partial-update corruption.| Signal | Default Action | Common Cause |
|---|---|---|
| SIGINT (2) | Terminate | Ctrl-C from terminal |
| SIGTERM (15) | Terminate | kill PID (default signal) |
| SIGKILL (9) | Terminate (uncatchable) | kill -9 PID |
| SIGSEGV (11) | Core dump | Invalid memory access |
| SIGCHLD (17) | Ignore | Child terminates/stops |
| SIGALRM (14) | Terminate | alarm() or setitimer() expiry |
| SIGUSR1 (10) | Terminate | User-defined use |
| SIGUSR2 (12) | Terminate | User-defined use |
| SIGSTOP (19) | Stop (uncatchable) | kill -STOP PID |
| SIGCONT (18) | Continue | kill -CONT PID |
| SIGHUP (1) | Terminate | Terminal closed / daemon reload |
| SIGABRT (6) | Core dump | abort() called |
