In this article, we have discussed about Context Switching, which is one of the most important and fundamental topics in Operating Systems.
Table of Contents
- What is Context Switching?
- The need for Context Switching
- Preserving State
- I/O Resource Allocation
- Handling Interrupts
- Context Switching triggers
3.User and Kernel Mode Switching
- What is the PCB?
- Steps for Context Switching
- The disadvantage of Context Switching
- Difference between Swapping and Context switching
- Context Switching vs Swapping
But before we discuss Context Switching, we must know what we refer to as a context with respect to Operating Systems.
When a process is removed from the processor, information about process must be stored such that when it is scheduled to run on the processor later, it can continue it's operation from the same state.
This operational state data is called as context.
The best way to visualise it is to think that what context is to a process is what a bookmark is to a book.
The purpose of a context is to be useful when the processor returns to the process.
What is the context switching in the operating system?
Have you ever wondered how multitasking processors handle multiple processes simultaneously, and when you go back to any of the processes, it starts from the same point where it was left?
This is possible because of Context Switching.
Context Switching helps store the context of a process, thread, or state, which can be accessed at the same point when reloaded.
Tt allows multiple processes to share the same CPU at a single given point of time, making it a multitasking feature.
Context switching helps in sharing a single processor across all processes to complete its execution and store the task status.
When the process reloads, the process executes at the same point where there was conflict.
For the sake of ease, we will mostly refer Context Switching for processes in this article.
The need for Context switching
Context switching is a very important feature in Operating Systems. Here are some of the reasons we need context switching:
The switching of one process to another process is not direct.
Context switching helps the system that is switching between the processes to use the CPU's resources to store its context.
We can resume the service at the same point later. We have to store the currently running process's data or context otherwise the stored data may be lost while switching between processes.
Ready queue in Operating Systems keeps a set of all processes residing in main memory, ready and waiting to execute in a line. A new process is always put in this queue.
If a process which has a priority higher than the currently running process falls into the ready queue, the currently running process will be shut down or stopped by a high priority process to complete its tasks in the system.
This ensures more important processes aren't held up by lower priority processes running.
I/O Resource Allocation
For processes, using I/O (Input/Output) devices can be expensive. A process running and using I/O resources will hold up the processor for other processes.
If a process requires I/O resources in the system, the current process will be switched by another process to use the CPUs.
And when the requirement is met, the old process goes into a ready state to wait for its execution.
Context switching stores the state of the process for it to resume its tasks. Otherwise, the process needs to restart its execution from it's initial state.
An Interrupt is a signal emitted by a hardware or software when a process needs immediate attention. It alerts the processor in favor of a high-priority process requiring interruption of the current process.
If interrupts occur while a process is running in the operating system, the process status is saved as registers by context switching.
After resolving the interrupts, the process switches from a waiting state to a ready state to resume its execution from the same point.
Context switching allows a single CPU to handle multiple process requests simultaneously without the need for any additional processors.
The last but also one of the most important reasons.
Context switching allows a single CPU to handle multiple process requests simultaneously without it needing any additional processors.
Context switching triggers
There are three major triggers for context switching. These are given as follows −
In a multitasking environment, a process is switched out of the CPU for another process to be run. The state of the old process is saved and the state of the new process is loaded.
On a system with pre-emption, processes may be switched out by the scheduler.
A part of the context is switch by the hardware when an interrupt occurs. This happens automatically.
Only a part of the context is changed to reduce the time required to handle the interrupt.
User and Kernel Mode Switching:
In Operating Systems, the user mode is the default mode where a process has limited access to system resources.
While the kernel mode is the privileged mode where a process has unrestricted access to system resources.
A context switch takes place in transition between the user mode and kernel mode is required in the operating system.
What is the PCB?
A PCB (Process Control Block) is a data structure that's used in the operating system to store all data related information to the process and context.
E.g. When a process is created, information is updated of the process, switching information of the process, process terminated in the PCB.
The context of a process includes things like:
- Address space, stack space, virtual address space
- Register set image (e.g. Program Counter (PC), Stack Pointer (SP), Instruction Register (IR), Program Status Word (PSW) and others),
- Updating profiling or accounting information,
- Making a snapshot of its associated kernel data structures and updating the current state.
All of this information is stored in the Process Control Block.
Steps for Context Switching
There are several steps involves in context switching of the processes.
The following diagram helps visualise the context switching of two processes, P1 to P2, when an interrupt occurs, it's I/O needs, or priority-based process occurs in the ready queue.
As we can see, initially, the P1 is running on the CPU to execute its task, and at the same time, P2, is in the ready state.
If an interruption occurs or the process requires I/O resources, the P1 process switches its state from running to waiting.
Before changing the state of the process P1, context switching saves the context of P1 in registers and the program counter to the PCB1. After that, it loads the state of P2 from ready state of the PCB2 to the running state.
The following steps are taken when switching P1 to P2:
- Context switching needs to save the state of P1 in the program counter and the registers to the PCB, which is in it's running state. We can call it PCB1.
- PCB1 is updated to P1 and moves the process to the appropriate queue, such as the ready queue, I/O queue and waiting queue.
- A process gets into the running state, or we select a new process from the ready state, which is to be executed, or the process with a higher priority to execute its task. We can call this P2.
- PCB is updated for P2 called PCB2. It includes switching the state from ready to running or from another state like blocked or suspend.
- If the CPU has already executed P2, we need to get the status of P2 to resume its execution at the same time point when the system interrupt occurs.
Similarly, P2 is switched off from the CPU so that the P1 can resume execution. P1 is reloaded from PCB1 to the running state to resume its task at the same point.
If not, the information is lost, and when the process is executed again, it starts execution from it's initial state.
The disadvantage of Context Switching
A disadvantage with is that it has a cost in performance.
It is directly due to running the task scheduler, translate and the TLB flushes, and indirectly due to the sharing of the CPU cache between multiple tasks.
These costs are purely overhead. The processor is not useful for anything else while the context switching happens.
A solution to this to use threads, instead of processes or increase the degree of multithreading since context switching threads have lower cost compared to process' context switching as threads have the same virtual memory. Because of this TLB flushing is not required.
Difference between Swapping and Context switching
People sometimes confuse Context Switching with another Operating System scheme called Swapping.
In this section we will discuss the difference between Context Switching and Swapping. But first we should first know what Swapping is:
Swapping is a memory management scheme in which any process can be temporarily swapped from main memory to secondary memory so that the main memory can be made available for other processes. It is used to improve main memory utilization. In secondary memory, the place where the swapped-out process is stored is called swap space.
The purpose of the swapping in operating systems is to access the data stored in the hard disk and bring it to RAM so that the applications can use it.
Swapping is used only when data is not present in RAM.
Context Switching vs Swapping
|Stores the state of a process & loads it into a new one.||It is a method of replicating the entire process.|
|Transfering of control of CPU from one process to another.||The entire process is moved to the disk.|
|Determines whether a process is paused||Deals with how much memory is being swapped.|
|Toggles the process from running to ready states.||Exchange of data between the disk & main memory.|
|Active processes do context switching.||Inactive processes do swapping.|
|It offers a higher degree of multi-tasking.||Provides a more degree of multiprogramming.|
|It helps to get better utilization of the operating system.||It helps to get better utilization of memory.|
Which of the following can periodically trigger the context switch?
After reading this article at OpenGenus, you must now have a good understanding of Context Switching and the other related concepts.
You can understand the other aspects of Operating Systems much better that are based on this concept.