ਇੱਕ 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
