CSE Roadmap

Core tier · about 3 hours · 8 parts

Part 2 of 8 · 2 min read

Why interrupts

Think of a visitor who can ring a doorbell, or you can walk to the door every minute to see if anyone is there. The doorbell lets you get on with other work until something happens. A device such as a disk, a keyboard or a network card is slow compared with the processor, so the processor should not wait for it.

In plain words: A device can tell the processor that it is ready in two ways. In polling the processor keeps asking. In an interrupt the device sends a signal and the processor stops what it is doing. For rare or slow events, interrupts save much processor time. For a very busy device, such as a network card with 100,000 events a second, polling or batching can be cheaper.

There are two ways to learn that a device is ready.

  • Polling: the processor asks the device again and again whether it has finished.
  • Interrupts: the device sends a signal to the processor when it is done. The processor stops what it is doing for a short time, deals with the event and carries on.

A worked example.

When the device is slow, interrupts waste far less processor time. A worked comparison (made up to show the idea). A keyboard sends 10 events a second. Suppose each poll costs 2 microseconds and each interrupt costs 5 microseconds.

  • Polling every millisecond: 1,000 polls a second × 2 µs = 2,000 µs, which is 0.2 per cent of one second of processor time, whether or not anything happens.
  • Interrupts: 10 events × 5 µs = 50 µs, which is 0.005 per cent.

For a busy device the result flips. Take a network card with 100,000 events a second.

  • Interrupts: 100,000 × 5 µs = 0.5 seconds of every second, so 50 per cent of the processor.
  • Polling every millisecond (and collecting all waiting events each time): still 0.2 per cent for the checking itself.

So interrupts suit rare events, and heavy streams of events sometimes use polling or batches of events per interrupt (Level 8 covers this). The rest of this page is about the interrupt path itself.

Interrupt

An interrupt is a signal that tells the processor an event needs attention. The processor saves its current state, jumps through the interrupt vector to the handler routine for that event, and afterwards restores the state and resumes the interrupted work.

Example: when a disk finishes a read, its controller raises an interrupt so the kernel can pass the data to the program that waited for it.

Check yourself: 1. Why does polling use processor time even when nothing happens? 2. In the example, which method uses less of the processor at 10 events a second? 3. Which method uses less at 100,000 events a second?