Shortest job first (SJF), also called shortest job next, is a CPU scheduling algorithm that selects the ready process with the smallest expected CPU burst and runs it next. Within processes & scheduling in operating systems, it is the policy that provably minimizes average waiting time for a given set of jobs, which makes it the theoretical benchmark against which practical schedulers are measured.
Its weakness is equally fundamental: the scheduler cannot know burst lengths in advance, so real systems either rely on user estimates or predict bursts from past behavior. SJF can also cause starvation of long processes, which is why it is rarely used in pure form outside batch environments.
How shortest job first selects the next process
SJF orders the run queue by predicted burst length and dispatches the process at the head whenever the CPU becomes free. Ties are usually broken by arrival order, so the policy falls back to first-come, first-served behavior among equal jobs.
The algorithm exists in two variants. In non-preemptive SJF, a running process keeps the CPU until its burst ends, even if a shorter job arrives. In the preemptive variant, known as shortest remaining time first (SRTF), a newly arrived process preempts the current one when its burst is shorter than the time the current process still needs.
| Property | Non-preemptive SJF | Shortest remaining time first |
|---|---|---|
| Decision point | Only when the running burst ends | Also whenever a new process arrives |
| Average waiting time | Optimal among non-preemptive policies | Optimal among all policies |
| Context switches | Fewer | More, because of preemption |
| Suitability | Batch jobs with known run times | Systems with frequent arrivals |
Why SJF minimizes average waiting time
SJF is optimal because moving a short job ahead of a long one reduces the waiting time of the short job by more than it increases the waiting time of the long job. An exchange argument proves this: in any schedule where a longer job precedes a shorter adjacent one, swapping them lowers the total waiting time, so the sorted order is the minimum.
Consider four processes arriving together at time zero with bursts of 7, 2, 5 and 4 milliseconds, listed in arrival order as A, B, C and D. The table compares first-come, first-served order with SJF order.
| Metric | First-come, first-served | Shortest job first |
|---|---|---|
| Execution order | A, B, C, D | B, D, C, A |
| Individual waiting times | 0, 7, 9 and 14 ms | 0, 2, 6 and 11 ms |
| Average waiting time | 7.5 ms | 4.75 ms |
| Average turnaround time | 12 ms | 9.25 ms |
The total work is identical in both schedules, so CPU utilization and throughput do not change. Only the order differs, yet the average waiting time drops by more than a third, which shows how strongly ordering affects responsiveness.
Predicting the next CPU burst
Because a short-term scheduler cannot see the future, practical SJF uses exponential averaging to predict the next burst from the measured length of previous bursts. The estimate is updated as tau_next = alpha * t_last + (1 - alpha) * tau_prev, where alpha lies between 0 and 1.
The smoothing factor alpha controls how quickly the prediction follows changes in behavior. A common choice is alpha of 0.5, which weights the latest burst and the accumulated history equally. For example, with an initial estimate of 10 ms and a measured burst of 6 ms, the next prediction becomes 8 ms.
| Value of alpha | Prediction behavior | Practical consequence |
|---|---|---|
| Alpha close to 0 | Relies almost entirely on history | Slow to react when a process changes phase |
| Alpha of 0.5 | Balances recent and past bursts | Reasonable default for mixed workloads |
| Alpha close to 1 | Follows only the last burst | Reacts fast but amplifies random variation |
In batch systems, the estimate often comes from the user-declared run time limit instead. This creates an incentive problem: users who underestimate gain priority, so batch schedulers typically terminate jobs that exceed their declared limit.
SJF compared with related scheduling policies
SJF can be viewed as a special case of priority scheduling in which priority is the inverse of the predicted burst length. Other policies trade some of its waiting-time optimality for fairness, predictability or responsiveness.
| Policy | Selection rule | Starvation risk | Main strength |
|---|---|---|---|
| Shortest job first | Shortest predicted burst | High for long jobs | Minimal average waiting time |
| First-come, first-served | Arrival order | None | Simplicity and predictability |
| Round-robin scheduling | Fixed time slice in rotation | None | Good response time for interactive work |
| Multilevel feedback queue | Demotes processes that use full slices | Possible without aging | Approximates SJF without prior knowledge |
| Highest response ratio next | Waiting plus burst divided by burst | None | Favors short jobs while aging long ones |
The multilevel feedback queue is the most common practical descendant of SJF. It observes behavior instead of predicting it: I/O-bound processes with short bursts stay in high-priority queues, while CPU-bound processes sink to lower levels, which approximates shortest-first ordering automatically.
Starvation and other limitations
The central risk of SJF is indefinite postponement of long jobs. If short processes keep arriving, a long process may never reach the head of the queue, a failure mode known as starvation.
- Aging raises the effective priority of a process the longer it waits, which guarantees that long jobs eventually run.
- Inaccurate predictions reduce the benefit, and a burst that is much longer than predicted can delay every job queued behind it.
- SRTF overhead grows with frequent arrivals, because each preemption costs a context switch and may disturb cache locality.
- Variance of response time increases for long jobs, so SJF suits throughput-oriented batch work better than interactive desktops.
Understanding it simply
Imagine a supermarket checkout where the cashier always serves the customer with the fewest items first. People with one or two items leave almost immediately, and the average time spent waiting in line drops. That is the idea behind shortest job first: the computer gives the processor to the quickest task.
The catch is that a customer with a full cart could wait forever if quick shoppers keep arriving. Computers solve this by slowly raising the priority of anyone who waits too long, so every task eventually gets its turn.
Applied in practice
A university computing cluster runs a batch scheduler for several hundred researchers. Each job submission must declare a maximum wall-clock time, and the scheduler uses that value as the burst estimate. Jobs declaring 30 minutes are generally started ahead of jobs requesting 48 hours, and a backfilling step slots short jobs into idle nodes while large jobs wait for enough free resources.
To prevent starvation, the scheduler adds a priority bonus for time spent in the queue, so a 48-hour simulation submitted on Monday is guaranteed to start within a predictable period. Jobs that exceed their declared limit are terminated, which keeps estimates honest. The result is a shorter median queue time for the many small analysis jobs, while long simulations still complete reliably.