Shortest Job First (SJF)
Provably the best average waiting time, if only you knew the future.
Skip to the animationShortest Job First always dispatches the waiting process with the smallest CPU burst, and once started it runs to completion. It provably gives the lowest possible average waiting time — if you know the burst lengths in advance.
How it works
When the CPU frees up, look at every process that has already arrived, and dispatch the one with the smallest burst time. Ties are broken FCFS. This version is non-preemptive: a process that gets the CPU keeps it even if a shorter job shows up a moment later. (The preemptive version is a separate topic — SRTF.)
“Already arrived” is the rule that decides most exam answers. SJF cannot pick a process that has not shown up yet, however short it is.
Why it is optimal
The claim is precise: for a fixed set of processes available at the same time, SJF minimises average waiting time. The argument is a swap argument. Suppose a long job runs before a short one. Swapping them lowers the short job's waiting time by the long job's burst, and raises the long job's by the short one's — a smaller amount. Every such swap improves the average, so the sorted-by-burst order must be the best one.
Concretely, four processes all arriving at t = 0 with bursts 6, 8, 7 and 3:
| Order | Schedule | Average waiting |
|---|---|---|
| FCFS (P1, P2, P3, P4) | 6, 8, 7, 3 | 10.25 |
| SJF (P4, P1, P3, P2) | 3, 6, 7, 8 | 7.00 |
The catch: you cannot know the future
A scheduler does not know how long the next burst will be. So real systems predict it from history, on the assumption that a process which has been short tends to stay short. The standard method is an exponential moving average:
τ(n+1) = α · t(n) + (1 − α) · τ(n) — where t(n) is the burst just observed, τ(n) was the previous prediction, and α (commonly 0.5) sets how heavily recent behaviour counts. α = 0 ignores history's latest entry entirely; α = 1 predicts purely from the last burst.
The vocabulary
- Arrival time
- When the process shows up in the ready queue. The scheduler cannot pick something that has not arrived yet — that single rule decides most exam answers.
- Burst time
- How long this process needs the CPU for, uninterrupted, before it would block or finish. Also called CPU burst.
- Ready queue
- Every process that could run right now but does not have the CPU. Scheduling is entirely the question of who gets pulled out of here next.
- Preemptive vs non-preemptive
- A non-preemptive scheduler cannot take the CPU back — once dispatched, a process runs until it blocks or finishes. A preemptive one can interrupt it mid-burst.
- Context switch
- Saving one process's registers and loading another's. Pure overhead: real work stops while it happens, typically a few microseconds.
- Starvation
- A process that stays runnable but never gets picked, because something more attractive keeps arriving. The classic fix is ageing — slowly raising the priority of anything that has waited a long time.
Starvation
If short jobs keep arriving, a long job can sit in the ready queue indefinitely — it is never the shortest. It is not deadlocked and nothing is broken; it is simply never chosen. Systems that use burst-based scheduling in practice add ageing so waiting time eventually beats shortness.
The numbers you will be asked for
- Turnaround time
completion − arrival
Total time the process spent in the system, from the moment it arrived to the moment it finished.
- Waiting time
turnaround − burst
The part of that which was pure waiting. It is the only part a scheduler can actually change — the burst is fixed work.
- Response time
first run − arrival
How long until the process gets the CPU for the first time. This is what an interactive user actually feels.
- Throughput
processes ÷ total time
How much work the machine completes per unit of time.
Advantages and disadvantages
Advantages
- Provably the minimum average waiting time for a given set of processes.
- Favours short interactive jobs, which is usually what users notice.
- Non-preemptive, so context switch overhead stays low.
Disadvantages
- Requires knowing the next burst length, which is not knowable — only estimable.
- Long processes can starve when short ones keep arriving.
- Poor response time for a long job that has already been passed over repeatedly.
- Once dispatched it cannot be interrupted, so a much shorter arrival still waits.
Watch it work
Check yourself
question 1 / 4
One question at a time. Pick an answer to see why it is right or wrong, then move on — there is no score to keep and nothing is saved.