Skip to main content

Why Is a Process Switch Slower Than a Thread Switch?

๐ŸŽฏ The Questionโ€‹

"Why is switching between two processes significantly more expensive than switching between two threads of the same process? Isn't it just saving and restoring CPU registers in both cases?"


โšก 30-Second Elevator Pitchโ€‹

While both a process switch and a thread switch require saving CPU registers and program counters into a Control Block (PCB/TCB), a thread switch stays within the exact same virtual address space.

A process switch, however, forces the OS to:

  1. Switch the memory address space by reloading the Page Table Base Register (e.g., CR3 on x86).
  2. Invalidate (flush) the Translation Lookaside Buffer (TLB), which caches virtual-to-physical address mappings.

Following a process switch, the CPU suffers heavy TLB misses and cache cold misses, forcing slow multi-level memory page table walks for subsequent instructions.


๐Ÿง  Under-the-Hood: Context Switch Overheadโ€‹

When switching between threads belonging to the same parent process, shared memory mappings (code, heap, global data) remain intact in the CPU hardware cache and TLB:


๐Ÿ”ฌ The Hidden Cost: The TLB Invalidationโ€‹

The Translation Lookaside Buffer (TLB) is an on-chip hardware cache for page table translations (O(1)O(1) virtual o o physical lookup).

  1. In a Thread Switch:

    • Thread T1T_1 and Thread T2T_2 share the same page table.
    • The TLB entries remain 100% valid.
    • Context switch takes ~1โ€“2 microseconds.
  2. In a Process Switch:

    • Process AA and Process BB have distinct page tables.
    • Unless PCID (Process-Context Identifiers) / ASID hardware tags are used, the CPU must flush all TLB entries.
    • Every subsequent memory access causes a TLB Miss, requiring 4 to 5 memory seeks to traverse the hierarchical page table in RAM.
    • Context switch overhead increases to ~5โ€“10+ microseconds, plus prolonged cache warm-up latency.

๐Ÿ“Œ Comparison Matrix: Thread Switch vs. Process Switchโ€‹

Metric / StepThread Context SwitchProcess Context Switch
State Saved / RestoredCPU Registers, SP, PC (TCB)CPU Registers, SP, PC, Memory Limits (PCB)
Address Space ChangeโŒ None (Shared text, data, heap)โœ… Yes (Switches CR3 / Page Table root)
TLB (Translation Buffer)Preserved & HotFlushed / Tagged (High TLB Miss rate)
CPU Cache PenaltyMinimal (Shared working set)High (Working set cold misses)
Relative Latencyโšก Very Fast (~100s of ns โ€“ 1ยตs)๐Ÿข Slow (~5ยตs โ€“ 10ยตs+)

๐Ÿ’ก What Interviewers Ask Next (Follow-Up Traps)โ€‹

  1. "What hardware optimization reduces the cost of TLB flushing during process switches?"

    • Answer: Modern CPUs support PCID (Process Context Identifiers) on x86 or ASID (Address Space Identifiers) on ARM. The TLB tags each cache line with a process ID, allowing entries from multiple processes to coexist in the TLB without requiring a full flush on every context switch.
  2. "Does a context switch involve switching between User Mode and Kernel Mode?"

    • Answer: Yes. Context switching is performed by the OS kernel scheduler. The CPU must trap into Kernel Mode (via a timer interrupt or system call), save execution state, invoke the scheduler, and restore state before returning to User Mode.

Placement & Interview Takeaway

Interview Answer: A process switch is slower than a thread switch because switching processes requires swapping the entire virtual memory address space. This invalidates the CPU's Translation Lookaside Buffer (TLB) and causes cold hardware cache misses, whereas threads share the same address space and keep the TLB warm.


๐Ÿ“บ Video Explanationโ€‹

๐Ÿ’ฌ

Discussion & Doubts