SJF Scheduling
Shortest Job First is the policy that proves why scheduling matters: it can reduce average waiting time dramatically, but only if you know how long each job will run.
In plain English
SJF serves the shortest order first, like an express lane for small baskets.
By the end you will be able to
- Trace non-preemptive SJF with arrival times.
- Choose the shortest ready burst at each decision point.
- Compute the average waiting time.
- Explain why SJF can starve long jobs.
The algorithm
At each decision point, look at all processes that have arrived and are not finished. Choose the one with the smallest burst. Run it to completion. If no process has arrived, advance the clock to the next arrival. Non-preemptive SJF does not interrupt a running process even if a shorter job arrives later.
time = 0;
while unfinished:
ready = arrived processes
if ready empty:
time = next arrival
continue
p = shortest burst in ready
start = time
time = time + p.burst
p.completion = timeWorked example
Processes: P1 arrival 0 burst 7, P2 arrival 2 burst 4, P3 arrival 4 burst 1, P4 arrival 5 burst 4. At time 0 only P1 is ready, so P1 runs 0–7. At time 7, P2, P3, and P4 are ready. P3 has the shortest burst (1), so P3 runs 7–8. Then P2 (4) runs 8–12, and P4 (4) runs 12–16. Completion: 7, 12, 8, 16. Waiting: 0, 6, 3, 7. Average waiting = 4.0.
| P1 | P3 | P2 | P4 |
0 7 8 12 16
P1: wait 0, turnaround 7
P2: wait 6, turnaround 10
P3: wait 3, turnaround 4
P4: wait 7, turnaround 11Optimality and starvation
When all processes arrive at the same time, SJF minimizes average waiting time. With arrivals, non-preemptive SJF is not always optimal; preemptive SRTF can do better. SJF also needs to know the burst time in advance, which real systems estimate from history. A stream of short jobs can starve a long job forever unless the scheduler uses aging.
Watch it happen
Step through the worked example. Play it, pause it, or move one step at a time.
step-by-step
Step 1 of 4
t = 0 → 7 · P1
At t=0, P1 has the shortest ready burst, so it runs until t=7.
Key takeaways
- SJF picks the shortest ready burst at each decision point.
- It is non-preemptive by default.
- SJF minimizes average waiting time when all jobs arrive together.
- It requires burst-time estimates.
- Long jobs can starve.
Common pitfalls
- Choosing a process that has not arrived yet.
- Preempting in non-preemptive SJF.
- Forgetting that SJF needs future knowledge or estimates.
Kernel notes
- Trace the ready set at every decision point.
- Compute waiting time from completion minus arrival minus burst.
- Explain why SJF is optimal only under a restricted assumption.
Quick check
1. Which process does SJF choose next?
2. What is the main practical problem with SJF?