SJF Scheduling — Step-by-Step With Animated Numericals
Press Next → or use ← → arrow keys
The Story That Explains SJF
The Two Flavours of SJF
CT = start + burst, TAT = CT − AT, WT = TAT − BT,
RT = start − AT. Only the selection rule changes — shortest burst, not arrival order.
SJF Algorithm — Six Steps
The preemptive version re-runs the selection at every arrival, comparing remaining times instead of full bursts — so a short newcomer can snatch the CPU from a longer running job.
Basic SJF — All Arrive at t = 0
| Order | Proc | AT | BT | Start | CT | TAT | WT |
|---|---|---|---|---|---|---|---|
| 1 | P3 | 0 | 1 | 0 | 1 | 1 | 0 |
| 2 | P2 | 0 | 4 | 1 | 5 | 5 | 1 |
| 3 | P4 | 0 | 4 | 5 | 9 | 9 | 5 |
| 4 | P1 | 0 | 7 | 9 | 16 | 16 | 9 |
| Averages → | 7.75 | 3.75 | |||||
Sorting by burst (1, 4, 4, 7) runs the tiny P3 first, so short jobs stop waiting behind the long one. Avg WT = 3.75 ms versus FCFS's 7.50 ms on the same data — a 50% improvement.
Non-Preemptive SJF, Different Arrivals
| Order | Proc | AT | BT | Start | CT | TAT | WT |
|---|---|---|---|---|---|---|---|
| 1 | P1 | 0 | 6 | 0 | 6 | 6 | 0 |
| 2 | P4 | 3 | 3 | 6 | 9 | 6 | 3 |
| 3 | P3 | 2 | 7 | 9 | 16 | 14 | 7 |
| 4 | P2 | 1 | 8 | 16 | 24 | 23 | 15 |
| Averages → | 12.25 | 6.25 | |||||
Only P1 has arrived at t=0, so it runs 0→6. By t=6 all four are present, and SJF picks by burst: P4·3, then P3·7, then P2·8. Non-preemptive means each finishes before the next choice. Avg WT = 6.25 ms.
Preemptive SJF — Preemption Steals the CPU at t = 1
| Proc | AT | BT | CT | TAT | WT |
|---|---|---|---|---|---|
| P1 | 0 | 8 | 17 | 17 | 9 |
| P2 | 1 | 4 | 5 | 4 | 0 |
| P3 | 2 | 9 | 26 | 24 | 15 |
| P4 | 3 | 5 | 10 | 7 | 2 |
| Averages → | 13.00 | 6.50 | |||
P1 starts, but the instant P2 arrives with a shorter burst it preempts P1. P2 finishes, then P4 (5), then P1 resumes its remaining 7, then P3. SRTF's Avg WT = 6.50 ms beats non-preemptive SJF's 7.75 ms on the very same data.
Preemptive vs Non-Preemptive, Same Data
SRTF gives the globally minimum average waiting time, but it must re-evaluate at every arrival and pays extra context-switch cost. Non-preemptive SJF is cheaper but can't react once a job is running.
Starvation — Watch P1 Never Get to Run
| Proc | AT | BT | Note |
|---|---|---|---|
| P1 | 0 | 20 | Preempted 6×; finishes only at t=30 → TAT 30 |
| P2…P6 | 1,3,5,7,9 | 2 each | Each short job jumps ahead of P1 |
Every time a shorter job arrives, P1 is shoved aside. If short jobs kept coming forever, P1 would starve — never finishing. The cure is aging: gradually raise a waiting process's priority so that, eventually, even a long job must be scheduled.
Predicting the Next CPU Burst
SJF needs to know a job's burst before it runs — impossible in general, so the OS predicts it from history with exponential averaging.
τₙ₊₁ = α · tₙ + (1 − α) · τₙ — the next prediction blends the actual last burst
tₙ with the previous prediction τₙ. A common choice is
α = 0.5, weighting recent history and the running estimate equally.
| Step | Predicted τ | Actual t | Updated τ (α=0.5) |
|---|---|---|---|
| 0 | 10.00 | 6 | 8.00 |
| 1 | 8.00 | 4 | 6.00 |
| 2 | 6.00 | 6 | 6.00 |
| 3 | 6.00 | 4 | 5.00 |
| 4 | 5.00 | 13 | 9.00 |
| 5 | 9.00 | 13 | 11.00 |
| 6 | 11.00 | 13 | 12.00 |
Notice how the prediction glides toward 13 as the actual bursts jump up — smoothing noise while still tracking a real shift in behaviour. That's exactly what you want for scheduling decisions.
SJF vs FCFS — Side by Side
| Property | FCFS | SJF (non-preemptive) | SRTF (preemptive) |
|---|---|---|---|
| Selection | Arrival order | Shortest burst among ready | Shortest remaining time |
| Preemption | No | No | Yes |
| Avg waiting time | Often poor | Optimal (given arrivals) | Globally optimal |
| Starvation risk | None | Yes | Yes |
| Overhead | Very low | Low | High |
| Needs burst prediction | No | Yes | Yes |
| Convoy effect | Severe | Solves it | Solves it |
The very problem that wrecked FCFS — short jobs trapped behind a long one — disappears when you run the shortest job first. The price is needing to predict bursts and risking starvation of long jobs.
Advantages & Disadvantages
Seven Rules for Solving SJF
τₙ₊₁ = α·tₙ + (1−α)·τₙ.Shortest Job First — Mastered
Pick the shortest ready job, draw the Gantt chart, and compute the metrics — and for SRTF, re-check at every arrival. You've seen all four cases: simultaneous arrivals, staggered arrivals, live preemption, and starvation.
Next come Priority Scheduling (with aging to prevent starvation) and Round Robin (fair time-slicing for interactive systems). Compare each one's average waiting time against this SJF baseline.
🛒 End of tutorial · Press ← to review, or click Restart