Table of contents
1.
Introduction
2.
Context switch-steps
2.1.
Calculating context switch time
3.
Frequently Asked Questions
3.1.
What is a context switch? 
3.2.
Why is context switching required? 
3.3.
How is context switching time calculated? 
4.
Conclusion
Last Updated: Mar 27, 2024

Measure the Time Spent in Context Switch

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?
Operating Systems

Introduction

Context switch means stopping one process and restarting another process. When an event occurs, the operating system saves the state of an active process and restores the new process. Context switching is purely overhead because the system does not perform any practical work while context switching. In other words, we can say that a context switch (also sometimes referred to as a process switch or a task switch ) is the switching of the central processing unit (CPU) from one process to another process. A context switch is needed when a high priority process comes in a ready state, when an interruption occurs and when a transition between the user mode and kernel mode is required.

Context switching time is the time taken between two processes. Most commonly, this happens in multitasking. Switching the CPU to another process requires performing a state save of the current process and restoring a different process.

Its speed varies depending on the memory speed, the number of registers that must be copied, and the existence of special instructions.

Typically the speed of context switching is a few milliseconds.

Recommended Topic, FCFS Scheduling Algorithm, Multiprogramming vs Multitasking

Context switch-steps

The state of the first process must be kept in some way in a switch so that when the scheduler returns to the execution of the first process, it may restore this state and continue.

The process state includes any registers that the process may be utilized, including the program counter and any other operating system-specific data required. This data is usually stored in a data structure called a process control block (PCB) or switch frame.

The first process's PCB must be generated and saved to switch processes. The PCBs are sometimes stored in kernel memory on a per-process stack (rather than the user-mode stack). There may be some particular operating system-designed data structure for this information. Because the operating system has essentially halted the first process's execution, it may now load the PCB and context of the second process. As a result, the program counter from the PCB is loaded, and execution may resume in the new process. A queue or queues are used to choose new processes. The priority of the process and the thread can impact which process continues execution, with processes with the highest priority searching first for ready threads to run.

Context switch steps

Summary notes 

  1. Operating system control (through interrupt)
  2. Save context of a running process in the process PCB
  3. Reload context of a new process from the new process PCB
  4. Return control to the new process

Calculating context switch time

One suitable method could be to record the end instruction timestamp of a process and start timestamp of a process and waiting time in the queue. If all the processes' total execution time was T, then the context switch time = T – (SUM for all processes (waiting time + execution time)).

Assume two processes, P1 and P2.

P1 is now executing, whereas P2 is awaiting execution. The operating system must switch P1 and P2 at some point, let's say at the nth instruction of P1. If t(x, k) represents the timestamp in microseconds of process x's kth instruction, then the context switch would take t(2, 1) – t(2 (1, n).

Another difficulty is that the operating system's scheduling mechanism regulates swapping, and several kernel-level threads also perform context switches. Other programs may be competing for CPU time or the kernel's handling of interrupts. The user does not influence these erroneous context switches. For example, if the kernel decides to handle an interrupt at time t(1, n), the context switch time will be pushed up.

To avoid these significant obstacles, we must create an environment where the task scheduler instantly picks P2 to perform after P1. This can be performed by connecting P1 and P2 via a data channel, such as a pipe. Allow P1 to be the first sender and P2 to be the first receiver. P2 is first blocked (sleeping) while it awaits the data token.

When P1 runs, it sends the data token to P2 through the data channel and immediately reads the response token.

As a result of the context switch, the task scheduler must choose another process to execute. Because P2 is now in a ready-to-run condition, it is a suitable candidate for task scheduler execution. When P2 executes, the roles of P1 and P2 are swapped.

P2 is now the sender, whereas P1 is the blocked receiver.

You can also read about layered structure of operating system, Open Source Operating System

Frequently Asked Questions

What is a context switch? 

Switching the CPU to another process means saving the state of the old process and loading the saved state for the new process is called context switching.

Why is context switching required? 

Context switching allows all processes to share a single CPU to finish their execution and save the system's task state. When the process reloads in the system, the execution of the process resumes from the point where there is a conflict.

How is context switching time calculated? 

One possible way would be to record the end instruction timestamp of a process, the start timestamp, and the waiting time in queue. If the total execution time of all processes was T, then the context switch time was T – (SUM for all processes (waiting time + execution time)).

Conclusion

In this blog, we have learned about context switches. A context switch is the computing method of preserving and restoring a CPU's state (context) so that execution of the process can be resumed later from the same point. Also, we learned the steps of context switching in detail. Then we looked at how to calculate the time between context switching.

Recommended Readings: 


Do check out The Interview guide for Product Based Companies as well as some of the Popular Interview Problems from Top companies like Amazon, Adobe, Google, Uber, Microsoft, etc. on Coding Ninjas Studio.

Also check out some of the Guided Paths on topics such as Data Structure and Algorithms, Competitive Programming, Operating Systems, Computer Networks, DBMS, System Design, etc. as well as some Contests, Test Series, Interview Bundles, and some Interview Experiences curated by top Industry Experts only on Coding Ninjas Studio.

Live masterclass