LOGBOOK

HELP

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:

  1. Timer interrupt fires (or process blocks on I/O)
  2. CPU automatically saves some state, jumps to kernel
  3. Kernel saves remaining state to Process Control Block (PCB)
  4. Kernel chooses next process (scheduler)
  5. Kernel loads new process's saved context
  6. 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:

From Quiz: REVE1 / Overview of Computer Systems | Updated: Jul 14, 2026