Shortest Job First (SJF) is an optimal scheduling algorithm as it gives maximum Throughput and minimum average waiting time (WT) and turnaround time (TAT) but it is not practically implementable because the burst time of a process can't be predicted in advance.
We may not know the length of the next CPU burst but we may be able to predict its value. We expect the next CPU burst will be similar in length to the previous ones. By computing an approximation of the length of the next CPU burst, we can pick the process with the shortest predicted CPU burst.
SJF is a CPU scheduling algorithm that selects the process with the smallest next CPU burst time. Since actual burst times are not known in advance, we predict them using past behavior.
Methods to Predict Burst Time
There are two methods by which we can predict the burst time of the process:

Static Method
We can predict the burst time by two factors using the static method:
Process size: Let's say we have Process Pold having size 200 KB which is already executed and its Burst-time is 20 Units of time, now let us say we have a New Process Pnew having size 201 KB which is yet to be executed.
We take the Burst Time of the already executed process Pold which is almost of the same size as that of the New process as the Burst Time of the New Process Pnew.
Process type: We can predict burst time depending on the Type of Process.
Operating System processes (like scheduler, dispatcher, segmentation, and fragmentation) are faster than User processes (Gaming, application software). Burst Time for any New OS process can be predicted from any old OS process of similar type and the same for the User process.
- OS Processes : These include system-level processes such as:
- Schedulers
- Compilers
- Program managers
Burst Time: Typically short, around 3 to 5 units, since these processes are small, frequent, and fast.
- User Processes : User processes are initiated by users and are categorized based on interaction and resource usage:
- Interactive Processes : Short CPU bursts due to frequent waiting for user actions.
- Foreground Processes: Moderate burst time balanced between CPU and I/O operations.
- Background Processes: Longer, since they often perform heavy or ongoing computations.
Dynamic Technique
Simple average: Given n processes ( P1, P2... Pn)
Τn+1 = (Σi=1 to n ti).1/n
Exponential average (Aging):
Τn+1 = αtn + (1 - α)Τn
- where α = is smoothing factor and 0 <= α <= 1 , tn = actual burst time of nth process, Τn = predicted burst time of nth process. General term,
αtn + (1 - α)αtn-1 + (1 - α)2αtn-2...+ (1 - α)jαtn-j...+ (1 - α)n+1Τ0
- Τ0 is a constant or overall system average.
- If α = 0, Τn+1 = Τn i.e. no change in value of initial predicted burst time.
- If α = 1, Τn+1 = tn i.e. predicted Burst Time of new process will always change according to the actual Burst-time of nth process.
- If α = 1/2, recent and past history are equally weighted.
How SJF with Prediction Works
- Initialize: Each process starts with an initial predicted burst time Τ0 .
- Monitor Actual Burst: After each execution, record actual burst tn .
- Update Prediction: Use exponential averaging to calculate Τn+1 .
- Schedule: Select the process with the smallest T .
Example :
| Process | Last Actual Burst (tn) | Last Predicted (Τn ) | New Predicted (Τn+1 = αtn + (1 - α)Τn) |
|---|---|---|---|
| P1 | 6 | 5 | 0.5⋅6 + 0.5⋅5 = 5.5 |
| P2 | 2 | 3 | 0.5⋅2 + 0.5⋅3 = 2.5 |
SJF chooses P2 since Τn+1 = 2.5 < 5.5 (P1).
Additional Considerations for SJF Scheduling Algorithm
- The SJF algorithm is useful in scenarios where the processes have varying burst times and there is a need to minimize the average waiting time of processes in the ready queue.
- One of the major drawbacks of the SJF algorithm is that it can lead to starvation for processes with longer burst times, as they may never get a chance to execute if shorter processes keep arriving.
- Preemptive SJF can address the issue of starvation by allowing processes with shorter burst times to be interrupted by processes with even shorter burst times.
- Predicting the burst time accurately can be challenging, especially for processes that have burst times that vary significantly over time.
Advantages
- Minimizes average waiting time.
- Efficient for batch systems where CPU bursts are predictable.
Disadvantages
- Starvation: Long jobs may wait indefinitely.
- Requires accurate burst time prediction.
- Hard to implement in real-time systems.