144k views
5 votes
In the context of the Shortest Remaining Time First (preemptive scheduling) algorithm, if three CPU-intensive processes, P1, P2, and P3, with execution times of 20, 10, and 30 units, respectively, arrive at times 1, 3, and 7, then how many context switches will be needed? (Assuming context switches at the beginning and end of the ready queue are not counted.)

a) 3

b) 2

c) 4

d) 5

User Ineztia
by
8.1k points

1 Answer

5 votes

Final answer:

The correct answer is option c (4) context switches. The SRTF algorithm prioritizes processes based on their remaining execution times and executes the process with the shortest time. In this case, P1, P2, and P3 execute in a manner resulting in 4 context switches.

Step-by-step explanation:

The correct answer is option c, which is 4 context switches. To understand Shortest Remaining Time First (SRTF) and the context switches, let's break down the process. At time 1, P1 starts executing. At time 3, P2 arrives and preempts P1 since it has a shorter time to execution (10 units) compared to what remains of P1. Then at time 7, P3 arrives, but it doesn't preempt since it has the longest execution time. P2 will continue until it finishes at time 13, at which point P1 will resume. P1 will execute until P3 preempts it because all remaining times are now equal, but P3 arrived last so it takes precedence. After P3 finishes, P1 will execute until completion.

In terms of context switches:

  1. From P1 to P2 at time 3.
  2. From P2 to P1 at time 13.
  3. From P1 to P3 at time 20 (Since P1 resumed at 13 and has seven units left).
  4. From P3 back to P1 at time 50 (30 units after P3 started, hence completing).

Therefore, there are four context switches required for the processes to execute entirely.

User Laurens Deprost
by
8.6k points