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 a 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 ageing.
- 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.
- Sort all the process according to the arrival time.
- Then select that process which has minimum arrival time and minimum Burst time.
- After completion of process make a pool of process which after till the completion of previous process and select that process among the pool which is having minimum Burst time.
How to compute below times in SJF 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
In this post, we have assumed arrival times as 0, so turn around and completion times are same.
Process ID Arrival Time Burst Time 1 2 3 2 0 4 3 4 2 4 5 4 Final Result... Process ID Arrival Time Burst Time Waiting Time Turnaround Time 2 0 4 0 4 3 4 2 0 2 1 2 3 4 7 4 5 4 4 8
In Set-2 we will discuss the preemptive version of SJF i.e. Shortest Remaining Time First
This article is contributed by Mahesh Kumar(NCE, Chandi). If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Program for Shortest Job First (SJF) scheduling | Set 2 (Preemptive)
- Shortest Job First (or SJF) CPU Scheduling Non-preemptive algorithm using Segment Tree
- Program for Preemptive Priority CPU Scheduling
- Difference between Preemptive and Non-preemptive CPU scheduling algorithms
- Preemptive and Non-Preemptive Scheduling
- Difference between Preemptive Priority based and Non-preemptive Priority based CPU scheduling algorithms
- First Come, First Serve – CPU Scheduling | (Non-preemptive)
- Implementation of Non-Preemptive Shortest Job First using Priority Queue
- Difference between Priority scheduling and Shortest Job First (SJF) CPU scheduling
- Shortest Job First CPU Scheduling with predicted burst time
- Difference between Shortest Job First (SJF) and Round-Robin (RR) scheduling algorithms
- Difference between Multi Level Queue Scheduling (MLQ) and Shortest Job First
- Program for FCFS CPU Scheduling | Set 1
- Program for Priority CPU Scheduling | Set 1
- Program for Round Robin scheduling | Set 1
- Program for SSTF disk scheduling algorithm
- Program for FCFS CPU Scheduling | Set 2 (Processes with different arrival times)
- Longest Remaining Time First (LRTF) CPU Scheduling Program
- Difference between Multi Level Queue Scheduling (MLQ) and Priority Scheduling
- Multilevel Feedback Queue Scheduling (MLFQ) CPU Scheduling