Calculating Average Waiting time in SJF algorithm

44.5k views Asked by At

SJF Table and its Gantt Chart

I understand how preemptive SJF Scheduling works but I dont know how to calculate the average waiting time..
This example is taken from 'OS Principles' by Galvin, Gagne and Silberschatz.
They calculate the average waiting time as:
((10-1)+(1-1)+(17-2)+(5-3)) / 4 = 6.5ms
How is this calculated? Please simplify.
Also, it would be great if you could solve the same using RR algorithm with time quantum of say... 5ms (for simple calculation) and explain how to calculate the avg waiting time

1

There are 1 answers

5
barak1412 On BEST ANSWER

Just look at the diagram and calculate for every process, how much time he have been waiting:

P1 run at the beginning 1 time (he is the only one) thus he wait 0 here. Than, he wait from time 1 until time 10 -> total waiting time = (10-1).

P2 arrive in time 1, and start running right away. So he didn't wait at all -> (1-1).

P3 arrive in time 2, and begin running in time 17 -> (17-2).

P4 arrive in time 3, and start running in time 5 -> total waiting time = (5-3).

Thus, the average waiting time = ((10-1)+(1-1)+(17-2)+(5-3))/4 = 6.5ms.

EDIT : For RR with 5 quanta, that's what gonna happen:

P1 arrives, and get quanta of 5ms -> in time 5, he stops with remaining burst of 3. Next, P2 will start running from time 5 to time 9. (his burst is only 4). So you get P2 waited for (5-1).

P3 will start running, from time 9 to time 14 with remaining burst of 4. Next, P4 will start running from time 14 to time 19 and will end. Total waiting time for P4 is (14-3).

Than P1 will continue running from time 19 to time 22 and will end. Total waiting time for P1 is (19-5). Than, P3 continue running from time 22 to time 26. Total waiting time for P3 is (9-2) + (22-14).

So you get average running time = ((5-1)+(14-3)+(19-5)+(9-2)+(22-14))/4 = 10.5ms