
- OS - Home
- OS - Needs
- OS - Overview
- OS - History
- OS - Components
- OS - Structure
- OS - Architecture
- OS - Services
- OS - Properties
- OS - TAT & WAT
- OS Processes
- OS - Processes
- OS - Process Scheduling
- OS - Scheduling Algorithms
- FCFS Scheduling Algorithm
- SJF Scheduling Algorithm
- Round Robin Scheduling Algorithms
- HRRN Scheduling Algorithms
- Priority Scheduling Algorithms
- Multilevel Queue Scheduling
- Context Switching
- Operations on Processes
- Lottery Process Scheduling
- Predicting Burst Time SJF Scheduling
- Race Condition Vulnerability
- Critical Section Synchronization
- Mutual Exclusion Synchronization
- Process Control Block
- Inter Process Communication
- Preemptive and Non-Preemptive Scheduling
- Operating System - Deadlock
- Introduction to Deadlock in Operating System
- Conditions for Deadlock in Operating System
- OS Synchronization
- Operating System - Process Synchronization
- Operating System - Critical Section
- Operating System - Semaphores
- Operating System - Counting Semaphores
- Operating System - Mutex
- Operating System - Lock Variable in Process Synchronization
- Operating System - Turn Variable in Process Synchronization
- Operating System - Bounded Buffer Problem
- Operating System - Reader Writer Locks in Process Synchronization
- Operating System - Test Set Lock in Process Synchronization
- Operating System - Peterson Solution in Process Synchronization
- Operating System - Monitors in Process Synchronization
- Operating System - Sleep and Wake in Process Synchronization
- OS Memory Management
- OS - Memory Management
- OS - Virtual Memory
- OS Storage Management
- File Systems in Operating System
- Linked Index Allocation in Operating System
- Indexed Allocation in Operating System
- Structures of Directory in Operating System
- File Attributes in Operating System
- Operating System - Page Replacement
- Operating Systems - Thrashing
- Belady’s Anomaly in Page Replacement Algorithms
- Optimal Page Replacement Algorithm
- Operating System - Types
- Types of Operating System
- Batch Processing Operating System
- Multiprocessing Operating System
- Hybrid Operating System
- Monolithic Operating System
- Zephyr Operating System
- Nix Operating System
- Blackberry Operating System
- Garuda Operating System
- Tails Operating System
- Clustered Operating System
- Haiku Operating System
- AIX Operating System
- Solus Operating system
- Tizen Operating System
- Bharat Operating System
- Fire Operating System
- Bliss Operating System
- VxWorks Operating System
- Embedded Operating System
- Single User Operating System
- OS Miscellaneous
- OS - Multi-threading
- OS - I/O Hardware
- OS - I/O Software
- OS - Security
- OS - Linux
- OS Useful Resources
- OS - Quick Guide
- OS - Useful Resources
- OS - Discussion
Shortest Job First (SJF) Scheduling
A CPU scheduling strategy is a procedure that selects one process in the waiting state and assigns it to the CPU so that it can be executed. There are a number of scheduling algorithms. In this section, we will learn about Shortest Job First or SJF scheduling algorithm.
SJF (SHORTEST JOB FIRST) Scheduling
In the Shortest Job First scheduling algorithm, the processes are scheduled in ascending order of their CPU burst times, i.e. the CPU is allocated to the process with the shortest execution time.
Variants of SJF Scheduling
There are two variants of SJF scheduling −
SJF non-preemptive scheduling
In the non-preemptive version, once a process is assigned to the CPU, it runs into completion. Here, the short term scheduler is invoked when a process completes its execution or when a new process(es) arrives in an empty ready queue.
SJF preemptive scheduling
This is the preemptive version of SJF scheduling and is also referred as Shortest Remaining Time First (SRTF) scheduling algorithm. Here, if a short process enters the ready queue while a longer process is executing, process switch occurs by which the executing process is swapped out to the ready queue while the newly arrived shorter process starts to execute. Thus the short term scheduler is invoked either when a new process arrives in the system or an existing process completes its execution.
Features of SJF Algorithm
- SJF allocates CPU to the process with shortest execution time.
- In cases where two or more processes have the same burst time, arbitration is done among these processes on first come first serve basis.
- There are both preemptive and non-premptive versions.
- It minimises the average waiting time of the processes.
- It may cause starvation of long processes if short processes continue to come in the system.
We can understand the workings of the two versions of this scheduling strategy through the aid of the following examples.
Examples of Non-Preemptive SJF Algorithm
Example 1
Suppose that we have a set of four processes that have arrived at the same time in the order P1, P2, P3 and P4. The burst time in milliseconds of each process is given by the following table −
Process | CPU Burst Time in ms |
---|---|
P1 | 6 |
P2 | 10 |
P3 | 4 |
P4 | 6 |
Let us draw the GANTT chart and find the average turnaround time and average waiting time using non-preemptive SJF algorithm.
GANTT Chart for the set of processes using SJF
Process P3 has the shortest burst time and so it executes first. Then we find that P1 and P4 have equal burst time of 6ms. Since P1 arrived before, CPU is allocated to P1 and then to P4. Finally P2 executes. Thus the order of execution is P3, P1, P4, P2 and is given by the following GANTT chart −

Let us compute the average turnaround time and average waiting time from the above chart.
Average Turnaround Time
=Sum of Turnaround Time of each Process / Number of Processes
= (TATP1 + TATP2 + TATP3 + TATP4) / 4
= (10 + 26 + 4 + 16) / 4 = 14 ms
Average Waiting Time
= Sum of Waiting Time of Each Process / Number of processes
= (WTP1 + WTP2 + WTP3 + WTP4) / 4
= (4 + 16 + 0 + 10) / 4 = 7.5 ms
Example 2
In the previous example, we had assumed that all the processes had arrived at the same time, a situation which is practically impossible. Here, we consider circumstance when the processes arrive at different times. Suppose we have set of four processes whose arrival times and CPU burst times are as follows −
Process | Arrival Time | CPU Burst Time |
---|---|---|
P1 | 0 | 6 |
P2 | 4 | 10 |
P3 | 4 | 4 |
P4 | 8 | 3 |
Let us draw the GANTT chart and find the average turnaround time and average waiting time using non-preemptive SJF algorithm.
GANTT Chart
While drawing the GANTT chart, we will consider which processes have arrived in the system when the scheduler is invoked. At time 0ms, only P1 is there and so it is assigned to CPU. P1 completes execution at 6ms and at that time P2 and P3 have arrived, but not P4. P3 is assigned to CPU since it has the shortest burst time among current processes. P3 completes execution at time 10ms. By that time P4 has arrived and so SJF algorithm is run on the processes P2 and P4. Hence, we find that the order of execution is P1, P3, P4, P2 as shown in the following GANTT chart −

Let us calculate the turnaround time of each process and hence the average.
Turnaround Time of a process = Completion Time Arrival Time
TATP1 = CTP1 - ATP1 = 6 - 0 = 6 ms
TATP2 = CTP2 - ATP2 = 23 - 4 = 19 ms
TATP3 = CTP3 - ATP3 = 10 - 4 = 6 ms
TATP4 = CTP4 - ATP4 = 13 - 8 = 5 ms
Average Turnaround Time
=Sum of Turnaround Time of each Process/ Number of Processes
= (6 + 19+ 6 + 5) / 4 = 9 ms
The waiting time is given by the time that each process waits in the ready queue. For a non-preemptive scheduling algorithm, waiting time of each process can be simply calculated as −
Waiting Time of any process = Time of admission to CPU Arrival Time
WTP1 = 0 - 0 = 0 ms
WTP2 = 13 - 4 = 9 ms
WTP3 = 6 - 4 = 2 ms
WTP4 = 10 - 8 = 2 ms
Average Waiting Time
= Sum of Waiting Time of Each Process/ Number of processes
= (WTP1 + WTP2 + WTP3 + WTP4) / 4
= (0 + 9 + 2 + 2) / 4 = 3.25 ms
Example of Preemptive SJF (SRTF) algorithm
Let us now perform preemptive SJF (SRTN) scheduling on the following processes, draw GANTT chart and find the average turnaround time and average waiting time.
Process | Arrival Time | CPU Burst Time |
---|---|---|
P1 | 0 | 8 |
P2 | 4 | 10 |
P3 | 4 | 3 |
P4 | 10 | 4 |
GANTT Chart
Since this is a preemptive scheduling algorithm, the scheduler is invoked when a process arrives and when it completes execution. The scheduler computes the remaining time of completion for each of the processes in the system and selects the process having shortest remaining time left for execution.

Initially, only P1 arrives and so it is assigned to CPU. At time 4ms, P2 and P3 arrive. The scheduler computes the remaining time of the processes P1, P2 and P3 as 4ms, 10ms and 3ms. Since, P3 has shortest time, P1 is pre-empted by P3. P3 completes execution at 7ms and the scheduler is invoked. Among the processes in the system, P1 has shortest time and so it executes. At time 10ms, P4 arrives and the scheduler again computes the remaining times left for each process. Since the remaining time of P1 is least, no process switch occurs and P1 continues to execute. In the similar fashion, the rest of the processes complete execution.
From the GANTT chart, we compute the average turnaround time and the average waiting time.
Average Turnaround Time
=Sum of Turnaround Time of each Process/ Number of Processes
= (TATP1 + TATP2 + TATP3 + TATP4) / 4
= ((11 - 0) + (25 - 4) + (7 - 4) + (15 - 10)) / 4 = 10 ms
Average Waiting Time
= Sum of Waiting Time of Each Process/ Number of processes
= (WTP1 + WTP2 + WTP3 + WTP4) / 4
= (3 + 11 + 0 + 1) / 4 = 3.75 ms
Advantages of SJF Algorithm
- In both preemptive and non-preemptive methods, the average waiting time is reduced substantially in SJF when compared to FCFS scheduling.
- SJF optimizes turnaround time to a considerable degree.
- If execution time of each process is estimated precisely, it promises maximum throughput.
Disadvantages of SJF Algorithm
- In situations where there is an incoming stream of processes with short burst times, longer processes in the system may be waiting in the ready queue indefinitely leading to starvation.
- In preemptive SJF, i.e. SRTF, if all processes arrive at different times and at frequent intervals, the scheduler may be always working and consequently the processor may be more engaged in process switching than actual execution of the processes.
- Correct estimation of the burst time a process is a complicated process. Since the effectiveness of the algorithm is entirely based upon the burst times, an erroneous calculation may cause inefficient scheduling.
Conclusion
Shortest Job First scheduling can be termed as the optimal scheduling algorithm due to its theoretical best results. However, the implementation is much more complex and the execution is more unpredictable than First Come First Serve or Round Robin scheduling.