Signal Interrupts in Linux – A Beginner’s Introduction

A Beginner's Introduction To Signal Interrupts In Linux

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.

Kernel Process Signal 1
Image 1: Flowchart representing the flow the signal between kernel and processes

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.

ActionDefault Disposition
TermThe process is terminated resulting in an abnormal process termination
IgnThe signal is ignored and has no effect on the process.
CoreThe process is terminated after creating a core dump file.
StopSuspend the process.
ContContinue the process if the process is suspended.
Table 1: Actions and their default dispositions

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 NameSignal NumberSingal ActionDescription
SIGHUP1TermHang up detected on controlling terminal or death of controlling process
SIGINT2TermKeyboard interrupt (usually Ctrl-C)
SIGQUIT3TermManual-interrupt(usually Ctrl-D)
SIGKILL9TermImmediate process termination with no cleanup.
SIGSEGV11CoreInvalid memory reference
Table 2: Some common signals with their respective actions.

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

Examples:

A SIGHUP signal is sent to the process with pid 1001

kill -1 1001

A 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.

References: