The full picture
🎬 What a context switch actually does
1 / 5Process A is running on the CPU: its program counter, registers and stack pointer live in CPU hardware. Process B sits in the ready queue, its state parked in its PCB (process control block).
- Thread switch within a process keeps the address space — cheaper (no TLB flush; tagged TLBs/ASIDs reduce even cross-process cost on modern CPUs).
- Mode switch ≠ context switch: a system call enters the kernel and returns to the same thread — much cheaper. Interviewers love this distinction.
- Voluntary vs involuntary: blocking on I/O (voluntary) vs preemption (involuntary). High involuntary switch rates in
pidstat -wmean too many runnable threads competing — the classic oversubscribed-thread-pool signature. - This cost is precisely what virtual threads and event loops avoid: switching between virtual threads or event-loop tasks happens in user space in nanoseconds, no kernel entry, no TLB damage.