serversim
LearnSimulatorTemplatesPricing
…
…
Open menu
Learn↗Simulator↗Templates↗Pricing↗
My DocumentsDocs
Contents/Chapter 66 / 16

When does the CPU rest — interrupts and context switches

While one program waits on the disk, does the CPU rest or go do something else? Changing who sits at the counter costs a moment: write down where you left off, then fetch the next person's papers.

See this first in FoundationsCan a computer do something else while it waits?→

Two common questions, answered here: "When code hits IO, does the CPU rest or go do something else?" and "With one core, how is work split up — and when does the CPU actually rest?" Short answer: it almost never rests. Follow program A from the moment it asks for a file, through B and C running, the CPU sleeping, and A resuming.

① The CPU has two modes

Your code runs in user mode, where it cannot command devices or touch other programs' memory. For that it must ask the OS — a system call — which switches the CPU into kernel mode to run OS code. Note that the CPU is not resting here; it is simply running different code.

② What "context" is

A running program's current state lives in a few CPU registers: how far it got (PC), values mid-computation, the stack position. That bundle is its context. A CPU holds one context at a time, so running someone else means writing this one down and loading another. Save + restore = a context switch — the amber and blue slots in the animation.

③ Two ways to take the CPU away

Voluntarily: a program that must wait calls into the kernel and drops to Waiting. Forcibly: a hardware timer interrupts every few milliseconds and hands control back to the kernel, so a program that never yields still gets pushed aside. That is preemption.

④ Interrupts — how devices call the CPU

When the disk finishes it raises an interrupt; the CPU stops what it is doing, jumps to kernel handler code, and the waiting program moves from Waiting to Ready. The CPU never sits watching the disk — the device reports back.

⑤ So when does it truly rest?

Only when nothing is Ready. Then the kernel runs the idle task, which uses an instruction like HLT to drop the CPU into a low-power state until the next interrupt. That is why laptops last and why a server can show 5% CPU. The hatched band in the timeline is that real rest.

Step 1/13Speed
CPU is now runningProgram APC · position A · line 12
CPU timeline (color change = a switch)
Program AProgram BProgram Ckernel (OS)idle
Program ARunning
saved context
held by the CPU
Program Bwaiting for CPU
saved context
B · line 7
Program Cwaiting for CPU
saved context
C · line 3

The CPU is running program A. Its registers hold A's PC and values — a CPU holds exactly one program's context at a time. B and C wait with their contexts parked in storage.

Try it in the simulator

  1. 1.Select WebFlux in the 'reactive stack' preset and find ioWorkerCount. This preset contains WebFlux and PostgreSQL; it has no Tomcat comparison node.
  2. 2.Keep the load and seed fixed. Run for a chosen duration and record CPU, throughput and latency. Change only ioWorkerCount, reset the simulation and run for the same duration.
  3. 3.This varies the number of event-loop workers; it does not isolate context-switch cost. Use the scheduler illustration above to follow why switches happen and what they cost.
Try it in the simulator →

Try it — three programs, one CPU

Now run it for real. Below are three programs with different personalities: A keeps calling a function, B does nothing but heavy computation, and C is a server that queries a database. They share a single CPU. The green border marks whoever currently holds it, and each PC marker shows how far that program has run. Watch this in particular — while A calls tax() and returns, the CPU never moves to another program (a call is just a jump). C, by contrast, yields at db.query(), and B never yields so it gets dragged out when its time slice expires. The log below names the reason every time. Change the time slice and watch switches — and the kernel's share — rise or fall.

SpeedTime slice
CPU timeline (color change = a switch)switches 0 · kernel share 0% · idle share 0%
kernel (OS)idle
Why it switched — recent events
  • —

Terms in this chapter

user / kernel mode
The CPU's two privilege levels: your code vs the operating system.
system call
The doorway for asking the OS to do something only it can do.
context
PC and registers — everything needed to resume execution.
context switch
Saving one context and restoring another. Never free.
interrupt
A signal from a device or timer that makes the CPU jump into kernel code.
preemption
Forcibly reclaiming the CPU from code that will not yield.
time slice
How long a flow may hold the CPU before being preempted — usually milliseconds.
idle task
What runs when nothing else can; it puts the CPU to sleep.

Why it evolved this way

If scheduling relied on goodwill alone, one infinite loop could freeze the whole machine — early systems worked exactly that way. Timer-based preemption is what made one runaway program survivable. Keep this in mind: the event loop, later, does not have it.

The CPU does not rest while waiting — it goes elsewhere through the kernel. It rests only when nothing is Ready.

The remaining problem

But processes were expensive to create and switch (ch.4). Is there a lighter way to run several flows inside one program? →

← Previous

Chapter 5. Processes — the first way to share a CPU

Next →

Chapter 7. Discovering the wait — IO idles the CPU