top of page

Understanding Interrupts in Operating System: Definition, Types, and Examples



📙 Introduction to Interrupts


An interrupt is a signal sent to the CPU by a device or program to request the CPU's attention. The CPU stops executing its current task and responds to the interrupt request. Interrupts are essential for multitasking and allowing the CPU to handle multiple tasks simultaneously.

Interrupts can be classified into two categories: hardware interrupts and software interrupts. Hardware interrupts are generated by hardware devices, such as a keyboard or a network card, while software interrupts are generated by software programs running on the system.


When an interrupt occurs, the CPU saves the current state of execution, such as the values of registers and the program counter, and jumps to a specific interrupt handler routine. The interrupt handler routine is responsible for servicing the interrupt, which can involve acknowledging the interrupt request, processing the data sent by the device or program that triggered the interrupt, and resuming the execution of the interrupted program.


📙 Types of Interrupts


Interrupts can be categorized into various types based on their source, purpose, and priority. Here are some of the common types of interrupts:


✔ Hardware Interrupts


Hardware interrupts are generated by external hardware devices that require the CPU's attention. These interrupts can be further classified into the following subtypes:

  • Maskable Interrupts: These interrupts can be temporarily disabled or masked by the CPU, allowing the system to prioritize other interrupts or tasks. An example of a maskable interrupt is the interrupt generated by a network card when data is received.

  • Non-Maskable Interrupts: These interrupts cannot be masked or ignored by the CPU and require immediate attention. Examples of non-maskable interrupts include system reset signals and hardware errors.

✔ Software Interrupts


Software interrupts are generated by software programs running on the system. These interrupts are usually used to request a system service or to invoke a system call. Software interrupts are also known as traps or exceptions. Here are some of the common types of software interrupts:

  • System Calls: A system call is a request made by a user-level program to the kernel for a service, such as opening a file or creating a process. The user-level program invokes a software interrupt, which transfers control to the kernel. The kernel performs the requested service and returns control to the user-level program.

  • Faults: A fault is a software interrupt that occurs when a program tries to access an invalid memory address or perform an illegal instruction. The kernel can handle these interrupts by terminating the program or performing error recovery.

  • Signals: A signal is a software interrupt used to notify a process of an event, such as a keyboard interrupt or a process termination request. Processes can also send signals to other processes to communicate information or request a specific action.

📙 Interrupt Handlers


Interrupt handlers are routines that handle the interrupts generated by hardware devices or software programs. When an interrupt occurs, the CPU transfers control to the corresponding interrupt handler routine, which is responsible for servicing the interrupt.

Interrupt handlers must be fast and efficient to minimize the delay in processing interrupts. They should also be designed to handle multiple interrupts simultaneously and avoid conflicts or race conditions between different interrupt handlers.


Interrupt handlers can be written in assembly language or high-level programming languages. In modern operating systems, interrupt handlers are usually written in C or C++ and are part of the kernel code.


An interrupt handler is a software routine that is executed when an interrupt occurs. It is responsible for handling the interrupt and performing the necessary actions. There are two types of interrupt handlers: system-level interrupt handlers (SLIHs) and first-level interrupt handlers (FLIHs).


✔ System-Level Interrupt Handlers (SLIHs):


SLIHs are responsible for handling interrupts that are generated by hardware devices, such as disk drives, network interface cards (NICs), and keyboards. They are executed in kernel mode and have access to system resources, such as memory and devices. When a hardware device generates an interrupt, the processor saves the current state of the system and transfers control to the SLIH. The SLIH then identifies the source of the interrupt and performs the necessary actions.


✔ First-Level Interrupt Handlers (FLIHs):


FLIHs are responsible for handling interrupts that are generated by software programs or the operating system. They are executed in user mode and have limited access to system resources. When a software program generates an interrupt, the processor saves the current state of the system and transfers control to the FLIH. The FLIH then identifies the source of the interrupt and performs the necessary actions.


📙 Types of Interrupt Handlers:

  1. Deferred Interrupt Handlers: Deferred interrupt handlers are executed after the interrupt has been handled by the SLIH or FLIH. They are responsible for performing tasks that require additional processing time, such as disk I/O operations. Deferred interrupt handlers are executed in kernel mode and have access to system resources.

  2. Fast Interrupt Handlers: Fast interrupt handlers are executed immediately after the interrupt has been handled by the FLIH. They are used for time-critical operations, such as real-time signal processing. Fast interrupt handlers are executed in kernel mode and have access to system resources.

  3. Bottom Half Handlers: Bottom half handlers are a type of deferred interrupt handler that is executed after the interrupt has been handled by the SLIH. They are used for tasks that can be deferred, such as processing network packets or disk I/O operations. Bottom half handlers are executed in kernel mode and have access to system resources.

SLIHs and FLIHs are both responsible for handling interrupts, but they differ in their execution context and the level of access to system resources. FLIHs are executed in user mode and have limited access to system resources, while SLIHs are executed in kernel mode and have full access to system resources. Deferred interrupt handlers, fast interrupt handlers, and bottom half handlers are different types of interrupt handlers that are used for specific tasks and have different execution contexts.


Interrupt Latency - Interrupt latency is the time taken by the system to respond to an interrupt. It is the time elapsed between the occurrence of an interrupt and the start of the execution of the corresponding interrupt handler. Interrupt latency is an important metric in determining the responsiveness of a system.


The interrupt latency can be calculated using the following formula:

Interrupt latency = (T2 - T1) - (Tservice + Tdispatch)

where T1 is the time at which the interrupt occurred, T2 is the time at which the interrupt handler starts executing, Tservice is the time taken by the interrupt handler to service the interrupt, and Tdispatch is the time taken by the system to dispatch the interrupt to the interrupt handler.

In general, the interrupt latency should be kept as low as possible to ensure the system is responsive to interrupts. A high interrupt latency can lead to missed interrupts and degraded system performance.


📙 How does CPU response to interrupts ?


When an interrupt occurs, the CPU must respond quickly to ensure the proper handling of the interrupt. The response of the CPU to interrupts can be broken down into several steps:

  1. Interrupt Detection: The first step in handling an interrupt is detecting that an interrupt has occurred. This is done by monitoring the interrupt request (IRQ) lines from the devices. When an IRQ line is asserted, it indicates that a device needs attention.

  2. Interrupt Acknowledgement: Once an interrupt is detected, the CPU sends an acknowledgement signal to the device. This lets the device know that the interrupt has been acknowledged and that it can stop sending interrupts.

  3. Interrupt Handling: After acknowledging the interrupt, the CPU will jump to the interrupt handler routine to service the interrupt. The interrupt handler is a software routine that is responsible for handling the interrupt. The interrupt handler will save the current state of the CPU, execute the interrupt service routine, and then restore the CPU state before returning to the interrupted task.

  4. Interrupt Service Routine: The interrupt service routine (ISR) is the routine that actually services the interrupt. The ISR performs the necessary actions to handle the interrupt. For example, in the case of an I/O interrupt, the ISR may read data from a device or write data to a device.

  5. Interrupt Completion: After the ISR has completed servicing the interrupt, it sends an end-of-interrupt (EOI) signal to the interrupt controller. This lets the interrupt controller know that the interrupt has been handled and that it can resume normal operation.

It is important for the CPU to respond quickly to interrupts to ensure the proper functioning of the system. Interrupt response time can be improved by minimizing interrupt latency, optimizing interrupt handling routines, and reducing the number of interrupts that are generated.


Thanks for reading, and happy coding!


Understanding Interrupts in Operating System: Definition, Types, and Examples -> CPU Context Switching in Operating System: Definition, Process, and Examples


bottom of page