PROCESS SCHEDULER

Who gets the CPU next, and how long you wait for it

L216 MINOPERATING SYSTEMS
PREDICT

Round robin: each job runs for a fixed slice (the quantum), then goes to the back of the queue. Two CPU hogs, A and B, quantum 8 ticks, each switch costs 1 tick. An editor wakes up just as A's quantum expires and B is being switched in. Roughly how long until the editor runs?

One CPU, many runnable processes. The scheduler picks who runs next and for how long. Every switch to a different process costs time: save one context, load another, start with cold caches.

FCFS runs a job until it blocks or exits, so a CPU hog stalls everything behind it. Round robin preempts after a fixed quantum. Small quanta respond fast but waste more of the CPU on switches; large quanta drift back toward FCFS. MLFQ watches behaviour: jobs that burn their whole quantum sink to lower levels with longer slices, and jobs that block early stay on top and preempt them.

▶ SIMULATION
CPU
t = 0 · on CPU: idle
useful 0 · switching 0 · idle 0
RR · quantum 2
READY QUEUE
ready: —
PROCESSES
A (hog): new, 20 left
B (hog): new, 20 left
E (editor): new, 1 left, 4 bursts
timeline—
legendA B E = running · ~ = context switch · . = idle
editor response— (worst 0, limit 8)
switch overhead0 of 0 ticks = 0.0% (target ≤ 25%)
event—
✓ interactive response ≤ 8 ticks
SCHEDULE
01[SCHED]policy: first come, first served, run to completion
02[CPU]run ticks 0–3
03[CPU]run ticks 4–7
04[CPU]run ticks 8–11
05[CPU]run ticks 12–15
06[CPU]run ticks 16–19
07[CPU]run ticks 20–23
08[CPU]run ticks 24–27
09[CPU]run ticks 28–31
10[CPU]run ticks 32–35
11[CPU]run ticks 36–39
12[CPU]run ticks 40–43
13[CPU]run ticks 44–47
14[CPU]run ticks 48–51
15[CPU]run ticks 52–55
16[CPU]run ticks 56–59
17[CPU]run ticks 60–63
18[CPU]run ticks 64–67
19[CPU]run ticks 68–71
20[CPU]run ticks 72–75
21[CPU]run ticks 76–79
22[CPU]run ticks 80–83
23[CPU]run ticks 84–87
Start of fcfs scenario. 23 steps.
Scheduler loop (one CPU)

  on timer tick / block / exit / wakeup:
    next = pick_next(ready queues)
    if next != current: context switch (save regs, load regs,
                         cold caches and TLB afterwards)

FCFS   run each job until it blocks or exits
       one CPU hog delays everyone behind it

RR     preempt after `quantum`, go to the back of the queue
       worst wait ~ (ready jobs) x (quantum + switch)
       small quantum: responsive, more switch overhead
       large quantum: efficient, degenerates to FCFS

MLFQ   several queues, highest non-empty runs first
       used a whole quantum? move down a level (CPU-bound)
       blocked before the quantum ended? stay (interactive)
       higher level arrives? preempt the lower one
       anti-starvation: boost / age jobs that waited too long

Watch the timeline and the editor response row. FCFS makes the editor wait 34 ticks behind the two hogs. ROUND ROBIN at quantum 2 keeps it at 7 ticks but spends 34% of the run on switches. MLFQ answers the editor in 1 tick, because the hogs have already sunk to the lower levels.

TUNE IT NOT YET

Round robin at quantum 2 keeps the editor responsive but burns over a third of the CPU on context switches. Find a QUANTUM where the editor still gets the CPU within 8 ticks and switching costs at most 25% of the run.

GOAL: worst response ≤ 8 and overhead ≤ 25%

FIX IT NOT YET

Write MLFQ's pick. queues is an array of levels, highest priority first; each level is a FIFO array of \{ pid, readySince, kind \}. Return the pid to run at time now, or null. Strict priority starves CPU-bound jobs when interactive ones keep the top level busy, so add aging: a job that has been ready for 20 ticks or more runs first (the longest-waiting one). The grader runs a hog against three busy interactive jobs, then this lesson's workload.

Mental model:

a scheduler trades response time against throughput, and the quantum is the dial. One fixed quantum for everyone forces one compromise. Good schedulers learn from behaviour: short slices for jobs that block often, long slices for jobs that never do, plus a rule so the long ones are never starved.

IN PRODUCTION

Linux CFS and EEVDF (weighted fair sharing, not MLFQ, but the same latency against throughput tension behind sched_latency_ns in CFS, base_slice_ns in EEVDF, and nice levels), Windows and macOS priority boosting of foreground threads, Kubernetes CPU limits that throttle a container mid-request, and tail latency that spikes when a batch job shares the box.

IN INTERVIEWS

“Compare FCFS, round robin and MLFQ” or “How do you pick a time slice?” Explain the quantum tradeoff with numbers (switch cost over slice length), how MLFQ infers interactivity from blocking, why it needs a boost against starvation, and what a context switch costs beyond the registers.

RECALL CARDS Tap to flip. They join your review deck when you mark the lesson complete.
NEXT: JOURNALING →