Linux signals are a mechanism used for inter-process communication in Unix and Unix-like operating systems, such as Linux. They are used to notify a process that a specific event has occurred, such as an error, an external interrupt, or a software condition trigger.
How They Work
- Generation of Signal: Signals can be generated by the operating system kernel, a process (towards another process), or a user (through a terminal or tool).
- Sending Signal: Once generated, the signal is sent to the target process.
- Receiving Signal: Upon receiving a signal, the target process can take several different actions:
- Execute the default signal handling behavior (such as terminating the process, ignoring the signal, etc.).
- Catch the signal and execute a custom handler function.
- Ignore the signal (some signals cannot be ignored).
Common Linux Signals
SIGINT
(Signal number 2): Interrupt signal. When the user pressesCtrl+C
, the terminal sends this signal to all processes in the foreground process group.SIGTERM
(Signal number 15): Termination signal. Typically used to request a program to exit gracefully.SIGKILL
(Signal number 9): Forceful termination signal. Used to immediately terminate a program and cannot be caught or ignored.SIGSTOP
(Signal number 17): Stop signal. Used to pause a program’s execution, also cannot be caught or ignored.SIGSEGV
(Signal number 11): Segmentation fault signal. Sent when a program tries to access illegal memory.SIGHUP
(Signal number 1): Hangup signal. Commonly used to notify a program that its configuration file has been changed and needs to be reloaded.SIGUSR1
andSIGUSR2
: User-defined signals, used for specific purposes in specific programs.
SIGHUP
(1): Hangup detected on controlling terminal or death of controlling process. Used to report that the user’s terminal is disconnected.SIGINT
(2): Interrupt from keyboard (Ctrl+C). Used to terminate a program.SIGQUIT
(3): Quit from keyboard (Ctrl+). Causes the process to terminate and produce a core dump.SIGILL
(4): Illegal Instruction. Indicates a program is trying to execute an invalid instruction.SIGTRAP
(5): Trace/breakpoint trap. Used by debuggers.SIGABRT
(6): Abort signal fromabort()
system call.SIGBUS
(7): Bus error. Indicates an invalid memory access.SIGFPE
(8): Floating-point exception. Indicates an erroneous arithmetic operation, such as division by zero.SIGKILL
(9): Kill signal. Forces the process to terminate immediately.SIGUSR1
(10): User-defined signal 1.SIGSEGV
(11): Invalid memory reference. Indicates a segmentation fault.SIGUSR2
(12): User-defined signal 2.SIGPIPE
(13): Broken pipe. Occurs when a process writes to a pipe with no readers.SIGALRM
(14): Timer signal fromalarm()
system call.SIGTERM
(15): Termination signal. Used to request a graceful shutdown of a process.SIGSTKFLT
(16): Stack fault on coprocessor (not common on most architectures).SIGCHLD
(17): Child stopped or terminated. Sent to a parent process when its child terminates or stops.SIGCONT
(18): Continue if stopped. Instructs the operating system to continue (restart) a stopped process.SIGSTOP
(19): Stop process. Causes the process to stop executing without terminating.SIGTSTP
(20): Stop typed at terminal (Ctrl+Z). Requests a process to stop temporarily.SIGTTIN
(21): Terminal input for background process. Sent to a background process attempting to read input from the terminal.SIGTTOU
(22): Terminal output for background process. Sent to a background process attempting to write output to the terminal.SIGURG
(23): Urgent condition on socket.SIGXCPU
(24): CPU time limit exceeded.SIGXFSZ
(25): File size limit exceeded.SIGVTALRM
(26): Virtual alarm clock.SIGPROF
(27): Profiling timer expired.SIGWINCH
(28): Window resize signal.SIGIO
(29): I/O now possible. Also known asSIGPOLL
.SIGPWR
(30): Power failure.SIGSYS
(31): Bad system call. Also known asSIGUNUSED
.
It’s important to note that not all signals can be caught, blocked, or ignored. For example, SIGKILL
and SIGSTOP
cannot be caught by the process. The behavior of signals can vary slightly between different Unix-like systems, but the above list is generally applicable to most Linux distributions.