Process

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:

  1. Interrupt, such as
    • Clock interrupt
    • I/O interrupt (= I/O completed)
    • Memory fault
  2. Trap (OS)
  3. Supervisor Call Instruction

How it works

Alright, how does a process switch actually work?

  1. Save context of processor including program counter and other registers
  2. Update the process control block of the process that is currently in the “Running” state
  3. Move process control block to 1 of the 3 appropriate queues:
    • Ready
    • Blocked
    • Ready/suspend
  4. Select another process for execution
  5. Update the process control block of the process selected
  6. Update memory-management data structures
  7. Restore context of the selected process

The mechanism is very similar to Interrupt Handling.

See Dispatcher (OS) for how the dispatching is done.