Start here
Context switch (definition)
The scheduler picks the next runnable entity, saves the old execution context (registers, kernel stack pointer, and optionally FPU), updates address-space registers if needed, then restores the new context and resumes execution.
By the end of this page you should be able to
- Explain why switching between two threads in the same process is often cheaper than two processes.
- Name two costs beyond saving general-purpose registers.
- Relate `switch_mm` (conceptually) to address-space change.
Operating systems · Processes
Context switching
Saving one execution context and restoring another
TL;DR
A context switch changes the CPU from running one thread to another: save registers and kernel stack pointer, switch address space if needed (`switch_mm`), load the next thread’s state, and resume at its saved PC, cost includes TLB/cache effects beyond the obvious register moves.
Summary
Context switches are the tax of multitasking. OS design minimizes them via batching, smarter wakeup patterns, and avoiding unnecessary cross-core migrations.
Deeper narrative
Context switches are not free: switching `mm` flushes TLB shootdowns on some paths; FPU state is lazy; spectre mitigations add fences. The scheduler switches when quantum expires, higher priority wakes, or blocking syscalls yield CPU.
Mechanism (step by step)
Pick next
Scheduler selects runnable entity respecting affinity and cgroup limits.
Save old
Store GPRs, kernel SP, FPU if needed, perf counters optional.
Switch address space
If `mm` differs, load CR3 / ASID; shootdown remote TLB entries if required.
Restore new
Load GPRs, return to kernel or user mode continuation.
Key insights
Scheduler vs context switch
Address space switch cost
Kernel preemption
Metrics
Did you know?
Common pitfalls
Ignoring TLB effects
Thread switches cheaper than process switches due to shared `mm`: benchmark fairly.
Measuring switch cost alone
Cache cold misses after switch dominate microbenchmarks, use realistic working sets.
Comparison
Thread switch vs process switch (typical)
| Thread switch (same process) | Process switch | |
|---|---|---|
| Address space | Usually unchanged | Often `switch_mm` |
| TLB user entries | May remain valid | Often flushed |
| Typical relative cost | Lower | Higher |
Another lens
Switch cost drivers
| Change | Typical extra cost |
|---|---|
| Same thread group, same mm | Registers + kernel stack |
| Different mm | TLB/PCID/ASID management |
| Cross-CPU migration | Cache cold + runqueue remote locks |
Glossary
ASID
CR3
Runqueue lock
Interview follow-ups
- Why is switching between two kernel threads cheaper than two processes?
- What is lazy FPU context switching?
- When does `switch_mm` not run during a context switch?
Callouts
Perf
If profiling shows high `context-switches` with low CPU, look for lock contention or excessive `poll`/`select` wakeups.
Walkthrough & deep dive
Left: practical “how would I use this?” threads. Right: slower, code-leaning explanations, open one panel at a time so the page does not feel like a wall of sameness.
Walkthrough
Estimate
Deep dive
What `schedule()` does (conceptually)
Dequeue current from runqueue if not blocking, pick next entity via class-specific logic, context_switch to next, return when your task runs again.
Context switch rate
grep ctxt /proc/stat
Self-check
1Switching address spaces on x86 typically involves…
2Voluntary context switch often follows…
3TLB flushes hurt performance because…