Context Switching in Operating System with the help of Diagram

In context switching, generally, “context” means situation and “switching” means jumping from one option to another option. Hence, when the process keeps jumping or switching between the processor is referred to as context switching. Context switching is the changing in the state of processes so that a single CPU can be used by multiple processes.

A state renews several processes, this work is known as the context. Switching the processor by processes needs to function a state similar of the current process. Context switching is a property of a multitasking OS, and it permits an individual CPU to be common in several processes. We can’t directly switch a process from one state to another state, and it requires some steps to be followed.

The steps included in the context switching are

  1. Initially, the context of the process (P1), which is present in the running state, will be kept in the PCB1 i.e., the process control block.
  2. After this, we have to change the PCB1 to the relevant queue like the waiting queue, ready queue and I/O queue.
  3. Now, choose the new process P2 from the ready queue that is to be run or executed.
  4. Renew the PCB2 by putting the process state to executing. Update the data structure with new process details.
  5. In the same way, when we want to run the process P1 once more, then we have to attend the above steps. 

Advantages of Context Switching

Context Switching helps to achieve multitasking. Multitasking provides us with an imagination that the processor executes multiple processes simultaneously.

In actuality, a single task is being executed at an instant of time by a CPU. But here, switching is so rapid, due to this, we feel that the CPU is executing multiple processes at one time. 

Disadvantages of Context switching

It needs some context switching time to save the context. During this time, the processor can’t do any useful work from the client’s viewpoint.