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).
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+Trestore) 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.
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). As q→0, η→1 (all overhead). As q→∞,
η→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.
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). Matlab agar time quantum q bahut chhota
rakhoge, toh switching bar-bar hoga aur CPU ka bada hissa waste ho jayega. Agar q 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.