Start from foundations · Course contents · فارسی
Previous lesson: CPU scheduling from scratch: FCFS, SJF and timelines · Next lesson: Priority and feedback queues: from starvation to MLFQ
Preemptive scheduling: SRTF and Round Robin turns
In the previous lesson, a started job kept the CPU until its execution interval finished. If a short job arrives while a long job runs, must it wait for the long job to finish? If execution lengths are unknown, how can several jobs get opportunities to run? These questions motivate different policies: choosing the least remaining work, and dividing time into bounded turns.
Objective: construct SRTF and Round Robin timelines using remaining work and a ready queue, and distinguish total waiting from initial response. Arrival, Burst, Start, Completion and the previous lesson's metrics are prerequisites. Assume one CPU, independent jobs with one burst each, no I/O, and zero switching cost. Example times use illustrative units. Priority, multilevel and real-time policies remain future topics.
1. Preemption suspends a process; it does not finish it
With preemption, the OS can take the CPU away before a process completes. A runnable suspended process returns to Ready; it is neither terminated nor blocked. State needed for resumption is preserved so it can continue later. A hardware timer interrupt can provide an opportunity for the OS to regain control; every process need not voluntarily surrender the CPU. Distinguish the selection decision from switching overhead: zero overhead is only our current calculation assumption.
Burst values below denote total CPU requirements. After two execution units from an original burst of seven, five remain. Suspension does not reset the requirement to seven.
2. SRTF: compare remaining work
Shortest Remaining Time First (SRTF) selects the least remaining work among the current job and ready jobs. A new arrival can change the choice. You may also see STCF or preemptive SJF. Knowing remaining duration is an important assumption in these exercises; real systems may have only estimates. The OSTEP scheduling chapter presents this distinction using the name STCF.
Our tie rule retains the current job if its remaining time ties for shortest. Otherwise use earlier arrival, then table order. This is an explicit exercise convention, not a universal rule shared by every text.
| Process | Arrival A | Total burst B |
|---|---|---|
| P1 | 0 | 7 |
| P2 | 2 | 3 |
| P3 | 3 | 1 |
Step by step:
- Only P1 exists from 0 to 2. It executes two units, leaving five.
- At 2, P2 arrives needing three. Since
3<5, P1 becomes ready and P2 starts. - At 3, P2 has executed one unit and has two left. P3 arrives needing one. Since
1<2, select P3. - P3 runs from 3 to 4 and finishes. At 4, choose P2 with two remaining over P1 with five. P2 runs from 4 to 6 and finishes.
- Only P1 remains. It executes its remaining five units from 6 to 11.
0–2 P1 | 2–3 P2 | 3–4 P3 | 4–6 P2 | 6–11 P1
| Process | First start S | Completion C | Turnaround C−A | Waiting C−A−B | Response S−A |
|---|---|---|---|---|---|
| P1 | 0 | 11 | 11 | 4 | 0 |
| P2 | 2 | 6 | 4 | 1 | 0 |
| P3 | 3 | 4 | 1 | 0 | 0 |
Mean waiting is (4+1+0)/3=5/3; mean response is zero. P1's zero response does not mean it never waits: after its first two execution units it waits from 2 to 6. P2 waits from 3 to 4. Under the no-I/O model, C-A-B totals these potentially separate ready intervals.
3. Round Robin: at most one time slice per turn
Round Robin (RR) rotates through a ready queue. A quantum limits CPU time per turn. With q=2, a process runs for at most two units. If it finishes sooner, its turn ends immediately. If work remains when its quantum expires, it joins the queue's tail. The policy does not need exact total burst lengths to select a turn; exercise tables provide lengths so we can calculate completions.
A new job joins the waiting queue's tail and does not interrupt the current turn merely by arriving. When arrival coincides with quantum expiration, this lesson enqueues arrivals at that instant before requeuing the unfinished current process. Simultaneous arrivals follow table order. Use a different convention if a question explicitly supplies one.
For the same jobs with q=2:
| Selection time | Queue before selection; head at left | Next execution and why it ends |
|---|---|---|
| 0 | P1 | P1 runs 0–2; quantum expires, five remain |
| 2 | P2, P1 | P2 runs 2–4; quantum expires, one remains |
| 4 | P1, P3, P2 | P1 runs 4–6; quantum expires, three remain |
| 6 | P3, P2, P1 | P3 runs 6–7 and finishes |
| 7 | P2, P1 | P2 runs 7–8 and finishes |
| 8 | P1 | P1 runs 8–10; quantum expires, one remains |
| 10 | P1 | P1 runs 10–11 and finishes |
P3 arrives at 3 while P2 runs and joins behind P1. At 4, before requeuing P2, the waiting queue is [P1,P3]. At 10 no other job waits: selecting P1 again need not switch to a different process. We could draw 8–11 as one continuous interval, but retain the quantum boundary at 10 for tracing.
The diagram compares proportional execution intervals and their order. Tables provide the essential information without the image.
| Process | S | C | Turnaround | Waiting | Response |
|---|---|---|---|---|---|
| P1 | 0 | 11 | 11 | 4 | 0 |
| P2 | 2 | 8 | 6 | 3 | 0 |
| P3 | 6 | 7 | 4 | 3 | 3 |
RR mean waiting is 10/3, mean turnaround is 21/3=7, and mean response is 3/3=1. Total burst length is eleven; without idle time or switching overhead, both policies finish at eleven. This does not establish that SRTF is best for every objective: information about lengths and the chosen metric matter.
4. What does changing the quantum do?
If q in this model is at least the largest burst, each job finishes on its first turn and RR behaves like FCFS. A smaller quantum gives others earlier opportunities in a finite queue but creates more turns. Real switching is not free, so “smaller is always better” is incorrect.
To see the overhead scale, consider a separate model: work is always ready, every slice performs exactly q units of useful execution, and every slice is followed by a switch costing s. The useful fraction of a repeating cycle is q/(q+s). With q=2,s=1, it is two thirds; with q=10,s=1, ten elevenths. This describes that steady-state model. Initial and final boundaries, jobs shorter than a quantum, and selecting the same job again must be examined separately in an actual exercise.
5. Independent practice and test tips
All questions are original. Write the queue or remaining times before opening an answer.
1. At time 3 in the SRTF example, why compare P3 with P2's remaining two units rather than its original burst of three?
Answer 1
P2 executed one unit from 2 to 3. Future selection depends on unfinished work. SRTF compares remaining durations; already completed work is not performed again.
2. A starts at zero with burst five. B arrives at 2 needing three. What does SRTF do under this lesson's tie rule?
Answer 2
A has three units left and B also needs three. Retain the current process on a tie: A finishes at 5, then B runs 5–8. Without a tie convention, the ordering could be ambiguous.
3. Under RR with q=1, A has A=0,B=3; B has A=1,B=1. Enqueue boundary arrivals before requeuing the current job. When does each finish?
Answer 3
A runs 0–1; the queue becomes B,A. B runs 1–2 and finishes. A runs 2–3, then 3–4. A completes at 4 and B at 2. Keeping A at the queue head at time 1 would violate the specified boundary rule.
4. Original multiple choice: Which claim is false? A) Response stops at first execution; B) RR requires exact burst lengths to choose turns; C) Quantum expiration can move Running to Ready; D) Waiting can span multiple intervals.
Test answer
B is false. RR uses queue order, the quantum, and completion or blocking events. Burst lengths are supplied to calculate this example, not as a necessary input to the RR policy.
5. Which interval accounts for P2's three waiting units in our RR table? What would using only S-A miss?
Answer 5
P2 starts immediately at 2, so its response is zero. After its quantum ends at 4, it remains ready until 7. All three waiting units occur during 4–7. S−A measures only initial response.
6. Retrieve and continue
Without looking, compare why execution stops in SRTF and RR, and what information each policy needs. Trace an RR queue with new arrivals, recording remaining work after each turn. If response and waiting become confused, separately mark first start and every Ready interval. Constructing a timeline with explicit conventions is a practical continuation criterion. Try a different quantum after a gap.
References and authorship
Source checked on 2026-11-23: Operating Systems: Three Easy Pieces, chapter 7, for STCF/SRTF, RR and the quantum/overhead tradeoff. Data, tables, diagrams, boundary conventions and exercises were authored for this lesson. No official exam questions or exam-frequency claims are presented.
Prerequisite: FCFS and SJF · Course contents · Start from foundations
Previous lesson: CPU scheduling from scratch: FCFS, SJF and timelines · Next lesson: Priority and feedback queues: from starvation to MLFQ