CSE Roadmap

Core tier · about 3 hours · 8 parts

Part 7 of 8 · 3 min read

The three doors

Putting it together

Think of a lighthouse keeper who sleeps until a bell rings, a ship signals, or the clock says that it is time to check the lamp. The kernel wakes for three reasons in the same way. The analogy stops in one place: the keeper may choose to stay in bed, and the hardware forces the kernel to run on a timer.

In plain words: The kernel runs only in reaction to one of three events: a system call, a trap or an interrupt. Between them the processor runs user programs. A file read shows all three kinds of event: the program makes a system call, the kernel starts the disk, the program waits, and a device interrupt later tells the kernel that the data has arrived.

The kernel does not run all the time. It runs only when one of three things happens:

  1. A program makes a system call (a trap on purpose).
  2. A program causes an exception (a trap by mistake or need).
  3. A device or the timer raises an interrupt.

Everything the OS does is a reaction to one of these. A program does a loop of calculations, and the kernel is idle until an event occurs. This is also why the timer matters: it guarantees an event at least every few milliseconds, so the kernel can always take control.

A worked example.

A closing worked example. A program reads a file. It makes the read system call (door 1). The kernel starts a disk read and puts the program to sleep, then runs another program (page 1, multiprogramming). Some milliseconds later, the disk raises an interrupt (door 3). The handler copies the data, marks the first program as ready, and the scheduler later lets it run again. If a different program divides by zero meanwhile, that is door 2. One read used two of the three doors.

Interview angle

Answer in your head first, then open it.

Interrupt versus trap?

An interrupt comes from outside the running instruction, from a device or timer, at any moment. A trap comes from the program's own instruction, such as a system call or a fault.

What happens on an interrupt?

The processor finishes the instruction, looks up the handler in the interrupt vector, saves state, switches to kernel mode, runs the handler, and returns.

Why mask interrupts?

To protect critical code that a handler could corrupt, for a very short time.

How does the OS regain control from a program in a loop?

A timer interrupt.

Check yourself: 1. Name the three events that give the kernel control. 2. Which door does a disk completion use? 3. Why does the kernel always get a chance to run, even if programs never call it?

Words you will meet

Word Meaning
Polling Repeatedly asking a device whether it is ready
Handler A routine that runs in response to an event
Interrupt request line The wire a device uses to signal the processor
Synchronous Caused by the instruction being run
Asynchronous Arriving independently of the instruction being run
Signal A software notification delivered to a process
Page fault An exception raised when a program touches memory that is not currently loaded

Quick notes

  • An interrupt tells the processor that a device needs attention. The processor saves its state, looks up the handler in the interrupt vector, and returns when done.
  • Interrupts suit rare events. Polling or batching can suit very busy devices.
  • Maskable interrupts can be delayed in critical sections. Nonmaskable ones cannot.
  • An interrupt is asynchronous. A trap is synchronous and is caused by the running program.
  • A signal is how a program learns that it caused a fault.
  • The kernel runs only after a system call, a trap or an interrupt.