Operating Systems
Context Switch
Definition
A context switch is the process of storing the state of a process or thread, so that it can be restored and resume execution at a later point. This allows multiple processes to share a single CPU.
Why It Matters
Context switching is the core mechanism that enables multitasking. The OS must be able to efficiently save the current state of a process (its registers, memory, etc.) and load the state of another to switch between them.
Contextual Example
When your time slice for one process ends, the OS performs a context switch. It saves all of that process's CPU register values to memory and then loads the saved register values for the next process that is scheduled to run.
Common Misunderstandings
- Context switching has a performance cost. The OS spends some time doing the switch itself, rather than running application code.
- Frequent context switching can be a source of overhead in a heavily loaded system.