What a context switch saves
To pause a process mid-instruction-stream and resume it later as if nothing happened, the OS must save its context: every CPU register, the program counter (hello, lesson 1-2), and memory-management state. Restoring another process's context reloads all of that.
Each switch costs on the order of microseconds, plus a hidden tax: the CPU's fast caches are full of the old process's data, so the new one starts slow. Thousands of unnecessary switches per second add up. This is why locks queueing threads (lesson 5-3) and having far more busy threads than cores can actually make software slower.
Rule of thumb: turn-taking is cheap enough to feel free, but never actually free.
The scheduler's best trick: skipping the waiters
Recall the process states from lesson 3-1: running, ready, waiting. The scheduler only gives slices to ready processes. A process that is waiting, for a disk read, a network reply, or a time.sleep, costs nearly zero CPU.
This is the difference between:
- CPU-bound work: actually computing (a busy loop, video encoding). Uses its whole slice.
- I/O-bound work: mostly waiting (web servers, chat apps). Gives up the CPU voluntarily by blocking, and the scheduler runs someone else.
time.sleep(0.2) does not spin the CPU for 0.2 seconds. It tells the OS "wake me in 0.2s" and the process leaves the ready queue entirely. Run the comparison.
Code exercise · python
Run this. The busy loop makes the CPU do a million additions. The sleep makes the CPU do nothing at all for 0.2 seconds, the OS simply does not schedule us until the timer fires.
Code exercise · python
Run this to measure the claim directly. time.perf_counter() measures wall-clock time (what a stopwatch would show), while time.process_time() counts only the time the CPU actually spent running THIS process. A 0.3-second sleep passes on the stopwatch but costs almost zero CPU time, because the scheduler simply did not run us.
Quiz
A web server thread spends 95% of its life waiting for database replies. While it waits, what does that thread cost the CPU?
Problem
Saving one process's registers and program counter, then loading another's so it can resume, has a two-word name. What is it?