ایک context switch یہ ہے کہ kernel اس وقت چل رہے thread کی CPU حالت (registers، program counter، stack pointer) کو محفوظ کرتا ہے اور کسی دوسرے thread کی بحال کرتا ہے، تاکہ ایک ہی core بہت سے threads کو multiplex کر سکے۔ یہ ہر scheduler preemption، blocking syscall، یا interrupt پر ہوتا ہے — اور یہ مفت نہیں ہے۔
لاگت کہاں چھپی ہوتی ہے
DIRECT cost (visible, ~1–5 µs):
save/restore registers · run scheduler logic · switch kernel stacks
if PROCESS switch: also swap page tables (CR3) → FLUSHES the TLB
INDIRECT cost (often much larger):
cold CPU CACHES → the new thread's data isn't in L1/L2/L3 → cache misses to RAM
cold TLB → address translations must be re-walked
lost pipeline / branch-predictor state
