4.2.6 · HinglishOperating Systems

Context switch — what gets saved, overhead

2,081 words9 min readRead in English

4.2.6 · Coding › Operating Systems


WHAT is a context switch?

WHY does it exist? Kyunki hum chahte hain ki kai programs ek saath few cores par run hone ka illusion ho. Time-sharing ke liye baar baar ek ko pause karna aur doosre ko resume karna zaroori hai. Bina state save kiye pause possible nahi hai.

WHAT triggers it?

  • Timer interrupt (quantum expire ho gaya → scheduler dwara preemption).
  • Ek process I/O par block ho jaati hai (e.g. read() → so jaati hai, scheduler doosri pick karta hai).
  • Ek higher-priority process runnable ho jaati hai (preemption).
  • Ek system call ya page fault jo CPU yield kar deta hai.
  • 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)

Hum koi number memorize nahi karte — hum ise causes se build karte hain. Overhead = woh time jab CPU program ka kaam nahi kar raha.

Direct cost () aksar ~1 µs hoti hai (hundreds of cycles). Indirect cost 10–100× zyada ho sakti hai kyunki working set ko DRAM se cache hierarchy mein wapas fetch karna padta hai.


Common Mistakes (Steel-manned)


Active Recall

Recall Switch ke dauran exactly kya kahan store hota hai?

PC, general registers, SP, flags, (lazily) FP/SIMD regs, aur ek process switch ke liye page-table base (CR3). Sab process ke PCB / task_struct mein save hota hai.

Recall Indirect cost usually direct cost se zyada kyun hoti hai?

Kyunki resumed process cache aur TLB misses ka storm suffer karta hai (uski working set evict ho chuki thi), har DRAM refill ~100 cycles ka hota hai — ~16 registers copy karne se bahut zyada.

Recall Lost-CPU fraction derive karo aur uske do limits explain karo.

. Jab , (sab overhead). Jab , lekin responsiveness/fairness suffer karta hai.

Recall (Feynman) Ek 12 saal ke bachche ko context switch explain karo.

Socho ek game console hai lekin do doston ke paas alag saved games hain. Players swap karne ke liye tum pehle game ki exact spot (score, position, lives) ek sticky note par likh lo, phir doosre dost ki sticky note load karo taaki unki game exactly wahan se continue ho jahan wo ruke the. Likhna/load karna time waste karta hai — woh hai overhead — aur naye player ko bhi "zone mein wapas aana" padta hai, jo aur bhi cost karta hai.


Flashcards

Context switch kya hota hai?
Running task ki CPU state uske PCB mein save karna aur doosre task ki state restore karna taaki CPU ek alag process/thread run kare.
Kaunse teen types ke events context switch trigger karte hain?
Timer interrupt (quantum expiry/preemption), I/O par block hona, aur ek higher-priority task ka runnable banna (saath mein yield/exit/page fault bhi).
Saved context mein kya store hota hai?
Program counter, general registers, stack pointer, status/flags, lazily FP/SIMD regs, aur process switches ke liye page-table base register (CR3).
Saved context kahan rakha jaata hai?
Process Control Block (PCB) / task_struct mein.
Mode switch vs context switch?
Mode switch privilege level change karta hai (user↔kernel) lekin same process rakhta hai; context switch change karta hai ki kaunsa task run hoga (scheduler + possibly CR3 swap).
Process switch, same-process thread switch se zyada costly kyun hota hai?
Process switch page-table base swap karta hai, TLB entries flush karte hue; threads address space share karte hain isliye CR3/TLB preserved rehta hai.
Usually-dominant overhead kya hai aur kyun?
Indirect cost — resumed process par cache aur TLB cold misses; har DRAM refill ~100 cycles ka, register copy se bahut zyada.
CPU ka switching mein jaane wala fraction ka formula kya hai?
η_lost = T_cs / (q + T_cs), jahan q time quantum hai.
Bahut chhote time quanta kyun bure hote hain?
η_lost = T_cs/(q+T_cs) → jab q chhota hota hai wasted fraction 1 ki taraf badhta hai; CPU overhead par thrash karta hai.
Switch ke end mein iret / return-from-interrupt kya karta hai?
PC aur flags restore karta hai taaki CPU naye loaded process ko user mode mein exactly wahan resume kare jahan wo ruka tha.

Connections

  • Process Control Block (PCB) — woh structure jo saved context hold karta hai.
  • CPU Scheduling — decide karta hai ki switch ke dauran kaun next run karega.
  • Time Quantum and Round Robin set karta hai, switch frequency govern karta hai.
  • TLB and Page Tables — kyun CR3 swap translations flush karta hai.
  • Cache Hierarchy and Locality — indirect overhead ka source.
  • Interrupts and Mode Switches — woh mechanism jo switches initiate karta hai.
  • Threads vs Processes — cost asymmetry explain karta hai.

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