Skip to content
CurriculumLesson 11 · CPU Scheduling
Advanced~16 min

SRTF (Preemptive SJF)

SRTF is SJF with a stopwatch. When a shorter job arrives, the running job is paused and the shorter one takes the CPU.

In plain English

SRTF is SJF with a pause button. If a shorter job arrives, it goes first.

By the end you will be able to

  • Trace SRTF step by step using remaining times.
  • Explain when preemption occurs.
  • Compare SRTF with non-preemptive SJF.
  • Recognize the starvation risk.
01

The algorithm

At every arrival or completion, compare the remaining time of the running process with the burst time of the newly arrived process. If the newcomer is shorter, preempt. Otherwise continue. If the CPU is idle, start the shortest remaining job. SRTF always runs the process with the smallest remaining time among those that have arrived.

lesson-11.osOS
time = 0;
while unfinished:
    ready = arrived unfinished processes
    p = smallest remaining time in ready
    run p for one unit
    decrement p.remaining
    if p.remaining == 0: complete p
02

Worked example

Processes: P1 arrival 0 burst 7, P2 arrival 2 burst 4, P3 arrival 4 burst 1. At 0, only P1 is ready, so P1 runs. At 2, P2 arrives with burst 4 while P1 has 5 remaining; P2 is shorter, so P2 preempts and runs 2–4. At 4, P3 arrives with burst 1 while P2 has 2 remaining; P3 preempts and runs 4–5. At 5, P2 has 2 remaining and P1 has 5; P2 runs 5–7. At 7, P1 runs 7–12. Completion: P1 12, P2 7, P3 5. Waiting: P1 5, P2 1, P3 0. Average waiting = 2.0.

lesson-11.osOS
| P1 | P2 |P3| P2 | P1      |
0    2    4  5    7         12

P1: completion 12, wait 5
P2: completion 7,  wait 1
P3: completion 5,  wait 0
03

SJF versus SRTF

Non-preemptive SJF would let P1 finish to 7 before running P2, producing a higher average waiting time. SRTF is more responsive to short arrivals but causes more context switches and can starve long jobs. Both need burst estimates. SRTF is usually better for average waiting time when arrivals are unpredictable and short jobs matter.

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 5

t = 0 → 2 · P1

At t=0, P1 has the shortest remaining time, so it runs until t=2.

Gantt chartSRTF
P1
P2
P3
P2
P1
012
PIDArrBurstRemStateDone
P1075running—
P2244ready—
P3411not arrived—
ready at t=2:P1P2

Key takeaways

  • SRTF is preemptive SJF.
  • Preempt when a newly arrived process has a shorter remaining time.
  • SRTF can reduce average waiting time compared with SJF.
  • It causes more context switches and can starve long jobs.

Common pitfalls

  • Comparing total burst instead of remaining time.
  • Forgetting to account for the running process's remaining time at an arrival.
  • Preempting when the newcomer is longer.

Kernel notes

  • Maintain a remaining-time column for every process.
  • Make a decision at every arrival and completion.
  • Compare the final averages with SJF.

Quick check

0 / 2 answered

1. When does SRTF preempt the running process?

2. What is the main cost of SRTF compared with SJF?