Context Switching in Operating System (OS)

Context Switch in OS

Context Switching is a cost and time saving measure performed by the CPU that is handing task 1 and has to stop executing this task to priority execute another task 2. To do this effectively, the system stores the initial task in its processed form so that when this task is resumed, it can be loaded and resumed from the same progress point as earlier.

This allows the computer to multitask and multiple processes can share the same CPU. This is also done to make sure that tasks do conflict one another, as they may be holding a same resource for execution like memory etc.

Context Switching in OS Operating System

Steps Leading to Context Switching

In the above diagram the following happens –

  1. Process 1 running in the system
  2. Interrupt or system call appears
  3. Causes Process 1 to be saved into PCB 1 and switched out
  4. Process 2 is loaded from PCB 2 and executed
  5. Again interrupt or system call appears
  6. Process 2 is saved into PCB 2 and process 1 loaded from PCB 1
  7. Process 1 is executed

Triggers for Context Switching

  1. Multitasking
  2. Interrupt Handling
  3. User and Kernel mode Switch – System may switch from user mode to the kernel mode, you can think of this as windows 10 running and system is switched to boot mode.