Multi-Level Feedback Queue
The multi-level feedback queue (MLFQ) is a CPU scheduler that ranks processes by observed behavior rather than declared priority. Every job starts in the top queue, drops one level each time it uses a full time quantum with work left, and returns to the top when it blocks for I/O. In this way, the scheduler identifies interactive jobs from runtime behavior without being told burst lengths in advance.
Picture a help desk where every visitor starts at an express window: long requests move to slower lines with longer time limits, visitors who step away for documents rejoin the express line when they return, and every twelve minutes waiting visitors in slower lines move back to the front so none starve. This lesson runs MLFQ head-to-head against plain Round Robin on the same four-process workload so you can compare the two schedules directly.
How Multi-Level Feedback Queue Works
The player opens in MLFQ mode with three ready queues: Q0 (2-tick quantum), Q1 (4-tick quantum), and Q2 (8-tick quantum). The scheduler follows six rules:
- Always run the head of the highest non-empty queue, with shorter quanta at the top and longer quanta below.
- New arrivals enter Q0.
- A job that consumes its full quantum and still has CPU work is demoted one level. Using the full slice indicates a CPU-bound job.
- A job that blocks for I/O before its quantum runs out is not demoted; when the I/O completes, this simplified model returns it to Q0. Yielding the CPU early indicates an interactive job.
- If a higher-priority job becomes ready while a lower-priority one is running, the runner is preempted and returned to the tail of its own queue.
- Every 12 ticks, a priority boost lifts every ready job in Q1 and Q2 back to Q0. The currently running job is not lifted, though a freshly boosted Q0 job usually preempts it on the next step.
Switch the policy to Round Robin for the baseline: one FIFO ready queue with a fixed quantum of 3 ticks. New work joins the tail, the head gets the CPU, and a job whose quantum expires with CPU work left rotates back to the tail. Round Robin is MLFQ with one queue, serving as a control case that isolates what multi-level feedback changes.
Tie-breaking follows a fixed order on each clock tick: the trace resolves arrivals first, then I/O completions, then the priority boost, and finally preemption or requeue. Same-tick arrivals therefore line up ahead of a job whose quantum just expired.
Multi-Level Feedback Queue Step by Step
The workload has four processes: P1 arrives at t=0 needing 5 CPU ticks, then 4 ticks of I/O, then 3 more CPU ticks. P2 arrives at t=1 with a single 4-tick burst. P3 arrives at t=2 with 2 CPU, 3 I/O, and 4 CPU ticks. P4 arrives at t=4 with one 3-tick burst.
MLFQ (quanta 2/4/8, boost every 12, default run). P1 runs 0–2, uses its full Q0 quantum, and drops to Q1; P3 arrives at t=2 and enters Q0 ahead of it. P2 runs 2–4 and is demoted. P3 runs 4–6 and finishes its 2-tick burst rather than expiring, so it leaves for I/O until t=9 without demotion. P4 runs 6–8 and is demoted. With Q0 empty, P1 runs from Q1 at t=8 for one tick until P3's I/O completes at t=9, re-enters Q0, and preempts P1 back to Q1's tail.
P3 runs 9–11, burns a full Q0 quantum this time, and drops to Q1. P2 gets one Q1 tick at 11–12 before the t=12 boost lifts ready jobs P4, P1, and P3 from Q1 back to Q0; P2, running and therefore unlifted, is preempted into Q1. Q0 then drains in arrival order: P4 runs 12–13 and completes (turnaround 9); P1 runs 13–15, where its burst ends at the same tick as its quantum so it goes to I/O rather than down a level; P3 runs 15–17 and completes (turnaround 15). P2 finishes 17–18 from Q1 (turnaround 17). The CPU sits idle 18–19 while P1's I/O drains, then P1 runs 19–21 from Q0, demotes once more, and finishes 21–22 from Q1 (turnaround 22, finish time t=22).
Round Robin (quantum 3). Toggle the policy to compare. P1 runs t=0–3 while P2 and P3 arrive and queue behind it. When P1's quantum expires at t=3, the queue reads P2, P3, P1 because same-tick arrivals enqueue ahead of the requeued job. P2 runs 3–6 (P4 arrives at t=4) and expires with one tick left. P3 runs 6–8, finishes its first burst, and leaves for I/O until t=11. P1 runs 8–10, completes its 5-tick burst, and blocks on I/O until t=14. P4 runs 10–13 and completes while P3 rejoins the queue mid-slice at t=11. P2 runs 13–14 and completes; P1's I/O finishes on the same tick. From there the remaining work alternates: P3 runs 14–17 and expires one tick short, P1 runs 17–20 and completes, and P3 finishes 20–21. The CPU is never idle, and the schedule ends at t=21. Turnarounds: P1 = 20, P2 = 13, P3 = 19, P4 = 9. Response times: 0, 2, 4, 6.
On screen, each process keeps one identity color from arrival to the done lane, and the active card wears a halo ring while bystanders fade back. The CPU slot fills a quantum meter with ticks used in the current slice, demotions drop a card one shelf, and the t=12 boost lifts three cards at once. The Gantt ribbon at the bottom records every slice, windowed to recent ticks during playback and shown in full beside the metrics table on the final beat.
Why MLFQ Doesn't Need to Know Burst Times
With all jobs available together and known bursts, shortest-first order minimizes average turnaround; staggered arrivals need a more careful claim. In practice, future CPU-burst lengths are unknown and must be estimated from execution history. MLFQ treats recent CPU usage as that estimate: a process that burns a full quantum with work left is demoted, while one that yields for I/O stays at high priority.
Comparing the two runs shows the trade-off. Response times drop from 0, 2, 4, 6 under Round Robin to 0, 1, 2, 2 under MLFQ because the short Q0 quantum lets every newcomer reach the CPU quickly. The cost falls on the CPU-heavy job: P1's turnaround grows from 20 to 22 because the changed priorities, preemptions, and I/O timing delay its completion (P1 is demoted twice in this trace, not after every interruption). The t=12 boost prevents starvation: without periodic resets, a demoted job behind a stream of interactive ones could wait indefinitely. MLFQ improves interactive response time by penalizing processes that recently spent full CPU quanta, while the boost caps how long those penalties compound.
Edge Cases
- Burst ends exactly at the quantum boundary. Completion outranks expiry: P1 at t=15 has both its burst and its Q0 quantum hit zero, so it goes to I/O instead of down a level. P4 completes at t=13 before using its full Q0 quantum, so it also leaves rather than requeuing.
- Same-tick arrival vs. expiry. Arrivals are admitted before the expired job is requeued: P3 lands in Q0 ahead of P1 at t=2 in MLFQ, and the Round Robin queue reads P2, P3, P1 at t=3.
- The boost skips the running job. At t=12, P2 holds the CPU in Q1 and gets no lift; it is preempted by the boosted Q0 crowd and resumes from Q1 five ticks later.
- Idle is a real schedule entry. From t=18 to 19 nothing is runnable while P1's I/O drains; the Gantt ribbon shows an IDLE block and the clock jumps to the next event rather than ticking through empty slots.
Common Mistakes
- Reading the Gantt chart as the policy. The ribbon is the output; the policy is the rule set that chose each next slice, which is why the animation spends its beats on decisions rather than on the ribbon.
- Thinking I/O gets a job demoted. Blocking before the quantum expires is how a job avoids demotion and keeps high priority.
- Expecting MLFQ to win on every metric. It shortened response times here while making P1's turnaround worse and finishing the workload a tick later than Round Robin; schedulers trade metrics rather than dominating across all of them.
- Forgetting preemption. Reaching the head of a lower queue does not guarantee an uninterrupted slice: P1 at t=9 ran one tick before higher-priority work took the CPU back.
- Treating queue levels as permanent judgments about a process. They are estimates from recent behavior, and the boost exists because estimates go stale.
A Note on Simplification
This model isolates one question: which runnable process gets the CPU next, and why. To keep that question visible, it uses one CPU, integer ticks, fixed bursts, and I/O always returning to Q0, while omitting context-switch cost, interrupt latency, locks, affinity, niceness, and production fairness accounting. Real schedulers add those mechanisms on top of the same core feedback idea: penalize recent full-quantum CPU use, keep I/O-bound jobs responsive, and periodically reset stale priorities.
Keep exploring
A few lessons that share the same technique or lead into the next idea.
Found a mistake or something unclear?