CPU Scheduling
Scheduling criteria and metrics, FCFS, SJF, SRTF, priority, round robin, multilevel (feedback) queues, Linux CFS/EEVDF, multiprocessor and real-time scheduling — with fully worked Gantt charts.
The scheduling problem
When more threads are ready than there are CPUs, the short-term scheduler decides who runs next. It runs when a task:
- switches from running to blocked (I/O, lock) — non-preemptive point
- exits — non-preemptive point
- becomes ready (I/O completes, new task) — preemption opportunity
- uses up its time slice (timer interrupt) — preemption
The dispatcher performs the actual switch; its delay is the dispatch latency.
CPU bursts and I/O bursts
Programs alternate between CPU bursts and I/O waits. I/O-bound programs have many short CPU bursts; CPU-bound programs have few long ones. Good schedulers favour short bursts to keep devices busy and users happy.
Metrics
| Metric | Formula | Want |
|---|---|---|
| Turnaround time (TAT) | completion − arrival | low |
| Waiting time (WT) | TAT − burst | low |
| Response time (RT) | first run − arrival | low (interactive) |
| Throughput | jobs completed / time | high |
| CPU utilisation | busy time / total time | high |
| Fairness | share of CPU vs entitlement | even |
The running example
We'll schedule the same four processes with every algorithm:
| Process | Arrival | Burst | Priority (lower = more important) |
|---|---|---|---|
| P1 | 0 | 8 | 2 |
| P2 | 1 | 4 | 4 |
| P3 | 2 | 9 | 1 |
| P4 | 3 | 5 | 3 |
FCFS — First Come, First Served
Run in arrival order, non-preemptive. Simple FIFO queue.
| Completion | TAT | WT | RT | |
|---|---|---|---|---|
| P1 | 8 | 8 | 0 | 0 |
| P2 | 12 | 11 | 7 | 7 |
| P3 | 21 | 19 | 10 | 10 |
| P4 | 26 | 23 | 18 | 18 |
| Average | 15.25 | 8.75 | 8.75 |
Problem: the convoy effect — short P2 and P4 wait behind long jobs.
SJF — Shortest Job First (non-preemptive)
When the CPU frees up, pick the ready job with the shortest burst.
At t = 0 only P1 is present, so it runs to 8. At t = 8 the ready set is P2(4), P3(9), P4(5) → P2, then P4, then P3.
| Completion | TAT | WT | |
|---|---|---|---|
| P1 | 8 | 8 | 0 |
| P2 | 12 | 11 | 7 |
| P3 | 26 | 24 | 15 |
| P4 | 17 | 14 | 9 |
| Average | 14.25 | 7.75 |
SRTF — Shortest Remaining Time First (preemptive SJF)
Whenever a job arrives, compare its burst with the remaining time of the running job; preempt if shorter.
- t=0: P1 starts (remaining 8).
- t=1: P2 arrives (4) < P1's remaining 7 → preempt, run P2.
- t=2, t=3: P3 (9) and P4 (5) arrive; P2's remaining (3, then 2) is still shortest.
- t=5: P2 done. Remaining: P1 7, P3 9, P4 5 → P4 runs 5–10.
- t=10: P1 (7) runs 10–17; then P3 17–26.
| Completion | TAT | WT | RT | |
|---|---|---|---|---|
| P1 | 17 | 17 | 9 | 0 |
| P2 | 5 | 4 | 0 | 0 |
| P3 | 26 | 24 | 15 | 15 |
| P4 | 10 | 7 | 2 | 2 |
| Average | 13.0 | 6.5 | 4.25 |
SRTF gives the minimum average waiting time of all algorithms — but needs burst-time knowledge and can starve long jobs.
Priority scheduling (preemptive)
Run the highest-priority ready job (here: lowest number). Using the priority column:
- t=0: P1 (2) runs. t=1: P2 (4) arrives — lower priority, no preemption.
- t=2: P3 (1) arrives → preempts P1 (P1 remaining 6). P3 runs 2–11.
- t=11: ready P1(2), P2(4), P4(3) → P1 11–17, P4 17–22, P2 22–26.
| Completion | TAT | WT | |
|---|---|---|---|
| P1 | 17 | 17 | 9 |
| P2 | 26 | 25 | 21 |
| P3 | 11 | 9 | 0 |
| P4 | 22 | 19 | 14 |
| Average | 17.5 | 11.0 |
Starvation: if high-priority jobs keep arriving, P2 might never run. Fix with aging (raise priority while waiting).
Round Robin (quantum = 3)
Each job runs for at most one quantum, then goes to the back of the ready queue. Convention: a job arriving at the same instant another is preempted enters the queue first.
| Time | Runs | Queue afterwards |
|---|---|---|
| 0–3 | P1 (rem 5) | P2, P3, P4, P1 |
| 3–6 | P2 (rem 1) | P3, P4, P1, P2 |
| 6–9 | P3 (rem 6) | P4, P1, P2, P3 |
| 9–12 | P4 (rem 2) | P1, P2, P3, P4 |
| 12–15 | P1 (rem 2) | P2, P3, P4, P1 |
| 15–16 | P2 ✓ | P3, P4, P1 |
| 16–19 | P3 (rem 3) | P4, P1, P3 |
| 19–21 | P4 ✓ | P1, P3 |
| 21–23 | P1 ✓ | P3 |
| 23–26 | P3 ✓ | — |
| Completion | TAT | WT | RT | |
|---|---|---|---|---|
| P1 | 23 | 23 | 15 | 0 |
| P2 | 16 | 15 | 11 | 2 |
| P3 | 26 | 24 | 15 | 4 |
| P4 | 21 | 18 | 13 | 6 |
| Average | 20.0 | 13.5 | 3.0 |
RR has the worst average turnaround here but by far the best response time — exactly the trade-off interactive systems want.
Comparison
| Algorithm | Preemptive | Avg WT (example) | Avg RT | Starvation | Best for |
|---|---|---|---|---|---|
| FCFS | no | 8.75 | 8.75 | no | batch, simplicity |
| SJF | no | 7.75 | 7.75 | yes | batch with known bursts |
| SRTF | yes | 6.5 | 4.25 | yes | theoretical optimum |
| Priority | either | 11.0 | — | yes (use aging) | importance-based systems |
| Round robin | yes | 13.5 | 3.0 | no | time-sharing, interactivity |
Multilevel queue & multilevel feedback queue
Multilevel queue: partition tasks into fixed classes (system, interactive, batch), each with its own queue and algorithm; schedule between queues by fixed priority or time slices. Tasks never move between queues.
Multilevel feedback queue (MLFQ): tasks move between queues based on behaviour.
Real schedulers
Linux: CFS → EEVDF
- Normal tasks (
SCHED_OTHER) are scheduled by fairness. CFS tracked each task's weighted virtual runtime in a red-black tree and always ran the task with the smallest vruntime. - Nice values (−20 … +19) map to weights; each step is ≈ 10% CPU share.
- Since Linux 6.6, EEVDF (Earliest Eligible Virtual Deadline First) picks among tasks that are owed CPU the one with the earliest virtual deadline — better latency for tasks that request short slices.
- Real-time classes always beat normal tasks:
SCHED_FIFO(run until block/yield),SCHED_RR(FIFO with a quantum),SCHED_DEADLINE(EDF with runtime/period/deadline reservations). - cgroups (
cpu.max,cpu.weight) cap or weight CPU per container — this is how Kubernetes CPU limits work (and why throttling happens when a container exhausts its quota within a 100 ms period).
Windows
Priority-based, preemptive, 32 levels, with dynamic priority boosts for foreground windows and I/O completion — an MLFQ-like design.
Multiprocessor scheduling
- Per-CPU run queues with load balancing (push/pull migration, work stealing).
- Affinity: prefer running a task where its cache is warm; pin with
taskset. - NUMA: keep tasks near their memory node.
- SMT: sibling hyperthreads share execution units.
Real-time scheduling
| Algorithm | Priority | Schedulable if |
|---|---|---|
| Rate-monotonic (RMS) | static: shorter period = higher priority | utilisation ≤ n(2^(1/n) − 1) (≈ 69% for large n) — sufficient test |
| Earliest Deadline First (EDF) | dynamic: nearest deadline first | utilisation ≤ 100% (preemptive, independent tasks) |
Hard real-time systems must never miss deadlines (airbags); soft real-time tolerates occasional misses (video playback).
Interview questions
Answer out loud first, then open the model answer. Mark a question confident only when you could explain it to an interviewer without notes.