SJF(shortest job first)
Shortest job first (SJF) or shortest job next, is a scheduling policy that selects the waiting process with the smallest execution time to execute next. SJN is a non-preemptive algorithm.
- Shortest Job first has the advantage of having minimum average waiting time among all scheduling algorithms.
- It is a Greedy Algorithm.
- It may cause starvation if shorter processes keep coming. This problem can be solved using the concept of aging.
- It is practically infeasible as Operating System may not know burst time and therefore may not sort them. While it is not possible to predict execution time, several methods can be used to estimate the execution time for a job, such as a weighted average of previous execution times. SJF can be used in specialized environments where accurate estimates of running time are available.
How SJF works?
Shortest Job First scheduling works on the process with the shortest burst time or duration first.
- This is the best approach to minimize waiting time.
- This is used in Batch Systems.
- It is of two types:
- Non Pre-emptive
- Pre-emptive
- To successfully implement it, the burst time/duration time of the processes should be known to the processor in advance, which is practically not feasible all the time.
- This scheduling algorithm is optimal if all the jobs/processes are available at the same time. (either Arrival time is
0
for all, or Arrival time is same for all)
As you can see in the GANTT chart above, the process P4 will be picked up first as it has the shortest burst time, then P2, followed by P3 and at last P1.
We scheduled the same set of processes using the First come first serve algorithm , and got average waiting time to be 18.75 ms
, whereas with SJF, the average waiting time comes out 4.5 ms
.
Algorithm:
1- Sort all the processes in increasing order according to burst time.
2- Then simply, apply FCFS.
[ad type=”banner”] [ad type=”banner”]How to compute below times in Round Robin using a program?
- Completion Time: Time at which process completes its execution.
- Turn Around Time: Time Difference between completion time and arrival time. Turn Around Time = Completion Time – Arrival Time
- Waiting Time(W.T): Time Difference between turn around time and burst time.
Waiting Time = Turn Around Time – Burst Time
C++ Programming:
[ad type=”banner”]Output:
Order in which process gets executed 4 1 3 2 Processes Burst time Waiting time Turn around time 4 3 0 3 1 6 3 9 3 7 9 16 2 8 16 24 Average waiting time = 7 Average turn around time = 13[ad type=”banner”]