Un context switch es cuando el kernel guarda el estado de CPU del thread en ejecución (registros, program counter, stack pointer) y restaura el de otro thread, de modo que un solo core pueda multiplexar muchos threads. Ocurre en cada preemption del scheduler, cada syscall bloqueante, o cada interrupt — y no es gratis.
Dónde se esconde el coste
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
