4.2.6Operating Systems

Context switch — what gets saved, overhead

2,187 words10 min readdifficulty · medium2 backlinks

WHAT is a context switch?

WHY does it exist? Because we want the illusion of many programs running at once on few cores. Time-sharing requires repeatedly pausing one and resuming another. There is no pause without saving state.

WHAT triggers it?

  • Timer interrupt (quantum expired → preemption by the scheduler).
  • A process blocks on I/O (e.g. read() → goes to sleep, scheduler picks another).
  • A higher-priority process becomes runnable (preemption).
  • A system call or page fault that yields the CPU.
  • Voluntary yield() / process exit.

WHAT gets saved (the context)


HOW the switch happens (step by step)

Figure — Context switch — what gets saved, overhead

WHY there is overhead (derivation of total cost)

We don't memorize a number — we build it from causes. Overhead = time the CPU spends not doing the program's work.

The direct cost (Tsave+Tsched+TrestoreT_{save}+T_{sched}+T_{restore}) is often ~1 µs (hundreds of cycles). The indirect cost can be 10–100× larger because the working set must be re-fetched from DRAM into the cache hierarchy.


Common Mistakes (Steel-manned)


Active Recall

Recall What exactly is stored where during a switch?

PC, general registers, SP, flags, (lazily) FP/SIMD regs, and for a process switch the page-table base (CR3). All saved into the process's PCB / task_struct.

Recall Why is the indirect cost usually larger than the direct cost?

Because the resumed process suffers a storm of cache and TLB misses (its working set was evicted), each costing ~100 cycles to refill from DRAM — far more than copying ~16 registers.

Recall Derive the lost-CPU fraction and explain its two limits.

ηlost=Tcs/(q+Tcs)\eta_{lost}=T_{cs}/(q+T_{cs}). As q0q\to 0, η1\eta\to 1 (all overhead). As qq\to\infty, η0\eta\to 0 but responsiveness/fairness suffer.

Recall (Feynman) Explain a context switch to a 12-year-old.

Imagine one game console but two friends with different saved games. To swap players you write down the exact spot of the first game (score, position, lives) on a sticky note, then load the second friend's sticky note so their game continues exactly where they left off. The writing/loading wastes time — that's the overhead — and the new player also has to "get back in the zone," which costs even more.


Flashcards

What is a context switch?
Saving the CPU state of the running task into its PCB and restoring another task's state so the CPU runs a different process/thread.
What three classes of events trigger a context switch?
Timer interrupt (quantum expiry/preemption), blocking on I/O, and a higher-priority task becoming runnable (also yield/exit/page fault).
What is stored in the saved context?
Program counter, general registers, stack pointer, status/flags, lazily FP/SIMD regs, and for process switches the page-table base register (CR3).
Where is the saved context kept?
In the Process Control Block (PCB) / task_struct.
Mode switch vs context switch?
Mode switch changes privilege level (user↔kernel) but keeps the same process; context switch changes which task runs (scheduler + possibly CR3 swap).
Why is a process switch costlier than a same-process thread switch?
Process switch swaps the page-table base, flushing TLB entries; threads share the address space so CR3/TLB are preserved.
What is the usually-dominant overhead and why?
Indirect cost — cache and TLB cold misses on the resumed process; each DRAM refill ~100 cycles, far exceeding the register copy.
Formula for fraction of CPU lost to switching?
η_lost = T_cs / (q + T_cs), where q is the time quantum.
Why are extremely small time quanta bad?
η_lost = T_cs/(q+T_cs) → as q shrinks the wasted fraction grows toward 1; the CPU thrashes on overhead.
What does iret / return-from-interrupt do at the end of a switch?
Restores PC and flags so the CPU resumes the newly loaded process in user mode exactly where it paused.

Connections

  • Process Control Block (PCB) — the structure holding saved context.
  • CPU Scheduling — decides who runs next during the switch.
  • Time Quantum and Round Robin — sets qq, governing switch frequency.
  • TLB and Page Tables — why CR3 swap flushes translations.
  • Cache Hierarchy and Locality — source of indirect overhead.
  • Interrupts and Mode Switches — the mechanism that initiates switches.
  • Threads vs Processes — explains cost asymmetry.

Concept Map

requires

triggers

triggers

causes

saves

includes

includes for process

stored in

shares address space

swaps

causes

makes costly

costs

Context Switch

Time-sharing illusion

Triggers

Timer interrupt

I/O block or syscall

Process Control Block

Saved CPU state

PC and registers and SP and flags

Page-table base CR3

Thread switch

Process switch

TLB flush

Pure overhead

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Dekho, CPU ke paas ek hi set of registers hote hain, lekin OS ko bahut saare processes ko ek saath chalwaana hota hai. Toh jab ek process ko rokna ho aur doosre ko chalana ho, OS uss process ki poori "state" — Program Counter, general registers, stack pointer, flags, aur process switch mein page-table base (CR3) — sab kuch uske PCB (task_struct) mein save kar deta hai. Phir doosri process ka saved state load karke usko wahin se resume kar deta hai jahan woh ruki thi. Isi process ko context switch kehte hain. Sticky-note wali example yaad rakho: ek game ka exact spot likho, doosre ka note load karo.

Ab overhead ki baat. Register copy karna toh fast hai — asli kharcha chhupa hua hota hai. Jab nayi process chalti hai, uska data aur code cache mein nahi hota (purani process ne evict kar diya), aur TLB bhi flush ho gaya hota hai (CR3 change ki wajah se). Toh shuru mein bahut saare cache misses aur TLB misses hote hain, har miss ~100 cycles ka. Yahi indirect cost direct cost se 10-100 guna zyada ho sakta hai. Isliye ye mat sochna ki context switch ka matlab sirf register save karna hai.

Ek important formula: ηlost=Tcs/(q+Tcs)\eta_{lost} = T_{cs}/(q + T_{cs}). Matlab agar time quantum qq bahut chhota rakhoge, toh switching bar-bar hoga aur CPU ka bada hissa waste ho jayega. Agar qq bahut bada rakhoge, system slow/unfair feel hoga. Isliye OS ek balanced quantum (jaise 1-10 ms) choose karta hai. Exam mein yaad rakho: mode switch ≠ context switch — mode switch mein same process rehti hai, context switch mein task hi badalta hai.

Test yourself — Operating Systems

Connections