PROCESS SCHEDULER
Who gets the CPU next, and how long you wait for it
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.
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.
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%
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.
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.
“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.