In the previous post, we computed the average waiting time and average turnaround time for processes that are scheduled with First Come, First Serve (FCFS) algorithm using Tabular Method.
In this post, we will compute average waiting time and average turnaround time for scheduled processes with another algorithm called Shortest Job First (SJF). You may have already seen an example of SJF in the previous post when we changed the order of the process in FCFS. In shortest job first algorithm, the process with shortest burst time is scheduled first for CPU time.
There is two version of this algorithm.
The preemptive version is also known as Shortest-Job-Remaining-Time and Shortest-Job-First is the non-preemptive version. For example, consider following scheduled processes.
We see that there are 4 processes scheduled according to Shortest-Job-First and the arrival time of the processes is same which is 0.
The burst time decides the order of the processes for CPU scheduling. The order is a job with lowest burst time has the highest priority.
P4 -> P1 -> P3 -> P2
We will use this order in Tabulation Method for computing average wait time and turnaround time as follows.
The table has process ordered according to shortest job first algorithm and the arrival time is same for each process. The order of the process is most important for this method to work and the order is decided by the burst time for each process.
Let’s fill the table according to burst time of each process in such a way that only upper triangular part has the entries and lower triangular part of arrival time is not filled. Also, each row next to the process will have only burst time of that process and nothing else.
Each column total is nothing but Turnaround time for the process. Therefore,
Average Turnaround Time = (3 + 9 + 16 + 24)/4 = 52/4 = 13 ms
The first process in the order is P4 whose wait time is 0 and the first three column total gives wait time for the process – P1, P3, P2 respectively.
Average Waiting Time = (0 + 3 + 9 + 16)/4 = 28/4 = 7 ms
Shortest Job First (SJF) with Different Arrival Time
Shortest Job First Scheduling with a different arrival time for each process add more complexity, but you will find it easy with the Tabular Method. You can easily compute the average waiting time and average turnaround time.
However, you have to consider these very important points before drawing a table.
- First, consider the arrival time of the processes.
- Next, select the process with the smallest burst during that arrival time.
- Reiterate the first and the second step for next arrival time.
- Order the arrival times according to the order of processes.
Consider another schedule of processes,
At this time, the processes are not ordered according to SJF and arrival time is not ordered. We order the process and the arrival time for Tabulation method.
To compute individual turnaround time of each process we subtract the total of each column with arrival time. The processes are ordered as follows
P2 = 10 – 4 = 6
P4 = 11 – 4 = 7
Average Waiting Time
(0 + 3 + 6 + 7)/4 = 16/4 = 4 ms
Abraham Silberschatz, Peter B. Galvin, Greg Gagne, A Silberschatz. 2012. Operating System Concepts, 9th Edition. Wiley.