← Back to CS Fundamentals

Context Switch

CS FundamentalsMid

The Question

What is a context switch and why is it expensive?

What a Strong Answer Covers

  • saves registers/program counter
  • "process switch more expensive
  • mention page tables or TLB

Senior-Level Answer

A context switch is the mechanism by which the operating system's scheduler pauses one executing process or thread and resumes another. It is fundamental to multitasking—the OS creates the illusion of simultaneous execution by rapidly switching between tasks.

**What happens during a context switch**: The CPU state of the outgoing task must be fully saved to its Process Control Block (PCB) or Thread Control Block (TCB): all general-purpose registers, the program counter (where execution was), the stack pointer, CPU flags, and floating-point/SIMD register state. The scheduler then selects the next task, loads its saved state into the CPU registers, and resumes execution from where that task left off.

**Why context switches are expensive**: The register save/restore itself takes ~hundreds of nanoseconds and is relatively cheap. The real costs are indirect:

1. **TLB flush**: Each process has its own virtual-to-physical address mapping. Switching processes invalidates the TLB (Translation Lookaside Buffer), causing subsequent memory accesses to take slower page-table walks until the TLB warms up again. Thread context switches within the same process don't require a TLB flush (shared address space).

2. **CPU cache cold**: The outgoing task's working set was in L1/L2 cache. The new task's data is likely not. The first many memory accesses after a switch are cache misses, taking 100× longer than cache hits. This is the dominant cost for cache-sensitive workloads.

3. **Pipeline flush**: Modern CPUs use deep out-of-order execution pipelines. A context switch requires flushing the pipeline, discarding speculative work.

4. **Scheduler overhead**: The OS scheduler itself runs, selecting the next task from run queues—additional CPU cycles not doing user work.

Thread context switches are cheaper than process switches (no TLB flush, shared memory mappings), and user-space context switches (green threads, coroutines) are cheaper still—they only save/restore a minimal software stack frame, with no kernel involvement.

This cost is why high-performance servers prefer event-driven async models (asyncio, Node.js) over thread-per-request models: thousands of coroutines can multiplex on one thread with no context-switch overhead.

What Separates a 2/3 from a 3/3

2/3 — Passing but Incomplete

Explains what CPU state is saved/restored, identifies TLB flush and cache invalidation as the dominant costs, distinguishes thread vs. process switch cost.

3/3 — Strong Answer

Covers PCB/TCB mechanism, explains TLB flush and why it doesn't happen for thread switches within a process, quantifies relative costs (cache miss vs. cache hit), explains pipeline flush, and connects to practical design implications (async I/O vs. thread-per-request).

Common Mistakes

  • Saying context switches only cost 'a few cycles' — TLB and cache effects make them orders of magnitude more expensive
  • Not distinguishing process vs. thread context switch — thread switches skip TLB flush
  • Forgetting the indirect costs (cache cold, TLB) and focusing only on register save/restore
  • Not connecting to why this matters for system design decisions

Follow-Up Questions

  • Why is a thread context switch cheaper than a process context switch? — Threads share the same virtual address space, so no TLB flush is needed. Process switches invalidate the TLB, requiring expensive page-table walks until it warms up.
  • How do coroutines (green threads) avoid kernel context switch overhead? — Coroutines switch in user space only — no kernel trap, no full CPU register save, no scheduler invocation. Only a minimal software stack frame is swapped. The OS scheduler never sees individual coroutine switches.
  • What is a voluntary vs. involuntary context switch? — Voluntary: the running task yields (blocks on I/O, sleeps). Involuntary: the scheduler preempts a running task when its time slice expires. High involuntary switches on a process suggest CPU contention.
  • How would you diagnose excessive context switching on a Linux system? — vmstat 1 shows cs (context switches per second). pidstat -w shows per-process voluntary/involuntary switches. High involuntary switches indicate CPU saturation; high voluntary switches may indicate I/O blocking.

Related Questions

  • Process vs Thread
  • Race Condition
  • Deadlock — 4 Conditions, Prevention
  • Thread Safety — Is dict Thread Safe?
  • HashMap Internals

Can You Explain This Cold?

Reading the answer is step one. Explaining it unprompted — under interview pressure — is what actually matters. Get AI-graded feedback on your answer with follow-up probes on your weak points.

Get Graded — Free Assessment
GrindQuestionsAITechnical interview assessment
TermsPrivacyAbout