{}DSA Atlas

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.

Intermediate9 interview questions

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:

  1. switches from running to blocked (I/O, lock) — non-preemptive point
  2. exits — non-preemptive point
  3. becomes ready (I/O completes, new task) — preemption opportunity
  4. 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

MetricFormulaWant
Turnaround time (TAT)completion − arrivallow
Waiting time (WT)TAT − burstlow
Response time (RT)first run − arrivallow (interactive)
Throughputjobs completed / timehigh
CPU utilisationbusy time / total timehigh
Fairnessshare of CPU vs entitlementeven

The running example

We'll schedule the same four processes with every algorithm:

ProcessArrivalBurstPriority (lower = more important)
P1082
P2144
P3291
P4353

FCFS — First Come, First Served

Run in arrival order, non-preemptive. Simple FIFO queue.

P1 P2 P3 P4 08122126
FCFS: each unit = 20 px.
CompletionTATWTRT
P18800
P2121177
P321191010
P426231818
Average15.258.758.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.

P1 P2 P4 P3 08121726
SJF (non-preemptive).
CompletionTATWT
P1880
P212117
P3262415
P417149
Average14.257.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.
P1 P2 P4 P1 P3 015101726
SRTF: P1 is preempted at t=1.
CompletionTATWTRT
P1171790
P25400
P326241515
P410722
Average13.06.54.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.
P1 P3 P1 P4 P2 0211172226
Preemptive priority: low-priority P2 waits 21 units.
CompletionTATWT
P117179
P2262521
P31190
P4221914
Average17.511.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.

TimeRunsQueue afterwards
0–3P1 (rem 5)P2, P3, P4, P1
3–6P2 (rem 1)P3, P4, P1, P2
6–9P3 (rem 6)P4, P1, P2, P3
9–12P4 (rem 2)P1, P2, P3, P4
12–15P1 (rem 2)P2, P3, P4, P1
15–16P2 ✓P3, P4, P1
16–19P3 (rem 3)P4, P1, P3
19–21P4 ✓P1, P3
21–23P1 ✓P3
23–26P3 ✓—
P1 P2 P3 P4 P1 P3 P4 P1 P3 0369121519212326
Round robin, q = 3. The thin green slice is P2's last unit (15–16).
CompletionTATWTRT
P12323150
P21615112
P32624154
P42118136
Average20.013.53.0

RR has the worst average turnaround here but by far the best response time — exactly the trade-off interactive systems want.

Comparison

AlgorithmPreemptiveAvg WT (example)Avg RTStarvationBest for
FCFSno8.758.75nobatch, simplicity
SJFno7.757.75yesbatch with known bursts
SRTFyes6.54.25yestheoretical optimum
Priorityeither11.0—yes (use aging)importance-based systems
Round robinyes13.53.0notime-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.

highest priority Q0 · RR, quantum 8 ms · new & interactive jobs Q1 · RR, quantum 16 ms Q2 · FCFS / long quantum · CPU-bound jobs used full allotment → demote used full allotment → demote periodic boost: everyone → Q0
MLFQ approximates SJF without knowing burst lengths: short interactive jobs stay on top.

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

AlgorithmPrioritySchedulable if
Rate-monotonic (RMS)static: shorter period = higher priorityutilisation ≤ n(2^(1/n) − 1) (≈ 69% for large n) — sufficient test
Earliest Deadline First (EDF)dynamic: nearest deadline firstutilisation ≤ 100% (preemptive, independent tasks)

Hard real-time systems must never miss deadlines (airbags); soft real-time tolerates occasional misses (video playback).

Interview questions

0 / 9 marked confident

Answer out loud first, then open the model answer. Mark a question confident only when you could explain it to an interviewer without notes.