OS Notes
Complete guide to hardware and software interrupts — interrupt mechanism, interrupt handling process, interrupt vector table, priority, and interrupt-driven I/O.
Introduction
Imagine you are reading a book (executing a program) and someone taps your shoulder (interrupt). You mark your page (save state), address the interruption (run the handler), then resume reading from where you left off (restore state). This is exactly how CPU interrupts work — they allow external events to get immediate attention without the CPU constantly checking for them.
Interrupts are the nervous system of a computer. They allow hardware devices to signal the CPU that they need attention, the OS to handle errors and system calls, and real-time events to be processed promptly. Without interrupts, the CPU would have to constantly poll every device, wasting enormous amounts of processing power.
Types of Interrupts
Hardware Interrupts (External)
Generated by hardware devices:
- Keyboard key pressed
- Disk read completed
- Network packet arrived
- Timer tick (for scheduling)
- Mouse moved
Software Interrupts (Traps)
Generated by programs intentionally:
- System calls (INT 0x80 on Linux, SYSCALL instruction)
- Breakpoints (for debuggers)
- Division by zero
- Page faults
- Invalid memory access (segfault)
The Interrupt Handling Process
1. Device signals interrupt (raises interrupt line)
2. CPU finishes current instruction
3. CPU saves current state:
- Push PC (program counter) onto stack
- Push processor status register (flags)
- Set CPU to kernel mode
4. CPU looks up handler address in Interrupt Vector Table (IVT)
5. CPU jumps to the Interrupt Service Routine (ISR)
6. ISR executes:
- Save additional registers
- Handle the interrupt (read data, acknowledge device)
- Restore registers
7. ISR executes IRET (return from interrupt)
8. CPU restores saved state (PC, flags)
9. CPU resumes the interrupted program exactly where it left offInterrupt Vector Table (IVT)
A table mapping interrupt numbers to handler addresses:
| Vector # | Handler Address (ISR) |
|---|---|
| 0 | Divide-by-zero handler |
| 1 | Debug exception |
| 6 | Invalid opcode |
| 13 | General protection fault |
| 14 | Page fault handler |
| 32 | Timer interrupt (IRQ 0) |
| 33 | Keyboard interrupt (IRQ 1) |
| 40 | Real-time clock |
| 46 | Primary ATA disk |
| 128 | System call handler (Linux) |
Interrupt Priority and Masking
Not all interrupts are equally urgent. A priority system ensures critical interrupts are handled first:
- Non-Maskable Interrupts (NMI): Cannot be disabled. Reserved for critical errors (hardware failure, memory parity error).
- Maskable Interrupts: Can be temporarily disabled. Most device interrupts fall here. The OS disables them during critical kernel sections to prevent data corruption.
// Disabling interrupts for critical section (kernel code)
unsigned long flags;
local_irq_save(flags); // Disable interrupts, save state
// Critical section — cannot be interrupted
modify_kernel_data_structure();
local_irq_restore(flags); // Restore previous interrupt stateInterrupt-Driven I/O
Instead of the CPU continuously checking if a device is ready (polling), interrupt-driven I/O lets the CPU do other work and be notified when the device is ready:
| Polling (wasteful) | Interrupt-driven (efficient): |
| CPU | Is disk ready? No. CPU: Start disk read. |
| CPU | Is disk ready? No. CPU: Execute other processes... |
| CPU | Is disk ready? No. CPU: Execute other processes... |
| CPU | Is disk ready? No. CPU: Execute other processes... |
| CPU: Is disk ready? YES! Read data. *INTERRUPT* | Disk ready, read data. |
Context of Interrupts in Modern Systems
Modern CPUs handle thousands of interrupts per second. A typical desktop system processes:
- ~1000 timer interrupts/second (for scheduling)
- Variable keyboard/mouse interrupts
- Hundreds to thousands of disk/network interrupts during active I/O
Each interrupt costs ~1-10 microseconds, so at 10,000 interrupts/second, the overhead is ~1-10% of CPU time.
Real-World Analogy
Interrupts are like notifications on your phone. Without them, you would have to manually check every app every few seconds (polling) to see if anything new happened. With interrupts (notifications), you can focus on your current task and be alerted only when something needs attention. Priority levels are like notification settings — calls ring loud (NMI), messages vibrate (normal interrupt), and social media is silenced (masked).
Key Takeaways
- Interrupts allow devices to signal the CPU asynchronously without CPU polling
- Hardware interrupts come from devices; software interrupts (traps) come from programs
- The IVT maps interrupt numbers to handler routine addresses
- Interrupt handling saves state, runs the handler, then restores state transparently
- Priority and masking control which interrupts can preempt which
- Interrupt-driven I/O is fundamental to efficient multitasking — the CPU works while waiting for I/O
- Modern systems handle thousands of interrupts per second with microsecond overhead each
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Interrupts.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Operating Systems topic.
Search Terms
operating-systems, operating systems, operating, systems, input, output, interrupts
Related Operating Systems Topics