Quiz Entry - updated: 2026.07.14
What exactly gets saved and restored during a context switch?
Everything needed to resume the process exactly where it left off: the PC, stack pointer, general registers, flags, and FPU/vector state.
All of this is stashed in the kernel's process control block and reloaded later, which is why a switch is costly — and why threads, which share an address space, are cheaper to switch between.
Context = complete CPU snapshot:
| Saved State | Purpose |
|---|---|
| Program Counter (PC/RIP) | Where to resume execution |
| Stack Pointer (SP/RSP) | Top of process's stack |
| General Registers | rax, rbx, rcx... (all working data) |
| Flags Register | Condition codes (zero, carry, overflow) |
| Floating Point State | FPU/SSE/AVX registers |
| Segment Registers | Memory segmentation (mostly legacy) |
Context switch sequence:
- Timer interrupt fires (or process blocks on I/O)
- CPU automatically saves some state, jumps to kernel
- Kernel saves remaining state to Process Control Block (PCB)
- Kernel chooses next process (scheduler)
- Kernel loads new process's saved context
- Return from interrupt → now running new process!
Why it's expensive:
- Must save/restore dozens of registers
- Loses all cache warmth (new process has different data)
- TLB flush needed (different page tables)
- Typical cost: ~1-10 microseconds
Why threads are "lighter":
- Threads share address space → no page table switch
- Threads share most memory → cache stays warmer
- Only need to switch registers, not virtual memory state
Go deeper:
Process control block (Wikipedia) — the PCB that holds saved registers, program counter, stack pointer, and memory-management state.
Context switch (Wikipedia) — exactly which CPU state (registers, PC, page tables, TLB) is copied out and back in.