A Context switch is the time spent between two processes (i.e., bringing a waiting process into execution and sending an executing process into waiting state). This happens in multitasking.The operating system must bring the state information if waiting process into memory and save the state information of the currently running process.
In order to solve this problem, we would like to record the timestamps of the first and last instruction of the swapping processes.The context switch time is the difference between the two processes.
Let’s take an example: Assume there are only two processes, P1 and P2.
P1 is executing and P2 is waiting for execution. At some point, the operating system must swap P1 and P2, let’s assume it happens at the nth instruction of P1. If t(x, k) indicates the timestamp in microseconds of the kth instruction of process x, then the context switch would take t(2, 1) – t(1, n).
Another issue is that swapping is governed by the scheduling algorithm of the operating system and there may be many kernel level threads which are also doing context switches. Other processes could be contending for the CPU or the kernel handling interrupts. The user does not have any control over these extraneous context switches. For instance, if at time t(1, n) the kernel decides to handle an interrupt, then the context switch time would be overstated.
In order to avoid these obstacles, we must construct an environment such that after P1 executes, the task scheduler immediately selects P2 to run. This may be accomplished by constructing a data channel, such as a pipe between P1 and P2.
That is, let’s allow P1 to be the initial sender and P2 be the receiver. Initially, P2 is blocked(sleeping) as it awaits the data token. When P1 executes, it delivers the data token over the data channel to P2 and immediately attempts to read the response token. A context switch results and the task scheduler must selects another process to run.Since P2 is now in a ready-to-run state, it is a desirable candidate to be selected by the task scheduler for execution.When P2 runs, the role of P1 and P2 are swapped. P2 is now acting as the sender and P1 as the blocked receiver.
To summaries –
- P2 blocks awaiting data from P1
- P1 marks the starting time.
- P1 sends token to P2.
- P1 attempts to read a response token from P2. This induces a context switch.
- P2 is scheduled and receives the token.
- P2 sends a response token to P1.
- P2 attempts read a response token from P1. This induces a context switch.
- P1 is scheduled and receives the token.
- P1 marks the end time.
The key is that the delivery of a data token induces a context switch. Let Td and Tr be the time it takes to deliver and receive a data token, respectively, and let Tc be the amount of time spent in a context switch. At step 2, P1 records the timestamp of the delivery of the token, and at step 9, it records the timestamp of the response. The amount of time elapsed, T, between these events may be expressed by:
T = 2 * (Td + Tc + Tr)
This formula arises because of the following events:
- P1 sends the token (3)
- CPU context switches (4)
- P2 receives it (5)
- P2 then sends the response token (6)
- CPU context switches (7)
- and finally, P1 receives it (8)
GATE CS Practice Questions –
- TOC | Context-sensitive Grammar (CSG) and Language (CSL)
- Check if the language is Context Free or Not
- Regular Expression Vs Context Free Grammar
- Converting Context Free Grammar to Chomsky Normal Form
- Converting Context Free Grammar to Greibach Normal Form
- Difference between Hub and Switch
- Difference between Router and Switch
- Difference between Router and Layer-3 Switch
- Difference between Time Sharing OS and Real-Time OS
- Real Time Systems
- Tasks in Real Time systems
- Priority CPU Scheduling with different arrival time - Set 2
- Shortest Job First CPU Scheduling with predicted burst time
- Algorithm for Dynamic Time out timer Calculation
- GATE CS 2016 Sec 5 – Time Space Complexity
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.