A1.3.4 — Polling and Interrupt Handling
Two Methods for Handling Device Events
Polling
The CPU continuously and repeatedly checks whether a device needs attention ( asking "do you have data for me?" in a loop).
- Simple to implement.
- Wastes CPU cycles constantly checking devices even when nothing is happening.
- controlled, predictable latency.
Interrupt
A device sends a signal to the CPU when it needs attention. The CPU stops its current task, handles the interrupt, then resumes.
- Reduces waiting time.
- More efficient than polling, CPU doesnt have to work in the loop.
- Introduces overhead: the CPU must save its current job and restore it after.
- high-frequency or unpredictable events.
How an Interrupt Works
- Device signals the CPU via an interrupt signal (NIC, keyboard, mouse, etc.).
- CPU saves its current state (context save).
- CPU executes the Interrupt Service Routine (ISR) — a specific routine that handles that type of interrupt.
- After the ISR finishes, CPU restores its previous state and resumes its prior task.
Mouse movement as an example
- Physical movement detected by mouse sensor → electronic signal generated.
- Data packet (direction + distance) sent to computer via USB or wirelessly.
- Interface card generates an interrupt signal to the CPU.
- CPU executes the ISR → reads and interprets mouse data.
- OS calculates new cursor position, sends instructions to GPU.
- GPU redraws cursor on screen.
- CPU resumes previous tasks.
This happens so fast it appears instantaneous. Modern mice report at 125–1000+ Hz.
Use cases
| Factor | For Interrupt | for Polling |
|---|---|---|
| Event frequency | High-frequency or unpredictable | Low-frequency, predictable |
| CPU overhead | Lower overall — CPU free between events | Higher — CPU always busy checking |
| Power source | Battery devices benefit (sleep mode possible) | Mains-powered devices can afford polling |
| Latency | Lower and immediate response | Variable — depends on polling interval |
| Security | Harder to exploit for DoS attacks | Can be overwhelmed if event rate is high |
| Real-time systems | Preferred — responds immediately to events | Less suitable — can miss time-critical events |
Interrupt Service Routine (ISR)
ISR Sequence
- Interrupt signal received a device sends a signal to the CPU.
- CPU saves its state current register values, program counter, and context are saved so the interrupted task can resume later.
- ISR executes the specific routine assigned to that interrupt type runs and handles the event.
- State restored the CPU reloads the saved state.
- Resume previous task the CPU continues exactly where it left off.
Why this matters: Without saving state, the CPU would lose track of its current task. The ISR makes interrupts invisible to running programs.