Process Switch
When the OS changes from running process P1 to running process P2, it’s referred to as a process switch or context switch.
Context switching is the technique where CPU time is shared across all running processes and is key for multitasking.
When is the execution of a process halted (and potentially switch to different process)? 3 kinds of events that triggers it:
- Interrupt, such as
- Clock interrupt
- I/O interrupt (= I/O completed)
- Memory fault
- Trap (OS)
- Supervisor Call Instruction
How it works
Alright, how does a process switch actually work?
- Save context of processor including program counter and other registers
- Update the process control block of the process that is currently in the “Running” state
- Move process control block to 1 of the 3 appropriate queues:
- Ready
- Blocked
- Ready/suspend
- Select another process for execution
- Update the process control block of the process selected
- Update memory-management data structures
- Restore context of the selected process
The mechanism is very similar to Interrupt Handling.
See Dispatcher (OS) for how the dispatching is done.