Linux Signals Cheat Sheet
What Are Signals?
Signals are software interrupts sent to a process to notify it of various events such as termination, interrupts, alarms, etc.
Common Signals and Their Uses
| Signal | Number | Description | Use Case |
|-----------|--------|------------------------------------|-------------------------------------|
| SIGINT |2 | Interrupt from keyboard (Ctrl + C) | Gracefully stop a process |
| SIGKILL | 9 | Kill signal | Forcefully terminate a process |
| SIGALRM | 14 | Alarm clock | Sent after timer set by alarm() |
| SIGUSR1 | 10 | User-defined signal 1 | Custom inter-process communication |
| SIGUSR2 | 12 | User-defined signal 2 | Custom inter-process communication |
| SIGCHLD | 17 | Child stopped or terminated | Parent knows child finished |
| SIGTERM | 15 | Termination signal | Graceful stop (default kill) |
| SIGSTOP | 19 | Stop process execution | Pause a process (can't be caught) |
| SIGHUP |1 | Hangup | Terminal closed or restart process |
When to Use Which Signal
- Use SIGALRM: When you want to trigger an action after a specific time using alarm().
- Use SIGUSR1 / SIGUSR2: For custom communication between parent-child or other processes.
- Use SIGINT: To handle Ctrl+C input from the terminal.
- Use SIGCHLD: To know when a child process ends.
- Use SIGKILL / SIGSTOP: To forcefully stop or pause a process.
Useful Functions
- signal(sig, handler): Assigns a custom function to handle a specific signal.
- kill(pid, sig): Sends a signal to another process.
Linux Signals Cheat Sheet
- raise(sig): Sends a signal to the current process.
- pause(): Suspends the process until a signal is received.
- alarm(seconds): Sends SIGALRM after given seconds.
- sigaction(): Advanced signal handling (recommended for robust apps).
- sigprocmask(): Block or unblock signals temporarily.