An Ultimate Guides to Context Switching in Operating System
What is context switching in operating system?
There are situations in which the Operating System requires you to bring the process back to its running state. If the running process needs to perform an I/O operation, you will need to remove it from the running state. Sometimes the round-robin scheduling algorithm is used.
After each fixed time quantum, it must return to the running state. Context Switching is used to perform these process switchings. This blog will explain the concept of Context Switching within the Operating System. We will also discuss the benefits and drawbacks of Context Switching. So, let’s get started.
What is context switching?
Context switching refers to the switching of the CPU’s task or process from one to another. This phenomenon involves suspending the execution of a process in the running state and executing another process in the ready state.
This is an essential feature of multitasking operating systems. It is possible to switch between processes so quickly that it creates the illusion that they are all being executed simultaneously.
However, the context switching process required a series of steps. It is impossible to switch a process directly from its current state to its ready state. It is necessary to save the context for that process. If you don’t save the context of any particular process, the next time the process comes back to the CPU for execution, the process will begin execution from the beginning.
It should execute from the point it left the CPU during its previous execution. Before any other processes are started, it is important to save the context.
A context refers to the contents of a CPU’s registers at any given time. These are some reasons context switching may occur:
- If a high-priority process is in the ready state. If this happens, it is best to stop the execution of the running process and give the CPU access to the process with higher priority.
- If there is an interruption, the process should be stopped. The CPU should then handle the interrupt before proceeding with any other tasks.
- If a transition between user mode and kernel mode must be made, you will need to perform context switching.
Context switching: steps
There are many steps involved in context switching. The following diagram shows the context switching process between P1 and P2.
As you can see, P1 is initially in the running state while P2 is in the waiting state. After saving the context, you can switch P1 from running into the ready state and P2 from the running state to the running state if there is an interruption. These are the steps to follow:
- First, the context P1 (i.e. The context of the process P1 will be saved in Process Control Block process P1 i.e. PCB1.
- You now need to move the PCB1 into the appropriate queue, i.e. ready queue, I/O queue, waiting queue, etc.
- Select the process P2 from the state of readiness. The process P2.
- Update the Process Control Block for process P2 (i.e. Set the process state of PCB2 to running. You can find the position of the last executed instruction if the process P2 was previously executed by the CPU so you can resume execution of P2.
- You can also repeat the P1 process if you wish to do it again. Follow the steps from step 1 through 4.
Two processes are required for context switching to occur. In the case of round-robin algorithms, however, one process is sufficient.
Context switching time is the amount of time it takes to switch between two processes.
Advantage of Context Switching
Multitasking is achieved by context switching. multiprogramming with time-sharing(learn more about multitasking from here). Multitasking creates the illusion that multiple processes are being run simultaneously. In reality, only one task can be executed by a processor at any given moment. The context switching happens so quickly that the user may feel that the CPU is performing more than one task simultaneously.
Disadvantages Context Switching
Context switching has a disadvantage. It takes some time to do so. The context switching time. It takes time to save the context for one process while it is running and then get the context for another process. The CPU does not do any useful work during that time. This condition means that context switching is purely overhead.