Round Robin and SRTF — worked examples
Round Robin and SRTF — worked examples
Recorded by Dr. Krithika L.B. Slot m2-rr-srtf
Taking the CPU away: round robin and the time quantum, shortest-remaining-time-first, preemptive priority, and multilevel queues that adapt to process behaviour.
By the end of this session you should be able to:
Designed for time-sharing. The ready queue is a circular FIFO; each process gets a small unit of CPU time, the time quantum q (typically 10–100 ms). If it hasn't finished when the quantum expires, the timer interrupt preempts it and it goes to the tail of the ready queue.
P1 = 24, P2 = 3, P3 = 3, all at time 0.
Gantt: P1 [0–4] · P2 [4–7] · P3 [7–10] · P1 [10–14] · P1 [14–18] · … · P1 [26–30].
WT: P1 = 10 − 4 = 6, P2 = 4, P3 = 7 → average 17/3 ≈ 5.67 ms. Response times are 0, 4, 7 — every process starts quickly.
With n processes and quantum q, no process waits more than (n − 1) × q before its next turn — RR guarantees response time, at the cost of a higher average turnaround than SJF.
When a new process arrives whose burst is shorter than what remains of the running process, the running process is preempted.
(AT, BT): P1 (0, 8), P2 (1, 4), P3 (2, 9), P4 (3, 5).
t = 0 P1 starts. t = 1 P2 arrives (4 < 7 remaining) → preempt. P2 runs to 5. Then P4 (5) beats P1 (7) → P4 to 10. P1 to 17, P3 to 26.
Gantt: P1 [0–1] · P2 [1–5] · P4 [5–10] · P1 [10–17] · P3 [17–26].
WT: P1 = 17 − 0 − 8 = 9, P2 = 0, P3 = 26 − 2 − 9 = 15, P4 = 10 − 3 − 5 = 2 → average 6.5 ms (non-preemptive SJF gives 7.75).
When a process arrives with a higher priority than the running one, the running process is preempted immediately. Starvation is still possible; aging still applies. Try the same data with both versions in the simulator:
Processes are permanently assigned to one of several queues by type, each with its own algorithm. For example:
Scheduling between queues is either fixed-priority preemptive (a lower queue runs only if all higher queues are empty — risk of starvation) or time-slicing (e.g. 80% of CPU to foreground RR, 20% to background FCFS).
MLFQ lets processes move between queues based on their behaviour: a process that uses too much CPU is moved down; one that waits too long is moved up (aging). This favours short and interactive jobs without knowing burst lengths in advance.
Three queues: Q0 RR with q = 8 ms, Q1 RR with q = 16 ms, Q2 FCFS. Q1 runs only when Q0 is empty; Q2 only when Q0 and Q1 are empty.
Every new process enters Q0.
A process with an 5 ms burst finishes within its 8 ms quantum in Q0 — interactive jobs stay at the top and respond quickly.
A process that uses its full 8 ms is moved down to Q1, where it gets 16 ms when Q0 is empty.
If it still doesn't finish, it drops to Q2 and runs FCFS in leftover CPU time — long CPU-bound jobs sink.
A process that has waited too long in a low queue is promoted back up, preventing starvation.
An MLFQ is defined by: the number of queues, the algorithm for each queue, when to upgrade a process, when to demote it, and which queue a new process enters. It is the most general — and most complex — scheduler; variants are used by Windows, macOS and older Linux kernels.
| Algorithm | Preemptive? | Optimises | Risk |
|---|---|---|---|
| FCFS | No | Simplicity | Convoy effect |
| SJF | No | Average WT (optimal) | Starvation of long jobs |
| Priority | No / Yes | Importance | Starvation → aging |
| Round Robin | Yes (quantum) | Response time, fairness | Overhead if q too small |
| SRTF | Yes (on arrival) | Average WT | Starvation; needs prediction |
| MLQ / MLFQ | Yes | Mix of process types | Complex to tune |
Round Robin and SRTF — worked examples
Recorded by Dr. Krithika L.B. Slot m2-rr-srtf
Multilevel and feedback queues
Recorded by Dr. Krithika L.B. Slot m2-mlq-mlfq
Work through these in order after the lecture — each one expands a part of the notes with diagrams, hover notes and a quick check.
Answer each question in your own words first, then reveal the model answer.
Revealed 0P1 0–3, P2 3–6, P3 6–9, P1 9–12, P2 12–13, P3 13–16, P1 16–19, P1 19–20. CT: P1 20, P2 13, P3 16 → WT 10, 9, 10 → average 9.67.
No process is ever preempted by the timer, so RR behaves exactly like FCFS.
Whenever a process arrives (to compare its burst with the remaining time of the running process) and whenever the running process finishes or blocks.
Interactive processes use short CPU bursts and give up the CPU before their quantum ends, so they stay in the high-priority queues; CPU-bound processes use full quanta and are demoted.
(n − 1) × q = 4 × 20 = 80 ms (ignoring switch overhead).
Scheduling decides who gets the CPU. But processes also hold other resources — locks, files, printers. If each holds one and waits for another's, nobody proceeds: deadlock.
Continue to 2.6 — Deadlocks →