
Signals in Linux
Fri Nov 17 2023
Today i want to talk about signals in Linux. Linux, like other POSIX compatible operating systems (glares at windows), uses a variety of signals to communicate with processes. These signals, which are essentially simple notifications sent to a program by the operating system or another program, inform the program about various events.
I myself have worked with handling signals such as SIGQUIT and SIGKILL when i did electron development during my time at Amazon. In the average day-to-day, we work with signals all the time, probably most commonly SIGKILL. Understanding them more in-depth is a handy and useful tool in your linux skill kit.
What are Signals?
In Linux, a signal is a limited form of inter-process communication (IPC) used to notify a process that a particular event has occurred. A signal can be sent by the operating system to a process, by a process to another process, or by a process to itself.
Types of Signals
Linux supports a range of signals, each identified by a number and usually a corresponding name. Here’s a look at some of the most common ones:
- SIGINT (Signal Interrupt): Signal number 2. It's sent to a process by its controlling terminal when a user wishes to interrupt the process. This is commonly seen when you press Ctrl+C.
- SIGQUIT (Signal Quit): Signal number 3. It's similar to SIGINT but also causes the process to create a core dump, which is a snapshot of the process’s memory at the time of termination.
- SIGKILL (Signal Kill): Signal number 9. This signal forces a process to terminate immediately. Unlike SIGTERM, SIGKILL cannot be caught or ignored by the process.
- SIGTERM (Signal Terminate): Signal number 15. This is a polite request to a process to terminate. The process can catch this signal and perform clean-up tasks before exiting.
- SIGSEGV (Signal Segmentation Violation): Signal number 11. This signal is sent to a process when it makes an invalid memory reference, or segmentation fault.
- SIGSTOP (Signal Stop): Signal number 17, 19, 23 (depending on the system). This signal stops (pauses) a process. It’s similar to SIGTSTP but cannot be caught or ignored.
- SIGCONT (Signal Continue): Signal number 18. It's used to restart a process that has been stopped by SIGSTOP or SIGTSTP.
- SIGCHLD (Signal Child): Signal number 17. Sent to a parent process when one of its child processes terminates or stops.
- SIGTSTP (Signal Terminal Stop): Signal number 20. Similar to SIGSTOP, it's sent to a process by its controlling terminal to request it to stop (pause). It can be intercepted and handled by the process.
Handling Signals
In Linux, processes can handle signals in three ways:
- Default action: Most signals have a default action, which could be to terminate the process, ignore the signal, or stop or continue the process.
- Catch the signal: A process can register a function (signal handler) to run when a specific signal is received.
- Ignore the signal: A process can explicitly ignore a signal.
Comprehensive List of Linux Signals
Here's a detailed look at all of the various signals in Linux:
- SIGHUP (1): Sent when the terminal associated with a process is closed.
- SIGINT (2): Interrupt signal, typically sent when the user presses Ctrl+C.
- SIGQUIT (3): Sent when the user requests a process quit and dump core.
- SIGILL (4): Indicates an illegal instruction in the program.
- SIGTRAP (5): Used mainly in debuggers and tracers.
- SIGABRT (6): Sent when the abort() function is called, signaling an emergency stop.
- SIGBUS (7): Indicates incorrect memory access.
- SIGFPE (8): Sent on floating point exceptions.
- SIGKILL (9): Forces process termination, cannot be caught or ignored.
- SIGUSR1 (10): User-defined signal, left for programmers to use.
- SIGSEGV (11): Indicates invalid memory access, commonly a segmentation fault.
- SIGUSR2 (12): Another user-defined signal for programmers.
- SIGPIPE (13): Sent to a process writing to a broken pipe.
- SIGALRM (14): Alarm signal sent after a specified time, set by alarm() function.
- SIGTERM (15): Polite request to terminate a process.
- SIGCHLD (17): Sent to a parent process when a child process terminates or stops.
- SIGCONT (18): Continues a paused process.
- SIGSTOP (19): Pauses a process, cannot be caught or ignored.
- SIGTSTP (20): Terminal stop signal, similar to SIGSTOP.
- SIGTTIN (21): Sent when a background process attempts to read input from its terminal.
- SIGTTOU (22): Sent when a background process attempts to write to its terminal.
- SIGURG (23): Indicates urgent data on a network socket.
- SIGXCPU (24): Sent when a process exceeds its CPU limit.
- SIGXFSZ (25): Sent when a process tries to create a file larger than the file size limit.
- SIGVTALRM (26): Virtual alarm signal for process time.
- SIGPROF (27): Profiling alarm for process and system time.
- SIGWINCH (28): Sent when a window is resized (mostly unused now).
- SIGIO (29): Sent when I/O is available or an output channel is ready for writing.
- SIGPWR (30): Power failure signal, indicating a switch to emergency power.
- SIGSYS (31): Sent on bad system call (unused in most implementations).
Understanding signals in Linux comes in handy and is essential for effectively managing processes and system resources. Whether you are a system administrator, a dev or just a Linux user, familiarizing yourself with these signals can greatly enhance your proficiency in Linux.
Remember, while some signals can be caught or ignored, others like SIGKILL and SIGSTOP are non-negotiable and are always handled by the Linux kernel. Happy signaling in Linux!

