Context switch भनेको kernel ले हाल चलिरहेको thread को CPU state (register, program counter, stack pointer) सुरक्षित गरेर अर्को thread को restore गर्नु हो, ताकि एउटै 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
