up:: Fundamentals of Operating Systems prev:: Chapter 13 – Processes vs Threads next:: Chapter 15 – CPU Scheduling
Chapter 14 – Context Switching
Core idea
A context switch saves the state of one execution unit and restores another.
What gets saved
- Registers
- Program Counter
- Stack Pointer
- CPU flags
- Memory mappings (if process switch)
Types
- Thread switch – cheaper
- Process switch – more expensive
When switches happen
- Time slice expired
- I/O blocking
- Higher-priority process ready
- System call
Cost
- No useful work done
- Cache pollution
- TLB flush (for process switches)
Exam angle
- Context switches are overhead
- Threads reduce switching cost