1. Introduction

Signals and interrupts in Linux are used to handle asynchronous events, but they serve different purposes and operate at different levels of the system.

In this tutorial, we’ll first have a brief overview of signals and interrupts with examples. We’ll focus on both software interrupts and hardware interrupts. After that, we’ll discuss the difference between signal handler and interrupt service routine (ISR). Lastly, we’ll look at the signal and interrupt masking.

2. Signals in Linux

Signals are a means of communication between the OS kernel and OS processes. To elaborate further, they enable the OS or other processes to notify a given process about specific occurrences. Thus, a process can handle the signal through a designated signal handler.

2.1. Sources of Signals

We primarily use signals for inter-process communication (IPC) and handling exceptional conditions. Although the kernel is usually the source of most signals, there are also other possible sources:

  • exceptions during process executions, such as segmentation faults
  • hardware-generated signals, such as signals generated by a timer
  • signals sent by other processes using the kill system call

These various sources of signals effectively contribute to the signaling mechanism.

2.2. Examples of Signals

For example, we might want to check the file size while editing a file in the nano or vim text editor. To do so, we’ll suspend the current process by pressing Ctrl + Z. This sends the SIGTSTP (Terminal Stop) signal to the process and suspends it temporarily, placing it in the background.

After that, we can check the file size of any file we want and bring the suspended process back to the foreground using the fg command.

By using the SIGTSTP command, we sent a signal to the text editor process to suspend its execution and transfer the control to the terminal.

3. Interrupts in Linux

Interrupts are a mode of communication between the CPU and the OS kernel. In other words, interrupts are events that occur within the context of a running process. The CPU initiates the interrupt and usually manages it as well. In other words, it pauses the current task and triggers an ISR within the kernel.

There are many examples of interrupts that occur in the operating system:

  • keyboard interrupts, such as pressing a key on the keyboard
  • network data arrival which notifies the OS to handle incoming data packets
  • interrupts generated by hardware faults or invalid memory access
  • interrupts sent by external devices, such as printers
  • software-generated interrupts, such as system calls

In addition to the above examples, there are various interrupts that facilitate communication and the efficient working of the OS.

3.1. Inner Workings of Software Interrupts

Software interrupts are exceptions that occur due to special conditions. This type of interrupt is like a wake-up call for the system when something unusual happens.

For instance, the CPU devices can initiate an interrupt due to exceptions, such as a page fault, or the CPU instruction can invoke interrupts due to traps, such as breakpoints and system calls.

When such conditions occur, the processor raises an interrupt, and the OS kernel handles it by sending a signal to the affected process. After that, the kernel can terminate the program or assist it in responding properly.

For example, we can have a C program signal.c that divides two numbers:

$ cat signal.c
#include <stdio.h>
#include <signal.h>
#include <stdlib.h>
void divideByZeroHandler(int signum) {
    printf("Divide by zero exception occurred!\n");
    exit(1);
}
int main() {
    signal(SIGFPE, divideByZeroHandler);
    int dividend = 10;
    int divisor = 0;
    int result;
    printf("Attempting to divide %d by %d...\n", dividend, divisor);
    result = dividend / divisor;
    printf("Result: %d\n", result);
    return 0;
}

If the divisor is 0, the division results in an exception. When the exception occurs, the process raises an interrupt, and the kernel sends a SIGFPE (floating-point exception) to the process.

In particular, this happens when we compute result. After that, the program jumps to the registered signal handler that prints the error message. Lastly, it exits the program with an error status exit(1).

3.2. Inner Workings of Hardware Interrupts

On the other hand, hardware devices or external components generate hardware interrupts. When a hardware interrupt occurs, a process temporarily suspends and switches the control to an Interrupt Service Routine (ISR) defined by the kernel. After that, the ISR handles the specific interrupt.

For example, when we press any key on the keyboard, its driver sends a hardware interrupt to the kernel to process the new input. The processor stops what it’s doing and switches to a pre-defined action. This action, known as an Interrupt Service Routine (ISR), is a plan for how to handle specific signals.

The processor switches the control to the keyboard ISR to process the input value. After that, the ISR might save the key we pressed, store it in a buffer, or even wake up a program to deal with it. We can understand the process in the figure below:

       +-------------------+    +-------------------+
       |      Keyboard     |    |     Processor     |
       +-------------------+    +-------------------+
              |  Press Key               |
              |------------------------->|
              |                          |
              |  Hardware Interrupt      |
              |<-------------------------|
              |                          |
              |     Interrupt Service    |
              |     Routine (ISR)        |
              |------------------------->|
              |                          |
              |   Read Key Data          |
              |<-------------------------|
              |                          |
              |   Store in Buffer        |
              |------------------------->|
              |                          |

To summarize, when we press a key, the computer pauses the current task, deals with the pressed key, and goes back to what it was doing before.

4. Difference Between Signals and Interrupts

Signals and interrupts operate at different levels of the operating system. In particular, signals operate at the user level and the kernel level while interrupts operate at the hardware level and kernel level:

  +----------------------------------+
  |            User Level            |
  |                                  |
  |            +--------+            |
  |  +---------| Signal |            |
  |  |         +--------+            |
  |  |             ^                 |
  |  |             |                 |
  |  |             v                 |
  |  |         +--------+            |
  |  +-------->| Kernel |            |
  |            +--------+            |
  +----------------------------------+
                   |
                   v
            Hardware Level
                   |
                   v
         +-------------------+
         |     Interrupt     |
         +-------------------+
                   |
                   v
             Kernel Level

In addition, both are used for different purposes. We primarily use signals for communication and exception handling. In contrast, we use interrupts to handle hardware events.

5. Difference Between Signal Handler and Interrupt Handler

A signal handler is a function that is executed when a process receives a specific signal and exists in the user space code. In addition, a process can define its custom signal handlers using system calls.

On the other hand, an ISR handler is a low-level function provided by the kernel to handle the interrupts. When a hardware interrupt occurs, the processor switches to an ISR to handle the interrupt. In addition, ISRs operate at a lower level than signal handlers and reside in kernel space.

It’s crucial to note that ISRs are in kernel space and they prevent other processes from running in parallel while they’re being handled. Also, a bug in an ISR may cause data loss or hardware failure. Therefore, it’s important to avoid improper usage of ISR to prevent unpredictable machine behavior.

6. Difference Between Signal Masking and Interrupt Masking

Masking refers to the temporary suppression of signals and interrupts in a computer system. We stop the signal from being delivered to a process or we stop a hardware interrupt from being handled by the ISRs.

Signal blocking refers to temporarily blocking a process from receiving signals. We can achieve this using the sigprocmask() system call. In addition, when we block the signals, they’re pending until unblocked.

On the other hand, interrupt masking is a mechanism to disable hardware interrupts temporarily. When we mask an interrupt, the processor won’t respond to the corresponding hardware until we remove the masking.

For example, this feature is useful when we want to protect a critical section of code so that it can continue its execution atomically without being interrupted.

7. Conclusion

In this article, we learned the differences between signals and interrupts. Furthermore, we discussed the difference between a signal handler and an interrupt handler. Finally, we understood the difference between signal masking and interrupt masking.