• Operating System Video Tutorials

Context Switching in Operating System



In a preemptive multiprogramming system, executing processes often have to stop executing before their completion to allow some other process to execute. The switching of executing processes in the CPU causes a phenomenon called context switching. It is worthwhile to note here that context of a process refers to the properties of the process as well as the resource allotted to it. Context comprises of process stack, memory address space, virtual memory address space, values in its registers, and Stack Pointer (SP).

What is Context Switching?

Context switching is the procedure of storing the context or state of a process so that it can be reloaded when required and execution can be resumed from the same point as earlier. This is a feature of a multitasking operating system and allows a single CPU to be shared by multiple processes.

Context Switching Triggers

There are three major triggers for context switching. These are given as follows −

1. Multitasking

In a multitasking environment, a process is switched out of the CPU so another process can be run. The state of the old process is saved, and the state of the new process is loaded. On a preemptive system, processes may be switched out by the scheduler.

2. Interrupt Handling

The hardware switches a part of the context when an interrupt occurs. This happens automatically. Only some of the context is changed to minimize the time required to handle the interrupt.

3. User and Kernel Mode Switching

A context switch may take place when a transition between the user mode and kernel mode is required in the operating system.

Context Switching Steps

The steps involved in context switching are as follows −

  • Save the context of the process that is currently running on the CPU. Update the process control block and other important fields.
  • Move the process control block of the above process into the relevant queue, such as the ready queue, I/O queue, etc.
  • Select a new process for execution.
  • Update the process control block of the selected process. This includes updating the process state to running.
  • Update the memory management data structures as required.
  • Restore the context of the process that was previously running when it is loaded again on the processor. This is done by loading the previous values of the process control block and registers.

Example of Content Switching

Let us consider a scenario where context switch occurs from process P0 to process P1. The steps that occur are depicted in the following diagram −

Example of Content Switching

As we can see from the diagram, two context switches occur here. The first context switch causes a switch from process P0 to P1. We can see that process P0 is initially executing. A system call or interrupt occurs that requests P0 to be pre-empted from its executing state so that process P1 may execute. In order to do so, context switching takes place. In order to keep the partial results of execution of P0, the state of P0 is saved in its process control block, PCB0. The state of process P1 is reloaded to the CPU from its PCB1. Thus, P0 goes to an idle state, which P1 starts executing. In the second context switch, the reverse procedure occurs, whereby the state of process P1 is saved in PCB1 and the saved state of P0 is loaded from PCB0.

Cost and Performance Evaluation

The procedures of context switches are computationally intensive in nature. A context switch consumes many CPU cycles since it requires saving and reloading the entire contexts of two processes involved in the switching procedure. So, while designing operating systems and choosing scheduling algorithms, one of the main aims remains to optimize the number of context switches.

Switching between the processes requires a considerable amount of time. A lot of tasks are involved, like saving and loading partial values to and from registers and memory maps; updating values in tables, stacks, and files; and saving values in shareable resources. Besides, there are other additional operations that are dependent on the underlying computer architecture and choice of operating system. The time to switch between two separate processes is called the process switching latency.

The context switch has a profound effect on the overall performance of the process management system. If each context switch is done with minimum resource utilization using little time, then the CPU can be mostly engaged in execution of processes, thus improving the overall performance. On the other hand, elaborate context switches may be foolproof but may consume considerable time and resources, thus degrading overall performance. Operating systems that provide single address space for user processes have faster context switches than the operating systems that provide private address space to each process.

Performance can be enhanced if the process scheduler optimizes the number of context switches such that the number of CPU cycles expended for process execution is considerably higher than the number of CPU cycles used for context switches.

Advertisements