Interrupt handling and polling
In one sentence: Operating systems coordinate peripheral events using either event-driven interrupt handling (best for unpredictable, sporadic, or battery-constrained workloads) or periodic polling (often suited to regular, predictable, deterministic intervals).
What you will learn
By the end of this lesson, you should be able to:
- Explain how interrupt handling (signals and interrupt service routines) and polling (periodic status checks) differ operationally.
- Evaluate both approaches across event frequency, CPU processing overheads, power source, event predictability, controlled latency, and security concerns.
- Justify choosing interrupts or polling across keyboard and mouse inputs, network communications, disk I/O operations, embedded systems, and real-time systems.
Before you begin
Review The fetch-decode-execute cycle (A1.1.5), Functions of an operating system (A1.3.2 device management), and Approaches for scheduling (A1.3.3 context switching). Both interrupts and polling connect hardware devices (I/O) back to CPU attention.
Key words
| Term | Simple meaning |
|---|---|
| Interrupt | An asynchronous signal sent by hardware (or software) alerting the CPU that an event needs immediate service. |
| Interrupt service routine (ISR) | A dedicated OS/driver handler function that runs automatically when an interrupt arrives, servicing the event before returning control to the paused program. |
| Polling | A synchronous loop in which the CPU repeatedly checks device status registers on a schedule to see if service is required. |
| Latency | The time delay between an event occurring (such as a keypress or sensor trip) and the system responding to it. |
| Context switching overhead | CPU cycles spent saving current registers/state and restoring them around an interrupt or task switch instead of running user code. |
The idea, step by step
1. How interrupt handling works
In an interrupt-driven system, peripheral devices signal the CPU only when they actually have data or an error to report:
- Event occurs: A device event takes place (for example, a key is pressed, a network packet finishes arriving, or a collision sensor fires).
- Signal sent: The device asserts an electrical interrupt signal to the CPU.
- Routine triggered: At the end of the current fetch-decode-execute step, the CPU pauses its active program, saves the current register and program-counter state, and jumps to the matching interrupt service routine (ISR).
- Control restored: After the ISR processes the event, the CPU restores the saved state and resumes the interrupted program cleanly.
Think of an interrupt like waiting for a doorbell while reading: you focus on reading (or enter low-power rest) and pause only when the bell rings.
2. How polling works
In a polling system, peripherals stay passive while the CPU drives every check:
- Scheduled check: At set intervals (or in a dedicated check loop), the CPU queries each attached device's status flag one by one.
- Conditional service: If a flag indicates pending data or action, the CPU services it immediately in line.
- Continue loop: If the device has nothing new, the CPU moves on and checks again at the next interval.
Polling is simple to program without complex interrupt controller hardware. It works like opening your front door every five minutes to see if a parcel arrived, even when nothing is there.
3. Evaluating interrupts vs. polling across six engineering criteria
Neither technique is universally superior; engineers weigh six key criteria against the workload:
| Evaluation criterion | Interrupt handling | Polling |
|---|---|---|
| Event frequency | Highly efficient for infrequent or sporadic events because CPU cycles are spent only when an event happens. | More sensible for regular, steady, or high-frequency periodic events where checking at a set cadence matches incoming work. |
| CPU processing overheads | Very low overhead during quiet periods; however, extremely high-frequency interrupt storms raise context-switching overhead sharply. | High baseline overhead in idle periods because repeated status queries burn CPU cycles even when zero events occur. |
| Power source (battery vs. mains) | More power-efficient for battery devices (laptops, smartphones, wearables) because CPU cores stay in low-power idle states between events. | Less power-efficient because continual active status checking prevents deep CPU sleep states; usually better suited to mains-powered equipment. |
| Event predictability | Ideal for unpredictable, asynchronous events, responding immediately regardless of when they strike. | Suited to predictable, fixed-schedule events checked at predetermined intervals. |
| Controlled latency | Fast response when bus/interrupt traffic is light, but bursty concurrent interrupts can create variable latency. | Delivers controlled, deterministic latency at known bounded intervals, avoiding surprise preemption jitter. |
| Security concerns | Lets security and fault monitors react immediately to critical threats, shrinking windows for malicious interference (though unthrottled interrupt floods risk denial-of-service). | Long polling intervals can delay detection of critical alerts or intrusion events, widening exposure between checks. |
4. Evaluating real-world scenarios (why context decides)
The table below applies those criteria across five standard computing environments, showing how both approaches fit different cases:
| Domain | When interrupt handling fits better | When polling can fit better |
|---|---|---|
| Keyboard and mouse inputs | Everyday PCs, laptops, and tablets: human typing and clicking are unpredictable and slow compared with CPU speed, so interrupts give instant visual feedback without draining battery on empty loops. | Simple low-activity public kiosks or basic keypad terminals that are mostly idle and can poll cheaply on a slow schedule without complex interrupt wiring. |
| Network communications | Video calls, gaming, smartphones, and busy servers: arriving network packets are bursty and urgent, so interrupts pass data to applications promptly and save mobile battery life while avoiding packet drops. | Remote monitoring nodes sending small, predictable status updates on a fixed timer where interrupt complexity is unnecessary. |
| Disk I/O operations | General-purpose OS file reads and writes: drives are orders of magnitude slower than CPUs, so interrupts let the OS run other threads instead of spinning while waiting for disk completion. | Fixed-schedule data loggers that write a sample only once per timer tick and simply poll drive readiness right before writing. |
| Embedded systems | Safety-critical triggers such as automotive airbag deployment, where a collision spike is unpredictable and milliseconds determine occupant survival. | Building climate control (HVAC) checking temperature sensors on steady minute-by-minute cycles where minor changes happen slowly and predictably. |
| Real-time systems | Event-critical alarms and emergency shutdowns requiring instant sub-millisecond priority interruption regardless of current CPU tasks. | Industrial robotic arms performing repetitive sensor and actuator updates at exact fixed cycles (for example, every 5 ms), where polling guarantees predictable controlled latency without unpredictable interrupt context switches. |
Worked example
Compare two real-world engineering designs by applying all six criteria:
- Battery-powered wearable medical alert button and heart monitor:
- Workload: Emergency button presses and irregular heart alerts are rare and unpredictable; battery life must last days on a small cell; alert security and fast response cannot wait for a long timer.
- Evaluation: Interrupt handling fits best: the microcontroller sleeps at micro-watt power with zero polling CPU overhead until an alert edge fires an interrupt, triggering the ISR immediately.
- Mains-powered factory optical bottle-cap inspector:
- Workload: Bottles pass the sensor steadily every
10 msalong a synchronized conveyor powered by factory mains electricity; the controller must sample the camera gate and fire a pneumatic ejector at exact, predictable10 msintervals (controlled latency). - Evaluation: Polling on a deterministic hardware timer fits well: power draw on mains is negligible, events are predictable, checking at the fixed cadence avoids interrupt context-switching jitter, and actuator timing remains strictly deterministic.
- Workload: Bottles pass the sensor steadily every
Check your understanding
- What happens inside the CPU between an external device sending an interrupt signal and the original program resuming?
- Why is interrupt handling usually better than polling for a battery-powered smartphone keyboard and Wi-Fi chip?
- Under what condition can interrupt handling suffer higher CPU processing overhead than expected?
- Give one real-time system where interrupt handling is necessary and one real-time system where polling is preferred, justifying both choices.
Common mix-up
It is easy to think: Interrupt handling is always faster and better than polling in every real-time or industrial system.
Instead, remember: Context matters. If events arrive on a strict, predictable clock cycle (such as an industrial robot loop) or flood in at high frequency, polling provides predictable controlled latency and avoids costly context-switching bursts. Reserve interrupts for unpredictable, sporadic, battery-sensitive, or immediate-emergency events.
Quick recap
- Interrupt handling: Triggers an ISR only when an event occurs; ideal for unpredictable, sporadic, and battery-powered workloads, though vulnerable to context-switch overhead under very high interrupt rates.
- Polling: Checks device status on fixed schedules; wasteful during idle periods and harder on batteries, but simple and provides deterministic controlled latency for predictable periodic tasks.
- Six evaluation criteria: Always weigh event frequency, CPU overhead, power source, event predictability, controlled latency, and security concerns against the target device (
keyboard/mouse,network,disk I/O,embedded, orreal-time).
Textbook reading
- Computer Science (Baumgarten, Ganea, Turland, 2025), A1 Computer fundamentals > A1.3 Operating systems and control systems > A1.3.4 Interrupt handling and polling, Book pp. 85–88 (PDF pp. 95–98).
- Read these pages to review the textbook comparison boxes and worked scenario notes; this original lesson does not replace the textbook.
Sources and further learning
- IB Computer Science Guide (first assessment 2027), A1.3.4: Evaluate the use of polling and interrupt handling (Guide p. 30; PDF p. 36).
- Computer Science (Baumgarten, Ganea, Turland, 2025), A1.3.4 Interrupt handling and polling, Book pp. 85–88 (PDF pp. 95–98). All evaluation tables and worked comparisons above are original.