SJF Scheduling: Shortest Job First
Shortest Job First (SJF), also called Shortest Process Next, chooses the smallest CPU burst among processes that are ready when the CPU becomes free. After a process starts, this nonpreemptive policy lets it finish.
The rule
The word ready is essential. A scheduler cannot choose a process that has not arrived yet. A shorter arrival also cannot take the CPU away from a process already running under SJF.
Worked example
| Process | Arrival | Burst |
|---|---|---|
| P1 | 0 | 7 |
| P2 | 1 | 3 |
| P3 | 2 | 1 |
One CPU · One burst per process · No I/O · Dispatch cost 0. Times are abstract units.
P1 runs from 0 to 7. Jobs that arrive during its execution cannot interrupt it.
At 7, P3 has burst 1 versus 3 for P2. The shorter ready job wins. The full CPU trace is P1 [0, 7), P3 [7, 8), P2 [8, 11).
| Candidate | Burst | Decision |
|---|---|---|
| P3 | 1 | Selected after P1 completes |
| P2 | 3 | Waits for P3 |
- P1: [0, 7)
- P3: [7, 8)
- P2: [8, 11)
Calculate the result
| Process | Completion | Waiting | Turnaround | Response |
|---|---|---|---|---|
| P1 | 7 | 0 | 7 | 0 |
| P2 | 11 | 7 | 10 | 7 |
| P3 | 8 | 5 | 6 | 5 |
P3: turnaround = 8 − 2 = 6. Waiting = 6 − 1 = 5. First response = 7 − 2 = 5.
P3 waits 3 units less than with FCFS. P2 waits 1 unit longer. A better average need not help every process.
Mean waiting = (0 + 7 + 5) / 3 = 4 units. Mean response = 4 units.
What SJF assumes
Choosing the shortest burst requires knowing or estimating its length. The simulator uses the burst values you enter. With a continuing supply of short work, a long ready job may be delayed.
Do not apply the phrase "shortest job first" to the entire input table as if every process were available at time 0. Also distinguish SJF from SRTF: SRTF can interrupt current execution, while this SJF policy cannot.