The process of context switching involves the storage of the context/state of a given process in a way that it can be reloaded whenever required, and its execution can be then resumed from the very same point as earlier. It is basically a feature of the multitasking OS, and it allows the sharing of just a single CPU by multiple processes.
In this article, we will look more into the Context Switching in OS according to the GATE Syllabus for (Computer Science Engineering) CSE. We will read ahead to find out more about it.
Table of Contents
- What is Context Switching in OS?
- Why Do We Need Context Switching?
- Context Switching Triggers
- Steps of Context Switching
What is Context Switching in OS?
Context switching refers to a technique/method used by the OS to switch processes from a given state to another one for the execution of its function using the CPUs present in the system. When switching is performed in the system, the old running process’s status is stored as registers, and the CPU is assigned to a new process for the execution of its tasks. While new processes are running in a system, the previous ones must wait in the ready queue. The old process’s execution begins at that particular point at which another process happened to stop it. It describes the features of a multitasking OS where multiple processes share a similar CPU to perform various tasks without the requirement for further processors in the given system.
Why Do We Need Context Switching?
Context switching helps in sharing a single CPU among all processes so as to complete the execution and store the status of the system’s tasks. Whenever the process reloads in a system, its execution begins at the very point in which there is conflict.
Here are some of the reasons why an OS would need context switching:
- The switching of a given process to another one isn’t directly in the system. Context switching helps an OS switch between multiple processes to use the resources of the CPU for accomplishing its tasks and storing its context. The service of a process can be resumed at the same point later on. In case we don’t store the data or context of the currently running process, this stored info may be lost when switching between the given processes.
- In case a high-priority process is falling in a ready queue, the process running currently would be shut down/stopped with the help of a high-priority process for completing its tasks in a system.
- In case a running process needs various I/O resources in a system, another process will switch the current process if it wants to use the CPU. And when it meets the I/O requirements, the previous process would go into a ready state so that it can wait for the CPU execution. Context switching helps in storing the process’s state to resume the tasks in an OS. Else, the process has to restart the execution from the very initial levels.
- In case an interrupt occurs when a process runs in an OS, the status of the process is saved as the registers using context switching. After the interrupts are resolved, the process would switch from a wait to a ready state so as to resume its execution later at the very same point at which the OS interrupt occurs.
- Using context switching, a single CPU can simultaneously handle various process requests without requiring any additional processors.
Examples of Context Switching
Suppose that numerous processes get stored in a PCB (Process Control Block), and a process is in its running state for the execution of its task using the CPUs. As this process runs, another one arrives in the ready queue, which has a comparatively higher priority of completing its assigned task using the system’s CPU. In this case, we used context switching such that it switches the currently running process with the new one that requires the CPU to finish its assigned tasks. When a process is switching, the context switch saves the old process’s status in registers. Whenever any process reloads into a CPU, it initiates the process execution in which the new process intercepts the old process. In case we don’t save the process’s state, we have to begin its execution at its initial level. This way, context switching helps an OS to switch between the given processes and reload or store the process.
Context Switching Triggers
Here are the triggers that lead to context switches in a system:
Interrupts: The CPU requests the data to be read from a disk. In case there are interrupts, the context switching would automatically switch a part of the hardware that needs less time to handle the interrupts.
Multitasking: Context switching is the characteristic of multitasking. They allow a process to switch from the CPU to allow another process to run. When switching a given process, the old state gets saved so as to resume the execution of the process at the very same point in a system.
Kernel/User Switch: It’s used in the OS when it is switching between the kernel mode and the user mode.
What is the PCB?
PCB refers to a data structure that is used in the OS to store all the data-related info to the process. For instance, whenever a process is formed in the OS, the info of the process is updated, information about the process switching, and the process terminated in the PCB.
Steps of Context Switching
Several steps are involved in the context switching of a process. The diagram given below represents context switching between two processes, P1 and P2, in case of an interrupt, I/O need, or the occurrence of a priority-based process in the PCB’s ready queue.
As you can see in the illustration above, the process P1 is initially running on the CPU for the execution of its task. At the very same time, P2, another process, is in its ready state. If an interruption or error has occurred or if the process needs I/O, the P1 process would switch the state from running to waiting.
Before the change of the state of the P1 process, context switching helps in saving the context of the P1 process as registers along with the program counter (to PCB1). Then it loads the P2 process state from its ready state (of PCB2) to its running state.
Here are the steps are taken to switch the P1 to P2:
- The context switching must save the P1’s state as the program counter and register to PCB that is in its running state.
- Now it updates the PCB1 to the process P1 and then moves the process to its appropriate queue, like the ready queue, waiting queue and I/O queue.
- Then, another process enters the running state. We can also select a new process instead of from the ready state that needs to be executed or when a process has a higher priority of executing its task.
- Thus, now we need to update the PCB for the P2 selected process. It involves switching a given process state from its running state or from any other state, such as exit, blocked, or suspended.
- In case the CPU already performs the execution of the P2 process, then we must get the P2 process’s status so as to resume the execution of it at the very same time at the same point at which there’s a system interrupt.
In a similar manner, the P2 process is switched off from the system’s CPU to let the process P1 resume its execution. The process P1 is reloaded to the running state from PCB1 to resume its assigned task at the very same point. Else, the data is lost, so when the process is again executed, it starts the execution at its initial level.
Keep learning and stay tuned to get the latest updates on GATE Exam along with GATE Eligibility Criteria, GATE 2023, GATE Admit Card, GATE Syllabus, GATE Previous Year Question Paper, and more.