Appearance
40. Signals
A signal is a software interrupt notifying a process that an event occurred — no data transferred, just a notification ("something happened").
| Signal Number Meaning | ||
|---|---|---|
| SIGINT | 2 | Ctrl+C — interrupt |
| SIGTERM | 15 | Graceful termination request |
| SIGKILL | 9 | Force kill (cannot be caught) |
| SIGSEGV | 11 | Segmentation fault |
| SIGCHLD | 17 | Child stopped/terminated |
| SIGALRM | 14 | Timer expired |
| SIGPIPE | 13 | Broken pipe (reader closed) |
| SIGHUP | 1 | Terminal hangup / reload config |
| SIGSTOP | 19 | Pause (cannot be caught) |
| SIGCONT | 18 | Resume paused process |
Handling: default action (e.g., SIGTERM → terminate), a custom registered handler, or explicit ignore. SIGKILL and SIGSTOP can never be caught, blocked, or ignored — this is why kill -9 always works.
Real-world use: graceful shutdown (load balancer sends SIGTERM, server finishes in-flight requests then exits), config reload without restart (SIGHUP, used by nginx), crash logging (SIGSEGV handler logs a stack trace), and zombie prevention (parent's SIGCHLD handler calls wait() to reap the child immediately).
Safety warning: handlers run asynchronously and can interrupt code anywhere, so only async-signal-safe functions are allowed inside them — write(), _exit(), kill() are safe; printf(), malloc(), and any lock are not.