A1.3.4 — Polling and Interrupt Handling

← Back to A1.3 overview

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

  1. Device signals the CPU via an interrupt signal (NIC, keyboard, mouse, etc.).
  2. CPU saves its current state (context save).
  3. CPU executes the Interrupt Service Routine (ISR) — a specific routine that handles that type of interrupt.
  4. 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

FactorFor Interruptfor Polling
Event frequencyHigh-frequency or unpredictableLow-frequency, predictable
CPU overheadLower overall — CPU free between eventsHigher — CPU always busy checking
Power sourceBattery devices benefit (sleep mode possible)Mains-powered devices can afford polling
LatencyLower and immediate responseVariable — depends on polling interval
SecurityHarder to exploit for DoS attacksCan be overwhelmed if event rate is high
Real-time systemsPreferred — responds immediately to eventsLess suitable — can miss time-critical events

Interrupt Service Routine (ISR)

ISR Sequence

  1. Interrupt signal received a device sends a signal to the CPU.
  2. CPU saves its state current register values, program counter, and context are saved so the interrupted task can resume later.
  3. ISR executes the specific routine assigned to that interrupt type runs and handles the event.
  4. State restored the CPU reloads the saved state.
  5. 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.