All Exams Test series for 1 year @ ₹349 only
Question

Consider three intensive processes, which requires 10,20 and 30 units of time and arrive at times 0,2 and 6 respectively. how many context switches are needed if the operating system implements a shortest remaining time first scheduling algorithm? Do not count the context switches at time zero and at the end.

The correct answer is

2

Understanding Shortest Remaining Time First (SRTF) Scheduling

The question asks about the number of context switches when using the Shortest Remaining Time First (SRTF) scheduling algorithm for three given processes. SRTF is a preemptive version of the Shortest Job Next (SJN) scheduling algorithm. In SRTF, the CPU is allocated to the process that has the smallest remaining burst time. When a new process arrives, if its burst time is less than the remaining burst time of the currently executing process, the current process is preempted, and a context switch occurs to the newly arrived process.

Process Details

We have the following processes with their arrival times and burst times (units of time):

  • Process 1 (P1): Arrives at time 0, Burst Time = 10
  • Process 2 (P2): Arrives at time 2, Burst Time = 20
  • Process 3 (P3): Arrives at time 6, Burst Time = 30

We need to simulate the execution using SRTF scheduling and count the context switches, excluding the switch at time zero and the final switch when the last process finishes.

Simulating SRTF Execution and Counting Context Switches

Let's trace the execution time by time and see which process runs and when context switches occur.

Time Interval Events Running Process Ready Queue (Process: Remaining Time) Decision / Context Switch
0 P1 arrives P1 - P1 starts executing (initial switch, not counted)
0 - 2 P1 runs P1 - P1 remaining time = \(10 - 2 = 8\)
2 P2 arrives P1 P2: 20 Compare P1 remaining (8) vs P2 burst (20). P1 < P2. P1 continues. No switch.
2 - 6 P1 runs P1 P2: 20 P1 remaining time = \(8 - 4 = 4\)
6 P3 arrives P1 P2: 20, P3: 30 Compare P1 remaining (4) vs P2 (20) vs P3 (30). P1 is shortest. P1 continues. No switch.
6 - 10 P1 runs P1 P2: 20, P3: 30 P1 remaining time = \(4 - 4 = 0\). P1 finishes.
10 P1 finishes - P2: 20, P3: 30 P1 is done. Choose next from P2, P3 based on remaining time. P2 (20) < P3 (30). P2 starts. Context Switch 1: P1 to P2. (This switch is counted).
10 - 30 P2 runs P2 P3: 30 P2 remaining time = \(20 - 20 = 0\). P2 finishes.
30 P2 finishes - P3: 30 P2 is done. Only P3 remains. P3 starts. Context Switch 2: P2 to P3. (This switch is counted).
30 - 60 P3 runs P3 - P3 remaining time = \(30 - 30 = 0\). P3 finishes.
60 P3 finishes - - All processes finished (final switch, not counted).

Based on the simulation:

  • At time 0, P1 starts. This is the initial dispatch and is not counted as per the question.
  • At time 2, P2 arrives. P1's remaining time (8) is less than P2's burst time (20), so P1 continues. No switch.
  • At time 6, P3 arrives. P1's remaining time (4) is the smallest among ready processes (P1: 4, P2: 20, P3: 30), so P1 continues. No switch.
  • At time 10, P1 finishes. The scheduler chooses the next process from P2 and P3. P2 has the shortest remaining time (20) compared to P3 (30). P2 starts executing. This is a context switch from P1 to P2. This switch is counted. (Count = 1)
  • At time 30, P2 finishes. The only remaining process is P3. P3 starts executing. This is a context switch from P2 to P3. This switch is counted. (Count = 2)
  • At time 60, P3 finishes. This is the final completion and is not counted as a context switch for this problem.

Total Count of Context Switches

Excluding the context switch at time zero and at the end, we counted 2 context switches during the execution flow: one from P1 to P2 and one from P2 to P3.

Final Answer Summary

Simulating the Shortest Remaining Time First (SRTF) scheduling for the given processes shows that there are 2 context switches needed, not counting the initial and final switches.


Revision Table: Key SRTF Scheduling Concepts

Concept Description
SRTF (Shortest Remaining Time First) A preemptive scheduling algorithm that selects the process with the smallest remaining execution time to run.
Preemption The act of interrupting a process currently running on the CPU and switching to another process. SRTF is preemptive.
Context Switch The process of saving the state of one process so that it can be restored later, and restoring the state of another process. This happens when the CPU switches from executing one process to another.
Arrival Time The time at which a process becomes ready to be executed.
Burst Time (Execution Time) The total amount of CPU time required by a process to complete its execution.
Remaining Time The amount of CPU time still needed by a process to complete its execution at any given moment.

Additional Information: SRTF Scheduling Details

SRTF is known for providing minimum average waiting time among all scheduling algorithms. However, it has overhead due to frequent context switches, especially with many processes arriving with small differences in burst times. It also suffers from the possibility of starvation for processes with longer burst times, as they might never get to run if there is a continuous stream of short processes arriving.

Compared to non-preemptive SJN, SRTF offers better response time for short processes. Understanding arrival times and remaining burst times is crucial for accurately simulating SRTF and determining the number of context switches.

Was this answer helpful?

Important Questions from Process Scheduling - Teaching

  1. Consider the following four processes with the arrival time and length of CPU burst given in milliseconds :

    ProcessArrival TimeBurst Time
    P 108
    P 214
    P 329
    P 435

    The average waiting time for preemptive SJF scheduling algorithm is

  2. Consider the following set of processes and the length of CPU burst time given in milliseconds:

    Process

    CPU Burst time (ms)

    P 1

    5

    P 2

    7

    P 3

    6

    P 4

    4

    Assume that processes being scheduled with Round-Robin Scheduling Algorithm with Time Quantum 4ms. Then the waiting time for P 4is _________ ms.

  3. Which of the following CPU scheduling algorithms is/are supported by LINUX operating system?

  4. Given CPU time slice of 2 ms and the following list of processes.

    Process

    Burst time (ms)

    Arrival time (ms)

    p 1

    3

    0

    p 2

    4

    2

    p 3

    5

    5

    Find average turnaround time and average waiting time using round-robin CPU scheduling?
  5. In which of the following scheduling criteria, context switching will never take place?

Need Expert Advice?

Start Your Preparation with Prepp Mobile App

Download the app from Google Play & App Store
Download the app from Google Play & App Store
Prepp Mobile App