In: Computer Science
Context switching occurs when a task surrenders the CPU to
another task or
is preempted by another task. Use program examples (pseudo code
plus Gantt
charts) to show a scenario for each of the cases.
The procedure of switching from one process to another process is called Context switching. A computer system consists of many tasks to be completed. If a task requires more time to be completed and a second task can be completed within that time then, the computer switches the ongoing process and jumps to the second task putting the first task on hold. After the completion of the second task, the first task resumes smoothly. This process is known as Context switching.
Pseudo Code for round robin algorithm :
1. CPU scheduler picks the process from the circular/ready queue , set a timer to interrupt it after 1 time slice / quantum and dispatches it .
2. If process has burst time less than 1 time slice/quantum
then Process will leave the CPU after the completion
CPU will proceed with the next process in the ready queue / circular queue .
3. else If process has burst time longer than 1 time slice/quantum
then Timer will be stopped . It cause interruption to the OS .
Executed process is then placed at the tail of the circular / ready querue by applying the context switch
CPU scheduler then proceeds by selecting the next process in the ready queue .
For example ,
Consider three processes P1, P2, P3
Burst time Waiting time Turnaround time
P1 24 6 30
P2 3 4 7
P3 3 7 10
Gantt chart for the round robin algorithm :
|-----------|-----------|----------|------------|------------|------------|------------|---------------|
| P1 | P2 | P3 | P1 | P1 | P1 | P1 | P1
|-----------|-----------|----------|------------|------------|------------|------------|---------------|
0 4 7 10 14 18 22 26 30