Processes in Linux are controlled by signal interrupts. Signals in Linux are one of the most basic and fundamental structures of Linux. Signals and processes control almost every task of the system. So it is worthwhile to learn about the basic structure and functioning of Linux signals.
In this tutorial, we will be discussing various signals, their functions and how to invoke them.
Also read: How to use the kill command in Linux?
What is a signal interrupts and why do we need it?
In an operating system like Linux, there are a number of processes running simultaneously. These processes are often dependent on the state of other processes running with them. They need a way to communicate with each other to maintain stability. A process communicates by sending a one-way notification called signals.
A signal necessarily does not need to be between two processes, a signal can be sent to and from kernel to/from a process.
Signals are sometimes described as signal interrupts, because in most cases, they interrupt the normal flow of execution of a program and their arrival is unpredictable.
Actions of signals
Each signal interrupt has a current disposition, which determines how the process behaves when the signal is delivered to them. There some default ways (
Actions ) by which a process responds to a signal, here is a table describing the different Actions for each signal.
|Term||The process is terminated resulting in an abnormal process termination|
|Ign||The signal is ignored and has no effect on the process.|
|Core||The process is terminated after creating a core dump file.|
|Stop||Suspend the process.|
|Cont||Continue the process if the process is suspended.|
A process can change the disposition of a signal using sigaction. Using these system calls, a process can elect one of the following behaviors to occur on delivery of the signal:
- perform the default action
- ignore the signal
- catch the signal with a signal handler, a programmer-defined function that is automatically invoked when the signal is delivered.
Some common signals and their significance
There are 31 standard signals, numbered 1-31. Each signal is named as “SIG” followed by a suffix. Starting from version 2.2, the Linux kernel supports 33 different real-time signals.
Here are few of the common signals that you might have encountered:
|Signal Name||Signal Number||Singal Action||Description|
|SIGHUP||1||Term||Hang up detected on controlling terminal or death of controlling process|
|SIGINT||2||Term||Keyboard interrupt (usually Ctrl-C)|
|SIGKILL||9||Term||Immediate process termination with no cleanup.|
|SIGSEGV||11||Core||Invalid memory reference|
Signal number 0, which in POSIX.1 is called
null signal, is generally not used but
kill function uses this as a special case. No signal is sent but it can be used to check if the process still exists.
Sending a signal interrupt to a process
One of the most common methods for sending signal interrupts is to use the
kill function, the syntax of which is as follows:
kill -signal pid
SIGHUP signal is sent to the process with pid 1001
kill -1 1001
SIGKILL signal is sent to the process with pid 1001 to kill the process immediately.
kill -9 1001
There also exists another function
raise that sends a signal to the calling process itself. The signal is sent as such that the signal is received by the process even before the raise function returns.
Conclusion and references
This brings us to the end of this introductory tutorial on Linux Signals. Stay tuned for more such articles on Linux and other open-source programs.