ఒక context switch అనేది kernel ప్రస్తుతం నడుస్తున్న thread యొక్క CPU state (registers, program counter, stack pointer)ను సేవ్ చేసి, మరొక thread దాన్ని పునరుద్ధరించడం, తద్వారా ఒకే core అనేక threadలను 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
