Scheduling Algorithms

In the diagram above, process P1, which arrives first, will start running on the CPU. At millisecond 1, when process P2 with a shorter burst time arrives, the Scheduler will forcefully remove P1 from the CPU and let P2 run instead. Since the next arriving processes, P3 and P4, have longer burst times than this of P2, the Scheduler won't remove P2 from the CPU. After P2 finishes running, the Scheduler will run the remaining processes as follows: P4 (since its burst time is 5 ms,) then P1 (since its remaining burst time is 8 - 1 = 7 ms,) and finally P3 (since its burst time is the longest: 9 ms.)

Because the preemptive version of the SJF algorithm relies on the remaining burst time, we often call this algorithm the shortest remaining time first (SRTF) scheduling.

Finally, the average waiting time in this schedule is ((0 - 0) + (10 - 1) + (1 - 1) + (17 - 2) + (5 - 3)) / 4 = 26 / 4 = 6.5 ms. The reason we do subtraction inside the parentheses is that we subtract the arrival times from the wait times. If we use the cooperative version of SJF, the average waiting time would be 7.75 ms, and for FCFS it would be 8.75 ms.

The average resopnse time is ((0 - 0) + (1 - 1) + (5 - 3) + (17 - 2)) / 4 = 4.25 ms.