Context Switching - Deep Dive
Quick Reference (TL;DR)
Context Switch: Saving state of current process/thread and restoring another. Cost: ~1-10 microseconds. Expensive due to cache/TLB flushes, register saves, memory barrier overhead.
When it happens: Process/thread blocks, time quantum expires, higher priority thread becomes ready, system call blocks.
Optimization: Modern CPUs have hardware support, but context switches are still expensive.
1. Clear Definition
A context switch is the process of saving the execution state (context) of the currently running process or thread and restoring the state of another process or thread so it can resume execution.
Context includes:
- CPU registers (general purpose, floating point, control)
- Program counter (instruction pointer)
- Stack pointer
- Memory management information (page tables)
- I/O state
π‘ Key Insight: Context switches are expensive because they involve saving/restoring state and invalidating CPU caches/TLB.
2. Core Concepts
What is Context
Process Context includes:
-
CPU Registers:
- General purpose registers (RAX, RBX, RCX, etc.)
- Floating point registers
- Control registers (flags, status)
- Instruction pointer (program counter)
- Stack pointer
-
Memory Management:
- Page table base address (CR3 on x86)
- Memory mappings
- TLB entries (may be flushed)
-
I/O State:
- Open file descriptors
- Network connections
- Device state
-
Process Control Block (PCB):
- Process ID
- Parent process
- Priority
- Scheduling information
Context Switch Steps (Exact)
Complete Process:
1. SAVE CURRENT CONTEXT
ββ Save CPU registers to PCB
ββ Save stack pointer
ββ Save instruction pointer
ββ Save floating point state
2. UPDATE SCHEDULER STATE
ββ Mark current process as "not running"
ββ Update process state (running β ready/blocked)
ββ Add to appropriate queue
3. SELECT NEXT PROCESS
ββ Run scheduler algorithm
ββ Choose next process to run
ββ Update process state (ready β running)
4. RESTORE NEXT CONTEXT
ββ Load CPU registers from PCB
ββ Load stack pointer
ββ Load instruction pointer
ββ Load floating point state
ββ Load page table (CR3)
5. FLUSH CACHES (if needed)
ββ Flush TLB (Translation Lookaside Buffer)
ββ Invalidate instruction cache
ββ Memory barriers
6. RESUME EXECUTION
ββ Jump to saved instruction pointer
Cost Breakdown
Typical Context Switch Cost (~1-10 microseconds):
| Component | Time | Description |
|---|---|---|
| Save registers | 100-500 ns | Write to memory |
| Update scheduler | 50-200 ns | Queue operations |
| Select next process | 100-1000 ns | Scheduler algorithm |
| Restore registers | 100-500 ns | Load from memory |
| Load page table | 200-500 ns | Update CR3 |
| TLB flush | 500-2000 ns | Invalidate TLB |
| Cache effects | 1000-5000 ns | Cache pollution |
| Memory barriers | 50-200 ns | Synchronization |
Total: ~1-10 microseconds (1000-10000 nanoseconds)
Why it's expensive:
- TLB flush: Switching address spaces invalidates TLB
- Cache pollution: New process pollutes CPU cache
- Memory access: Saving/restoring state requires memory writes
- Scheduling overhead: Choosing next process takes time
When Context Switches Happen
Voluntary (process/thread yields):
- Blocking system call (read, write, wait)
- Thread explicitly yields (yield(), sleep())
- Waiting for I/O completion
- Waiting for synchronization (mutex, semaphore)
Involuntary (OS preempts):
- Time quantum expires (timer interrupt)
- Higher priority thread becomes ready
- Interrupt handler needs to run
- Process/thread exceeds CPU time limit
System Call vs Interrupt vs Exception
π― Interview Focus: This distinction is critical.
System Call:
- Trigger: Software (trap instruction)
- Purpose: Request kernel service
- Mode: User β Kernel β User
- Context switch: May or may not (depends on blocking)
Interrupt:
- Trigger: Hardware (I/O device, timer)
- Purpose: Handle hardware events
- Mode: Any β Kernel β Any
- Context switch: Usually not (interrupt handler is fast)
Exception:
- Trigger: CPU (page fault, division by zero)
- Purpose: Handle errors/events
- Mode: Any β Kernel β Any
- Context switch: May trigger (page fault may block)
Key Difference: System calls are synchronous (program requests), interrupts are asynchronous (hardware signals).
Why Syscalls Are Not Interrupts
Similarities:
- Both use interrupt mechanism
- Both switch to kernel mode
- Both have handlers
Differences:
| Aspect | System Call | Interrupt |
|---|---|---|
| Trigger | Software (program) | Hardware (device) |
| Synchronous | Yes (program waits) | No (asynchronous) |
| Purpose | Request service | Handle event |
| Frequency | Program-controlled | Event-driven |
| Context | User-initiated | Hardware-initiated |
Technical: System calls use the interrupt mechanism (int 0x80), but they're conceptually different from hardware interrupts.
Why Context Switching is Expensive Even Today
Modern Challenges:
-
TLB Flush:
- Each process has different page tables
- Switching invalidates TLB
- TLB miss = expensive page table walk
- Modern CPUs: ~100-200 cycles per TLB miss
-
Cache Pollution:
- New process has different memory access pattern
- Pollutes CPU cache (L1, L2, L3)
- Next process has cold cache
- Cache misses are expensive (~100-300 cycles)
-
Memory Bandwidth:
- Saving/restoring registers requires memory writes
- Modern CPUs: ~50-100 GB/s, but still overhead
- Register file is fast, but memory is slower
-
Scheduling Overhead:
- Choosing next process requires computation
- Complex schedulers (CFS) have overhead
- Queue operations (enqueue/dequeue)
-
Memory Barriers:
- Required for correctness
- Prevents instruction reordering
- Adds latency (~50-200 cycles)
Modern Optimizations:
- Hardware context switching (some CPUs)
- Lazy TLB flushing (don't flush if not needed)
- CPU affinity (keep process on same core)
- Better cache locality
But still expensive: Even with optimizations, context switches cost ~1-10 microseconds.
3. Use Cases
When Context Switches Are Necessary
- Multitasking: Multiple processes share CPU
- I/O Operations: Process blocks waiting for I/O
- Time Slicing: Fair CPU time allocation
- Priority Scheduling: Higher priority process preempts
- Real-time Systems: Meet timing deadlines
When to Minimize Context Switches
- High-performance code: Reduce overhead
- Real-time systems: Predictable latency
- Latency-sensitive: Minimize delay
- CPU-bound tasks: Avoid unnecessary switches
4. Advantages & Disadvantages
Context Switching
Advantages:
β
Multitasking: Multiple processes can run
β
Fairness: CPU time is shared
β
Responsiveness: System remains responsive
β
I/O Overlap: CPU works while I/O happens
Disadvantages:
β Overhead: ~1-10 microseconds per switch
β Cache effects: Pollutes CPU cache
β TLB flush: Invalidates address translation cache
β Latency: Adds delay to operations
5. Best Practices
- Minimize switches: Batch operations, avoid unnecessary blocking
- CPU affinity: Keep process on same core when possible
- Lock-free algorithms: Reduce need for blocking
- Profile: Measure context switch frequency
- Optimize hot paths: Reduce switches in critical code
6. Common Pitfalls
β οΈ Mistake: Thinking context switches are "free" (they're expensive)
β οΈ Mistake: Confusing system call with context switch
β οΈ Mistake: Not understanding when switches happen
β οΈ Mistake: Over-optimizing (premature optimization)
7. Interview Tips
Common Questions:
- "What is a context switch?"
- "Why are context switches expensive?"
- "What's the difference between system call and context switch?"
- "How can we reduce context switches?"
Key Points:
- Context = saved state (registers, stack, etc.)
- Expensive due to TLB flush, cache pollution
- System call may or may not trigger context switch
- Modern optimizations help, but still expensive
8. Related Topics
- System Calls (Topic 4): May trigger context switches
- Process Management (Topic 5): Process context switching
- Threads (Topic 6): Thread context switching (cheaper)
- CPU Scheduling (Topic 7): When context switches happen
9. Visual Aids
Context Switch Timeline
Process A Running Process B Running
β β
β Timer interrupt β
ββββββββββββββββββββββββββββ>β
β Save A's context β
β (registers, stack, etc.) β
β β
β Load B's context β
β (registers, stack, etc.) β
β β
β β Resume B
β β (from saved PC)
β β
Process vs Thread Context Switch
Process Context Switch:
- Save/restore full address space
- TLB flush required
- Cost: ~1-10 microseconds
Thread Context Switch:
- Save/restore registers only
- No TLB flush (same address space)
- Cost: ~100-1000 nanoseconds (10x faster)
10. Quick Reference Summary
Context Switch:
- Save current state, restore next state
- Cost: ~1-10 microseconds
- Expensive due to TLB flush, cache pollution
When it happens:
- Time quantum expires
- Process/thread blocks
- Higher priority ready
- System call blocks
Key Insight: Context switches enable multitasking but have significant overhead. Modern systems optimize but can't eliminate the cost.