Shortest job first

Also known as: SJF · shortest job next

On this page 7

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.

PropertyNon-preemptive SJFShortest remaining time first
Decision pointOnly when the running burst endsAlso whenever a new process arrives
Average waiting timeOptimal among non-preemptive policiesOptimal among all policies
Context switchesFewerMore, because of preemption
SuitabilityBatch jobs with known run timesSystems with frequent arrivals
Non-preemptive and preemptive forms of the policy

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.

MetricFirst-come, first-servedShortest job first
Execution orderA, B, C, DB, D, C, A
Individual waiting times0, 7, 9 and 14 ms0, 2, 6 and 11 ms
Average waiting time7.5 ms4.75 ms
Average turnaround time12 ms9.25 ms
Waiting and turnaround times for four jobs arriving together

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 alphaPrediction behaviorPractical consequence
Alpha close to 0Relies almost entirely on historySlow to react when a process changes phase
Alpha of 0.5Balances recent and past burstsReasonable default for mixed workloads
Alpha close to 1Follows only the last burstReacts fast but amplifies random variation
Effect of the smoothing factor on burst prediction

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 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.

PolicySelection ruleStarvation riskMain strength
Shortest job firstShortest predicted burstHigh for long jobsMinimal average waiting time
First-come, first-servedArrival orderNoneSimplicity and predictability
Round-robin schedulingFixed time slice in rotationNoneGood response time for interactive work
Multilevel feedback queueDemotes processes that use full slicesPossible without agingApproximates SJF without prior knowledge
Highest response ratio nextWaiting plus burst divided by burstNoneFavors short jobs while aging long ones
Shortest job first and neighboring policies

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.