Context switch म्हणजे kernel ने सध्या चालू असलेल्या thread ची CPU state (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
