Definition
An interrupt is a signal to the processor emitted by hardware or software that temporarily stops the current program execution and requests the CPU to handle an urgent task. After handling the interrupt, execution returns to the interrupted program. Interrupts are essential for handling asynchronous events like I/O completion, hardware failures, and user input.
Need for Interrupts
Why Interrupts?
- Asynchronous Events: I/O devices finish at unpredictable times - can’t have CPU keep checking
- Immediate Response: Some events need urgent attention (hardware failure, keyboard input)
- Efficiency: Without interrupts, CPU would waste time polling devices
- Responsiveness: System responds quickly to external events
- Power Saving: CPU can sleep instead of continuously polling
Without Interrupts (Polling)
CPU keeps asking: “Is data ready? Is data ready? Is data ready?”
- Very wasteful of CPU cycles
- Slow response (depends on polling frequency)
- CPU can’t do useful work
With Interrupts (Event-driven)
CPU does useful work until device says “I’m done!” Then CPU stops and handles the device
- Efficient use of CPU
- Fast response
- Device-initiated
Types of Interrupts
1. Hardware Interrupts (External)
Source: Hardware devices send signals through interrupt lines
Examples:
- Timer interrupt (clock tick every millisecond)
- Keyboard interrupt (user presses key)
- Disk interrupt (read/write complete)
- Network interrupt (data arrived)
- Power failure warning
- Memory error detected
Characteristics:
- Come from outside CPU
- Can occur anytime
- Can be disabled/enabled by OS
2. Software Interrupts (Internal)
Source: Running program issues interrupt
Examples:
- System call
read(),write(),fork() - Illegal instruction encountered
- Division by zero
- Invalid memory access
- Privileged instruction in user mode
- Page fault (memory not in RAM)
Characteristics:
- Generated by executing instruction
- Predictable (same instruction always interrupts)
- Synchronous (occurs at specific instruction)
3. Exception
Definition: Unexpected condition detected by CPU during instruction execution
Examples:
- Overflow in arithmetic
- Underflow in floating point
- Privilege violation
- Bounds check violation
Interrupt Handling Process
Step 1: Interrupt Occurs
CPU executing instruction → Hardware detects interrupt signal → CPU finishes current instruction
Step 2: Interrupt Detection
- CPU checks interrupt request line at end of each instruction cycle
- If signal present, CPU knows interrupt occurred
- Identifies which device/signal caused interrupt
Step 3: Save Context
OS saves:
- Program Counter (next instruction to execute)
- CPU Registers (current register values)
- Program Status Word (PSW): flags, privilege level
- Stack Pointer
Why save? To resume interrupted program later from exact same point
Step 4: Look Up Handler
Interrupt Vector: Table of addresses of interrupt handlers
Interrupt Vector:
Interrupt 0 → Address of Timer Handler (0x1000)
Interrupt 1 → Address of Keyboard Handler (0x2000)
Interrupt 2 → Address of Disk Handler (0x3000)
Interrupt 3 → Address of Network Handler (0x4000)
- OS looks up which handler to execute based on interrupt number
- Jumps to handler address
Step 5: Execute Interrupt Handler
Interrupt Service Routine (ISR):
- Code that handles the interrupt
- Runs in kernel mode (privileged)
- Example: Keyboard ISR reads key pressed, puts in buffer
Keyboard ISR:
- Read key from keyboard device
- Store in input buffer
- Wake up waiting process (if any)
- Return from interrupt
Step 6: Return from Interrupt
OS restores:
- Program Counter (from saved context)
- CPU Registers (from saved context)
- Program Status Word
- Stack Pointer
Resumed program continues as if never interrupted
Interrupt vs Trap
| Feature | Interrupt | Trap |
|---|---|---|
| Source | Hardware device or clock | Software (instruction) |
| When | Asynchronous (anytime) | Synchronous (at instruction) |
| Caused by | External event | Program itself or CPU |
| Error | Not necessarily | Usually error condition |
| Example | Timer, keyboard, disk | System call, division by 0 |
Interrupt Priorities
What if Multiple Interrupts Occur?
Scenario: While handling keyboard interrupt, disk finishes I/O → What to do?
Priority Levels
OS assigns priority to different interrupt types:
- High Priority: Power failure, hardware errors
- Medium Priority: Disk I/O completion
- Low Priority: Keyboard input, network
Rule: Higher priority interrupt can interrupt lower priority handler
Masking Interrupts
- Disable Interrupts: CPU can turn off interrupt processing
- Enable Interrupts: Turn interrupt processing back on
- Used during critical sections where interruption is dangerous
Interrupt Latency
Definition: Time from when interrupt occurs until handler starts executing
Components of Latency
- Recognition Time: Time to detect interrupt signal
- Acceptance Time: Time for CPU to finish current instruction
- ISR Lookup Time: Time to find handler in vector
- Context Save Time: Time to save registers
- Handler Execution Time: Time to execute ISR
Real-Time Systems
Must have predictable, bounded interrupt latency:
- Must respond within specific time
- Critical for safety systems
- Used in aircraft, medical devices, industrial control
Advantages of Interrupts
- Efficiency: CPU doesn’t waste time polling
- Responsiveness: Immediate response to events
- Asynchronous: Handles unpredictable events
- Multitasking: Multiple devices serviced concurrently
- Power Saving: CPU can sleep until interrupted
- Scalability: Works with many devices
Disadvantages
- Complexity: Interrupt handling adds complexity
- Overhead: Context saving/restoring takes time
- Latency: Unpredictable delays possible
- Debugging: Hard to debug interrupt-driven programs
- Race Conditions: Multiple interrupts can cause synchronization issues
- Stack Overhead: Interrupt handlers use stack space
Real-World Examples
Keyboard Interrupt Flow
- User presses ‘A’ key
- Keyboard sends interrupt signal to CPU
- CPU saves context (PC, registers)
- OS looks up keyboard handler in interrupt vector
- Keyboard ISR executes: reads ‘A’, stores in buffer
- ISR signals waiting process (terminal) that data available
- Returns from interrupt
- CPU restores context
- Interrupted program resumes
- Process reading keyboard wakes up, gets ‘A’
Disk I/O Interrupt Flow
- Process requests disk read
- OS sends command to disk
- Process blocks (goes to waiting state)
- Disk does I/O (millions of CPU cycles later)
- Disk sends interrupt when done
- CPU saves context
- Disk ISR executes: notifies OS data ready
- OS moves reading process to ready queue
- Returns from interrupt
- CPU resumes interrupted program (or runs another process)
- Later, reading process runs and gets data
Summary
Interrupts are fundamental mechanism for handling asynchronous events efficiently. Without interrupts, OS would waste CPU cycles polling devices. With interrupts, system responds quickly to events while keeping CPU productive. Understanding interrupt handling is essential for understanding OS I/O management and real-time systems.