Interrupts
1. What is an Interrupt?
An interrupt is a signal or event that causes the CPU to temporarily stop its current execution and transfer control to a special routine called an Interrupt Service Routine (ISR) to handle the event.
In simple words:
An interrupt is a notification that tells the CPU, “Something needs your attention.”
Example:
CPU is running a program ↓ Keyboard key is pressed ↓ Keyboard generates interrupt ↓ CPU temporarily handles keyboard event ↓ CPU returns to previous program
2. Why Do We Need Interrupts?
Without interrupts, the CPU could continuously check devices.
For example, with polling:
CPU → Is keyboard ready? CPU → Is keyboard ready? CPU → Is keyboard ready? CPU → Is keyboard ready?
This wastes CPU time.
With interrupts:
CPU → Continue normal work ↓ Keyboard event ↓ INTERRUPT ↓ CPU
The CPU doesn't need to continuously ask.
Main idea
Polling = CPU asks the device.
Interrupt = Device/event tells the CPU.
3. Basic Interrupt Architecture
CPU ↑ │ Interrupt │ │ ┌───────┴────────┐ │ │ Keyboard Disk │ │ Network Timer │ │ └────── Devices ┘
Different hardware components can generate interrupt requests.
4. Interrupt Handling Flow
A simplified flow is:
CPU executes program ↓ Interrupt occurs ↓ CPU detects interrupt ↓ Save current CPU state ↓ Identify interrupt ↓ Find appropriate ISR ↓ Execute ISR ↓ Restore CPU state ↓ Resume previous program
5. Detailed Interrupt Process
Suppose a keyboard key is pressed.
Step 1
CPU is executing:
Program A
Step 2
User presses:
A
Step 3
Keyboard controller generates an interrupt request.
Keyboard ↓ Interrupt Request ↓ CPU
Step 4
CPU recognizes the interrupt.
Step 5
CPU saves enough state to resume the interrupted execution.
Step 6
CPU determines which interrupt occurred.
Step 7
The corresponding interrupt handler runs.
Step 8
The handler processes the keyboard event.
Step 9
CPU restores the interrupted execution state.
Step 10
Program A continues.
Program A ↓ Interrupt ↓ Keyboard ISR ↓ Program A continues
6. Interrupt Service Routine (ISR)
An Interrupt Service Routine (ISR) is a special piece of code executed when a particular interrupt occurs.
It is also called an:
Interrupt Handler
Example:
Keyboard interrupt ↓ Keyboard ISR ↓ Read keyboard data ↓ Process event ↓ Return
7. What Does an ISR Do?
An ISR generally:
- identifies/handles the event
- reads device status/data
- acknowledges the interrupt
- performs urgent processing
- schedules additional work if needed
- returns control to the interrupted execution
Example:
Keyboard ISR ↓ Read key information ↓ Store/process event ↓ Acknowledge interrupt ↓ Return
8. Interrupt Vector
The CPU needs to know:
"Which handler should I execute for this interrupt?"
This information is provided through an interrupt vector or related interrupt-vector mechanism.
Conceptually:
Interrupt Number ↓ Interrupt Vector ↓ Address of ISR ↓ ISR executes
Example:
Interrupt 5 ↓ Vector 5 ↓ Keyboard Handler
9. Interrupt Vector Table
An Interrupt Vector Table (IVT) is a table containing references/addresses to interrupt handlers.
Simplified:
Interrupt Vector Table ┌───────────────┬─────────────────────┐ │ Interrupt No. │ Handler │ ├───────────────┼─────────────────────┤ │ 0 │ Handler 0 │ │ 1 │ Handler 1 │ │ 2 │ Handler 2 │ │ 3 │ Handler 3 │ │ ... │ ... │ └───────────────┴─────────────────────┘
When an interrupt occurs:
Interrupt number ↓ Vector table ↓ ISR address ↓ ISR
10. Interrupt Controller
A system may have hardware that manages interrupt requests from multiple devices.
An interrupt controller can:
- receive interrupt requests
- prioritize them
- mask/enable interrupts
- route interrupts to CPU cores
- help manage interrupt delivery
Conceptually:
Keyboard ──┐ Disk ──────┤ Network ───┼──→ Interrupt Controller ──→ CPU Timer ─────┤ USB ───────┘
Modern systems commonly use more advanced interrupt-controller architectures, but the basic concept remains the same.
11. Interrupt Request (IRQ)
An IRQ is an interrupt request generated by hardware.
Example:
Keyboard ↓ IRQ ↓ Interrupt Controller ↓ CPU
Historically, systems had fixed IRQ lines such as IRQ 0, IRQ 1, etc. Modern systems can use more flexible interrupt routing.
12. Hardware Interrupts
A hardware interrupt is generated by a hardware device or hardware event.
Examples:
- keyboard input
- mouse movement
- network packet arrival
- disk operation completion
- timer event
- USB device event
Example:
Network Card ↓ Hardware Interrupt ↓ CPU
13. Software Interrupts
A software interrupt is generated by software through a defined mechanism.
Historically, CPUs provided instructions such as:
INT
Software could invoke a particular interrupt number.
Software-generated interrupt mechanisms have been used for:
- system services
- debugging
- controlled transitions into privileged code
Modern operating systems commonly use dedicated system-call instructions rather than traditional software interrupt instructions for normal system calls, depending on CPU architecture.
14. Hardware vs Software Interrupts
| Feature | Hardware Interrupt | Software Interrupt |
|---|---|---|
| Generated by | Hardware/event | Software |
| Example | Keyboard | Software interrupt instruction |
| Purpose | Notify CPU of hardware event | Request/trigger software-defined handling |
| Source | Device/system hardware | Program/instruction |
15. Maskable Interrupt
A maskable interrupt is an interrupt that the CPU/system can temporarily disable or mask under appropriate conditions.
Example:
Mask enabled ↓ Interrupt temporarily ignored/deferred
This is useful when the CPU must perform a critical operation without being interrupted by certain interrupt sources.
16. Non-Maskable Interrupt — NMI
A Non-Maskable Interrupt (NMI) is intended for very important hardware conditions that should not be ignored through normal interrupt masking.
Examples can include serious hardware-related conditions depending on the platform.
Conceptually:
Normal interrupt ↓ Can potentially be masked NMI ↓ High priority / cannot normally be masked
17. Maskable vs Non-Maskable
| Feature | Maskable | Non-Maskable |
|---|---|---|
| Can normally be disabled/masked? | Yes | No |
| Priority | Normal/varies | Very high |
| Used for | Normal device events | Critical hardware conditions |
| Example | Keyboard/network event | Serious hardware condition |
18. Interrupt Priority
Multiple interrupts can occur at approximately the same time.
Example:
Keyboard ─────┐ Network ──────┤ Disk ─────────┼──→ CPU Timer ────────┘
The system needs to decide:
Which interrupt should be handled first?
This is interrupt priority.
Example:
High Priority ↓ Critical hardware event ↓ Timer ↓ Network ↓ Keyboard ↓ Low Priority
The exact priorities depend on the architecture and OS.
19. Nested Interrupts
A higher-priority interrupt may sometimes occur while another interrupt handler is executing.
Example:
Normal Program ↓ Low-priority ISR ↓ High-priority Interrupt ↓ High-priority ISR ↓ Low-priority ISR continues ↓ Normal Program
This is called nested interrupt handling.
Whether and how nesting occurs depends on the CPU, interrupt controller, and OS configuration.
20. Interrupt Latency
Interrupt latency is the time between an interrupt being generated and the beginning of its handling.
Simplified:
Interrupt generated ↓ waiting ↓ CPU starts handler
The smaller the latency, the faster the system responds to the event.
This is especially important in:
- real-time systems
- audio processing
- industrial control
- robotics
- embedded systems
21. Interrupt Overhead
Interrupt handling is not free.
The system may need to:
Save state ↓ Switch to handler ↓ Run handler ↓ Restore state
This consumes CPU time.
If thousands or millions of interrupts occur frequently, overhead can become significant.
22. Interrupt Storm
An interrupt storm occurs when a system receives an extremely high rate of interrupts, leaving excessive CPU time spent handling them.
Conceptually:
Interrupt Interrupt Interrupt Interrupt Interrupt Interrupt ↓ CPU overloaded handling interrupts
Possible causes include:
- faulty hardware
- misconfigured devices
- very high packet rates
- driver problems
Systems can use techniques such as interrupt moderation/coalescing for certain high-rate devices.
23. Interrupt Coalescing
For high-speed devices such as network adapters, handling one interrupt for every small event can be inefficient.
Instead, the device may combine multiple events before generating an interrupt.
Without coalescing:
Packet → Interrupt Packet → Interrupt Packet → Interrupt Packet → Interrupt
With coalescing:
Packet ─┐ Packet ─┤ Packet ─┤ → One interrupt Packet ─┘
This reduces interrupt overhead.
24. Polling vs Interrupts
This is an important exam comparison.
Polling
CPU ↓ Check device ↓ Check again ↓ Check again
Interrupt
CPU ↓ Do other work Device ↓ Interrupt ↓ CPU handles event
| Feature | Polling | Interrupt |
|---|---|---|
| CPU checks device | Continuously/repeatedly | No continuous checking |
| CPU efficiency | Lower | Better |
| Response | Depends on polling interval | Event-driven |
| Complexity | Simpler | More complex |
| CPU overhead | Can be high | Usually lower |
| Best for | Simple/highly predictable situations | Event-driven I/O |
25. Interrupts and DMA
Interrupts and DMA are often used together.
Suppose an SSD needs to transfer a large amount of data.
CPU ↓ Configure DMA ↓ DMA transfers data ↓ RAM ↓ Transfer complete ↓ Interrupt ↓ CPU
The CPU doesn't need an interrupt for every byte or word.
Instead:
DMA performs the transfer, and an interrupt can notify the CPU when the transfer is complete.
26. Interrupts and I/O
Typical I/O flow:
Application ↓ I/O Request ↓ Device Driver ↓ Device ↓ Operation ↓ Interrupt ↓ CPU ↓ Interrupt Handler ↓ I/O completion ↓ Application continues
27. Example: Keyboard Interrupt
Let's understand a complete example.
Before key press
CPU → Running application
Key pressed
Keyboard ↓ Keyboard Controller ↓ Interrupt
CPU responds
CPU ↓ Save execution state ↓ Keyboard ISR
ISR
Read keyboard data ↓ Process event ↓ Acknowledge interrupt
Return
Restore state ↓ Application continues
Complete:
Keyboard ↓ Controller ↓ Interrupt ↓ CPU ↓ ISR ↓ Keyboard Driver ↓ OS ↓ Application
28. Example: Disk Read Completion
Suppose an application requests data from an SSD.
Application ↓ read() ↓ OS ↓ Storage driver ↓ SSD
The SSD performs the operation.
If DMA is used:
SSD ↓ DMA ↓ RAM
After completion:
SSD / Controller ↓ Interrupt ↓ CPU ↓ Interrupt Handler ↓ Notify I/O subsystem
The waiting process can then continue.
29. Timer Interrupt
The operating system needs a way to periodically regain control of the CPU.
A hardware timer can generate interrupts:
Timer ↓ Interrupt ↓ CPU ↓ OS scheduler
This is extremely important for multitasking.
For example:
Process P1 running ↓ Timer interrupt ↓ OS scheduler ↓ Switch to P2
Therefore, timer interrupts help the OS implement preemptive scheduling.
30. Interrupts and CPU Scheduling
This is an important connection.
P1 running ↓ Timer interrupt ↓ Kernel ↓ Scheduler ↓ P2 selected ↓ Context switch ↓ P2 runs
So:
Timer interrupts provide opportunities for the OS scheduler to regain control of the CPU.
31. Interrupt vs Exception vs Trap
These terms can be confusing.
Interrupt
Usually refers to an event that causes control to move to the OS/handler, often asynchronously relative to the currently executing instruction stream.
Examples:
Keyboard Network Timer
Exception
A synchronous event caused by the currently executing instruction.
Examples can include:
Divide-by-zero Invalid instruction Page fault Protection fault
Trap
A deliberate synchronous transfer into a handler, often associated with software-generated events or debugging.
A simplified model:
Events │ ├── Interrupt → asynchronous ├── Exception → synchronous fault/event └── Trap → intentional synchronous transfer
Exact terminology varies somewhat by CPU architecture.
32. Interrupt vs Exception
| Feature | Interrupt | Exception |
|---|---|---|
| Timing | Generally asynchronous | Synchronous |
| Cause | External/device event | Current instruction/event |
| Example | Keyboard | Divide-by-zero |
| Relation to current instruction | Usually independent | Caused by current execution |
33. System Calls and Interrupts
Historically, operating systems used software interrupts for system calls on some architectures.
Modern CPUs often provide dedicated system-call instructions.
Conceptually:
Application ↓ System Call ↓ Controlled transition ↓ Kernel
The important point is:
A system call is a controlled request for an OS service; it is not simply the same thing as a hardware interrupt.
34. User Mode and Kernel Mode
Interrupt handling normally transfers execution into privileged kernel code.
User Mode ↓ Interrupt ↓ Kernel Mode ↓ Interrupt Handler ↓ Return ↓ User Mode
This allows the OS to safely manage hardware and system resources.
35. Interrupt Handling Diagram
DEVICE │ │ Interrupt Request ↓ ┌───────────────┐ │ Interrupt │ │ Controller │ └───────┬───────┘ │ ↓ CPU │ Save execution state │ ↓ Identify interrupt │ ↓ Interrupt Vector │ ↓ ISR │ ↓ Handle device/event │ ↓ Restore execution │ ↓ Resume interrupted code
36. Important Interrupt Components
Remember these:
Interrupt │ ├── Interrupt Request ├── Interrupt Controller ├── Interrupt Vector ├── Interrupt Handler / ISR ├── Priority ├── Masking └── Return to interrupted execution
37. Advantages of Interrupts
- Better CPU utilization
- No continuous polling
- Fast response to events
- Efficient I/O
- Supports multitasking
- Enables timer-based scheduling
- Useful for asynchronous hardware events
38. Disadvantages of Interrupts
- Interrupt handling has overhead
- Complex hardware/software coordination
- Too many interrupts can reduce performance
- Nested interrupts can complicate handling
- Poorly designed drivers can cause serious performance problems
39. Interrupts in Modern Systems
Modern computers have multiple CPU cores and sophisticated interrupt controllers.
For example:
Interrupt Controller / | \ ↓ ↓ ↓ CPU 0 CPU 1 CPU 2
Interrupts can often be routed to suitable CPU cores.
This helps with:
- scalability
- multicore performance
- network processing
- load distribution
40. Interrupts in Embedded/IoT Systems
Interrupts are especially important in Arduino/ESP32 and other microcontrollers.
Example:
Button ↓ GPIO Interrupt ↓ CPU ↓ ISR ↓ Set flag / handle event
Instead of continuously checking:
if (digitalRead(buttonPin) == HIGH) { // ... }
an interrupt-based design can react when the GPIO event occurs.
Conceptually:
Button pressed ↓ GPIO interrupt ↓ ISR ↓ Event handled
For real embedded systems, ISRs should generally be short and fast; longer work is often deferred to normal task/thread context.
41. Interrupt vs DMA
| Feature | Interrupt | DMA |
|---|---|---|
| Main purpose | Notify CPU of event | Move data efficiently |
| CPU involvement | Handler required | Mostly setup/completion |
| Best for | Events, small/medium operations | Large data transfers |
| Example | Keyboard event | Disk/network transfer |
| Can work together? | Yes | Yes |
Typical combination:
DMA transfers data ↓ Transfer completes ↓ Interrupt ↓ CPU notified
42. Most Important Exam Questions
Q1. What is an interrupt?
An interrupt is a signal or event that causes the CPU to temporarily suspend its current execution and transfer control to an appropriate interrupt handler to service the event.
Q2. What is ISR?
ISR is a special routine executed by the CPU to handle a particular interrupt.
Q3. What is an interrupt vector?
An interrupt vector identifies or provides the location of the handler associated with a particular interrupt.
Q4. What is an interrupt controller?
It is hardware that manages interrupt requests from devices and helps prioritize and route them to the CPU.
Q5. What is interrupt latency?
The time between an interrupt being generated and the start of its handling.
🧠 Final Revision
Interrupt flow
Event ↓ Interrupt Request ↓ Interrupt Controller ↓ CPU ↓ Save State ↓ Identify Interrupt ↓ Interrupt Vector ↓ ISR ↓ Handle Event ↓ Restore State ↓ Resume Program
Types/concepts to remember
Hardware Interrupt ↓ Generated by hardware Software Interrupt ↓ Generated through software mechanism Maskable Interrupt ↓ Can normally be masked/deferred NMI ↓ High-priority non-maskable event Exception ↓ Synchronous event caused by execution Trap ↓ Intentional synchronous transfer
🧠 Best memory trick
Interrupt = Notify → Save → Identify → Handle → Restore → Resume
And remember the three most important I/O mechanisms:
Polling = CPU asks
Interrupt = Device tells
DMA = Data moves directly between device and memory with minimal CPU involvement