Loading…
Loading…
Charting your lesson…
Opening your lesson…
Context Switching and the Dispatcher
Lesson 4 of 4
6 steps•about 12 min•11 flashcards
Step 1/ 61 min
Every time the CPU changes which process it runs, it performs a context switch — the invisible machinery that makes multitasking possible. It is pure overhead (no user work happens during it), so understanding its cost is key to reasoning about scheduler performance.
Core idea: a context switch is the act of saving the state of the currently running process into its PCB and loading the saved state of the next process from its PCB, so the CPU can switch execution. The dispatcher is the OS module that performs this switch and hands the CPU to the process the scheduler selected.
Step 2/ 61 min
A process's context is everything the CPU needs to resume it: the program counter, CPU registers, stack pointer, and memory-management state (page-table base). Switching means: freeze process A's context into A's PCB, thaw process B's context from B's PCB.
These are distinct roles often confused:
A context switch is triggered by:
Running(A) --interrupt--> kernel saves A's regs -> PCB[A]
-> scheduler picks B
-> dispatcher loads PCB[B]'s regs, switches address space
-> Running(B)
Cost is not just the register copy: switching address spaces flushes the TLB and pollutes the CPU cache, so the new process starts with cold caches. This indirect cost usually dwarfs the direct save/restore.
Step 3/ 61 min
Suppose a context switch costs 10 µs and a Round-Robin quantum is 100 µs. Then each 100 µs of user work carries 10 µs of switching → about 9% overhead (10 / 110). Halve the quantum to 50 µs for better responsiveness and overhead jumps to ~17% (10 / 60); the CPU now spends a sixth of its time just switching. This is the concrete trade-off behind "small quantum = responsive but wasteful" — the switch cost is fixed, so shorter slices amortise it over less useful work.
Step 4/ 61 min
Common asks: "what happens during a context switch", "difference between scheduler and dispatcher", "what is dispatch latency", and "why is a context switch expensive?" (TLB flush + cache pollution, not just register saves). A frequent point: during a context switch no useful work is done — it is pure overhead.
Step 5/ 61 min
Treating the scheduler and dispatcher as the same thing. The scheduler is the decision-maker (applies the scheduling algorithm to pick the next process); the dispatcher is the doer (executes the context switch and transfers control). Exams specifically test this separation and the term "dispatch latency" for the dispatcher's overhead.
Step 6/ 6· Worked examples2 min
Problem
A round-robin scheduler uses a time quantum of 4 ms. Each context switch costs 1 ms of pure overhead (no useful work). (a) What fraction of CPU time is wasted on switching? (b) If the quantum is dropped to 1 ms to improve responsiveness, what is the new overhead fraction, and what is the trade-off? (c) Name what the dispatcher actually does during a switch.
Try it on paper first.
Recall
Keep this
Context switching ties process states, the PCB, and scheduling together — interviewers ask "what happens during a context switch?" and "scheduler vs dispatcher?" to test whether you understand the mechanics behind multitasking.
A context switch is the OS saving the state of the current process (into its PCB) and loading the saved state of the next process, so the CPU can switch execution. The saved state includes the program counter, CPU registers, and memory-management info.
It is pure overhead — no useful work happens during the switch. Triggers: timer interrupt (preemption), I/O block, or a higher-priority process arriving.
Dispatch latency is the time the dispatcher takes to stop one process and start another.
| Scheduler | Dispatcher | |
|---|---|---|
| Role | decides who runs (policy) | performs the switch (mechanism) |
| Output | selects a process | gives it the CPU |
| Metric | scheduling criteria | dispatch latency |
"What happens during a context switch?", "scheduler vs dispatcher", "what is dispatch latency?", "is a context switch useful work?", and "what triggers a context switch?".
A context switch saves the running process's state to its PCB and restores the next process's — pure overhead triggered by interrupts, I/O, or preemption. The scheduler decides who runs (policy); the dispatcher performs the switch (mechanism), and dispatch latency measures how long that takes.
You've read 0 of 6 steps
Now lock it in — questions straight after reading are what make it stick.