Sub topic 13
Shortest Job First and Shortest Remaining Time First
This chapter assumes you know the scheduling criteria (waiting, turnaround and response time) and how First-Come First-Served builds a Gantt chart. You should also be comfortable with a ready queue and simple arithmetic on time intervals.
The idea
Shortest Job First (SJF) always gives the CPU to the ready process with the smallest next CPU burst. A CPU burst is the time a process runs before it blocks for I/O or finishes. SJF comes in two forms:
- Non-preemptive SJF lets the chosen process run until its burst ends. The scheduler decides again only when the CPU becomes free.
- Shortest Remaining Time First (SRTF) is the preemptive form. Whenever a process arrives, the scheduler compares the new burst with the remaining time of the running process. If the newcomer is shorter, it takes the CPU.
Short jobs leave the system quickly, so they stop adding to everyone else's waiting time. This is why SJF gives the minimum average waiting time among all non-preemptive algorithms for a fixed set of jobs. SRTF gives the minimum among all algorithms, preemptive ones included.
Decision rules
Two rules matter in practice:
- Compare against the remaining time of the running process, not its original burst.
- Break ties by earliest arrival, then by lowest process number, unless the question says otherwise. GATE questions usually state a tie rule or avoid ties.
Worked example
| Process | Arrival | Burst |
|---|---|---|
| P1 | 0 | 8 |
| P2 | 1 | 4 |
| P3 | 2 | 9 |
| P4 | 3 | 5 |
Non-preemptive SJF
At time 0 only P1 is ready, so it runs to time 8. At 8 the ready set is P2 (4), P3 (9) and P4 (5), so the order is P2, P4, P3.
Gantt chart: P1 [0–8], P2 [8–12], P4 [12–17], P3 [17–26].
| Process | Completion | Turnaround (C − A) | Waiting (TAT − burst) |
|---|---|---|---|
| P1 | 8 | 8 | 0 |
| P2 | 12 | 11 | 7 |
| P3 | 26 | 24 | 15 |
| P4 | 17 | 14 | 9 |
Average waiting time is 31/4 = 7.75. Without preemption, response time equals waiting time, because a process starts only once and then runs to the end.
SRTF
- At t = 1, P2 arrives with burst 4. P1 has 7 left, so P2 preempts it.
- At t = 2, P3 arrives with 9. P2 has 3 left, so P2 continues.
- At t = 3, P4 arrives with 5. P2 has 2 left, so P2 continues.
- P2 finishes at 5. The ready set is P1 (7 left), P3 (9) and P4 (5), so P4 runs.
- P4 finishes at 10, then P1 runs to 17, then P3 runs to 26.
Gantt chart: P1 [0–1], P2 [1–5], P4 [5–10], P1 [10–17], P3 [17–26].
| Process | Completion | Turnaround | Waiting | Response (first run − A) |
|---|---|---|---|---|
| P1 | 17 | 17 | 9 | 0 |
| P2 | 5 | 4 | 0 | 0 |
| P3 | 26 | 24 | 15 | 15 |
| P4 | 10 | 7 | 2 | 2 |
Average waiting time is 26/4 = 6.5, which beats the 7.75 from non-preemptive SJF. Average response time is 17/4 = 4.25.
Simulating both in code
This program steps one time unit at a time. It handles idle gaps, and it reproduces both tables above.
def sjf(procs, preemptive):
"""procs: list of (arrival, burst). Returns finish, turnaround, waiting, response."""
n = len(procs)
rem = [b for _, b in procs]
first = [None] * n
finish = [0] * n
t, done, current = 0, 0, None
while done < n:
if current is None or preemptive:
ready = [i for i in range(n) if procs[i][0] <= t and rem[i] > 0]
if not ready:
t += 1
continue
current = min(ready, key=lambda k: (rem[k], procs[k][0], k))
i = current
if first[i] is None:
first[i] = t
rem[i] -= 1
t += 1
if rem[i] == 0:
finish[i] = t
done += 1
current = None
tat = [finish[i] - procs[i][0] for i in range(n)]
wt = [tat[i] - procs[i][1] for i in range(n)]
resp = [first[i] - procs[i][0] for i in range(n)]
return finish, tat, wt, resp
procs = [(0, 8), (1, 4), (2, 9), (3, 5)]
print(sjf(procs, preemptive=False))
# ([8, 12, 26, 17], [8, 11, 24, 14], [0, 7, 15, 9], [0, 7, 15, 9])
print(sjf(procs, preemptive=True))
# ([17, 5, 26, 10], [17, 4, 24, 7], [9, 0, 15, 2], [0, 0, 15, 2])In the non-preemptive case current stays set until the process finishes, so the ready set is never re-examined mid-burst. In the preemptive case the choice is remade every time unit, which is equivalent to checking at each arrival.
Predicting the next burst
A kernel cannot see the future, so it estimates the next burst with an exponential average:
τ(n+1) = α · t(n) + (1 − α) · τ(n)
Here t(n) is the measured length of the latest burst, τ(n) is the previous prediction, and 0 ≤ α ≤ 1. A larger α trusts recent behaviour more. With α = 0 the prediction never changes. With α = 1 only the last burst counts. A common choice is α = 1/2.
Trade-offs and common errors
- Starvation. A steady stream of short jobs can keep a long job waiting forever. SRTF makes this worse, because a long job that has already run a lot is still "long" when measured by remaining time only if it has not yet shrunk. Aging, covered in the priority scheduling chapter, is the usual fix.
- Burst-length knowledge. Prediction errors reduce the benefit. The algorithm is optimal for the true bursts, not for the estimates.
- Context-switch overhead. SRTF switches more often than SJF. GATE questions normally ignore switch cost unless they give it. If a question counts context switches, count each preemption and each completion that hands the CPU to another process.
- Mistake: choosing by original burst under SRTF. Always use the remaining time.
- Mistake: forgetting idle time. If no process has arrived, the CPU idles. Time passes, but nobody is waiting.
- Mistake: using waiting time for response time. They are equal only when a process runs once without interruption. Under SRTF, response time can be smaller than waiting time.
- Mistake: assuming a newcomer with an equal remaining time preempts. It does not, because the comparison is strict "shorter than".
Comparison
| Property | FCFS | Non-preemptive SJF | SRTF |
|---|---|---|---|
| Decision points | CPU free | CPU free | CPU free and each arrival |
| Preemptive | No | No | Yes |
| Minimizes average waiting time | No | Among non-preemptive | Yes, overall |
| Convoy effect | Yes | Reduced | Reduced |
| Starvation possible | No | Yes | Yes |
| Needs burst estimates | No | Yes | Yes |
Exercise
Four processes have (arrival, burst) values P1 (0, 7), P2 (2, 4), P3 (4, 1), P4 (5, 4). Under SRTF, what is the average waiting time?
Show answer
Gantt chart: P1 [0–2], P2 [2–4], P3 [4–5], P2 [5–7], P4 [7–11], P1 [11–16]. At t = 2, P2 (4) is shorter than P1's remaining 5, so it preempts. At t = 4, P3 (1) is shorter than P2's remaining 2, so it preempts. At t = 5, P2 has 2 left, which is smaller than P4's 4 and P1's 5. Completion times are P1 = 16, P2 = 7, P3 = 5, P4 = 11. Turnaround times are 16, 5, 1, 6. Waiting times (turnaround − burst) are 9, 1, 0, 2. The average is 12/4 = 3.0.
Exercise
A process has a predicted burst τ(0) = 10 and α = 0.5. Its first actual burst is 6, and its second is 4. What are the next two predictions?
Show answer
τ(1) = 0.5 · 6 + 0.5 · 10 = 8. Then τ(2) = 0.5 · 4 + 0.5 · 8 = 6.
Exercise
Why can non-preemptive SJF give a worse average waiting time than SRTF on the same input, and which one starves long jobs more easily?
Show answer
Non-preemptive SJF cannot interrupt a long job that started just before a short job arrived, so the short job waits for the whole long burst. SRTF interrupts the long job and runs the short one at once, which lowers the average. Both can starve long jobs, but SRTF does it more easily, because a newly arrived short process takes the CPU immediately instead of waiting for the current burst to end.