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)? 2 ways
- Initiated by the process itself through a Trap (OS)
- Memory fault
- Supervisor Call Instruction
- Initiated by the hardware
- Clock interrupt
- I/O interrupt (= I/O completed)
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.
- The main difference is that for interrupt handling, the PCB doesn’t need to be updated if there is no process switch. We simply update the control stack (specifically
See Dispatcher (OS) for how the dispatching is done.