CPU Scheduling

shortest-remaining-time-first scheduling

/ SRTF /

Shortest-remaining-time-first is shortest-job-first with the gloves off: it is the preemptive version. The idea is the same — favor the job that has the least work left — but now the OS is allowed to interrupt a running job the instant a new arrival has a shorter remaining time, snatching the CPU away to the shorter newcomer.

How it plays out: at every moment, and especially whenever a new process arrives, the scheduler compares the remaining CPU-burst time of the running process against the new one. If the new process needs less time to finish than the current one has left, the current process is preempted and the new one runs. Because preemption can happen at any arrival, a long job that was running can be paused repeatedly as a stream of short jobs comes in. SRTF gives the lowest possible average waiting time of any scheduler — even better than non-preemptive SJF — because preemption lets a short late arrival cut ahead.

The same two problems as SJF, sharpened. It still needs to predict each burst length (via exponential averaging), since remaining time is just future burst time. And starvation is worse: a long job can be preempted again and again and may never finish if short jobs keep arriving. There is also more overhead, because every preemption is a context switch. Like SJF, it is more a theoretical ideal and teaching tool than a literal production scheduler.

P1 (burst 8) starts at time 0. At time 1 a new job P2 (burst 4) arrives. Since 4 is less than P1's remaining 7, SRTF preempts P1 and runs P2. P1 only resumes once shorter jobs are done — giving a lower average waiting time than letting P1 finish first.

SRTF preempts the moment a shorter job arrives, achieving the theoretical minimum average waiting time.

SRTF's optimality assumes you know remaining times and ignores switch overhead; in reality the prediction is approximate and the relentless preemption can starve long jobs more severely than non-preemptive SJF.

Also called
SRTFpreemptive SJF搶佔式最短工作優先