Un context switch (changement de contexte) est l'action du kernel de sauvegarder l'état CPU du thread en cours d'exécution (registres, program counter, stack pointer) et de restaurer celui d'un autre thread, afin qu'un seul cœur puisse multiplexer de nombreux threads. Il se produit à chaque préemption du scheduler, à chaque syscall bloquant, ou à chaque interruption — et ce n'est pas gratuit.
Où se cache le coût
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
