Start from foundations · Course contents · فارسی
Previous lesson: Preemptive scheduling: SRTF and Round Robin turns
Priority and feedback queues: from starvation to MLFQ
In the previous lesson, SRTF selected least remaining work and RR rotated turns. What if handling user input deserves earlier attention than a background calculation? If future execution length is unknown, what can past behavior tell us? Priority expresses a policy's preference; feedback queues can change that preference based on observed execution.
Objective: trace a priority schedule, explain starvation risk, and execute explicit rules for a small MLFQ. Ready versus Running, preemption, RR and scheduling metrics are prerequisites. Numerical examples assume one CPU, no I/O and zero switching overhead. Priority, CPU allowance and required execution duration are different concepts: high priority does not imply a short job.
1. Interpret priority numbers only after stating a convention
Here smaller numbers mean higher priority: one outranks three. Some texts reverse this convention. A priority number is not a duration; priority two does not mean two CPU units.
Two independent choices matter: whether priorities change, and whether scheduling is preemptive. Under preemptive priority scheduling, a newly ready higher-priority job can displace the current job. Under non-preemptive scheduling, selection waits until the CPU is released. Our next example is preemptive. Ties retain the current process; tied waiting jobs use earlier arrival, then table order.
| Process | Arrival A | Burst B | Priority number |
|---|---|---|---|
| P1 | 0 | 5 | 3 |
| P2 | 1 | 2 | 1 |
| P3 | 2 | 1 | 2 |
Only P1 is ready at zero. It runs 0–1, leaving four units. P2 arrives at 1 with priority one and displaces P1. P3 arrives at 2, but priority two is below one, so P2 continues until 3. At 3, P3 outranks P1 and runs 3–4 to completion. P1 then executes its remaining four units from 4 to 8.
0–1 P1 | 1–3 P2 | 3–4 P3 | 4–8 P1
| Process | First start | Completion | Turnaround | Waiting | Response |
|---|---|---|---|---|---|
| P1 | 0 | 8 | 8 | 3 | 0 |
| P2 | 1 | 3 | 2 | 0 | 0 |
| P3 | 3 | 4 | 2 | 1 | 1 |
P1 waits from 1 to 4: zero initial response does not remove later waiting. Although P3 has a shorter burst, it does not interrupt P2: this policy compares priorities, not remaining durations. Making the same problem non-preemptive produces P1 from 0–5, P2 from 5–7 and P3 from 7–8. Changing one assumption changes the answer.
2. Starvation, long waits and aging
Starvation means a ready job can be denied CPU indefinitely while preferred work keeps moving ahead. A finite example with only finite bursts cannot by itself demonstrate indefinite starvation. To construct the risk, suppose a low-priority job is ready and a new high-priority job is available every time the CPU becomes free. The low-priority job is continually bypassed.
Aging gradually improves a waiting job's priority. Consider an illustrative rule: every two continuous units waiting in Ready reduce its priority number by one, down to a minimum of one. Starting at four, it reaches three, two and one after two, four and six waiting units. Waiting and CPU consumption differ: this rule rewards waiting rather than execution.
That is an educational rule, not a universal implementation. A guarantee of eventual execution also depends on tie handling, priority limits, new arrivals and the durations of jobs ahead. Increasing priority alone is insufficient without appropriate tie fairness. A process blocked for I/O is not Ready; whether that time contributes to aging must be specified separately.
3. From fixed queues to execution feedback
Several queues can carry different priorities and use RR internally. In a conventional fixed Multilevel Queue design, jobs are assigned to queues in advance. A Multilevel Feedback Queue (MLFQ) can move jobs between levels based on observed behavior. Having several queues does not alone establish feedback.
Why feedback? Without knowing future burst length, a scheduler can initially give a new job an opportunity, then lower its level if it repeatedly consumes substantial CPU, letting fresh work respond. This is a policy estimate, not certain knowledge of whether a program is short or long. A job's behavior can change across its lifetime.
Historically, the OSTEP MLFQ chapter traces the approach to CTSS and Corbató and colleagues in 1962. The same source emphasizes that implementations vary. One exercise's rules are not universal operating-system laws.
4. A small MLFQ with explicit rules
For hand tracing, use this original policy:
- H has higher priority than L. Never select L while H contains ready work; a new H job can interrupt a running L job.
- New jobs join H's tail. H's quantum and cumulative CPU allotment are both two units. An unfinished job that consumes its full allotment joins L's tail.
- L uses RR with quantum four. There is no lower queue; an unfinished job rejoins L's tail after its quantum.
- If H interrupts an L job, preserve that job at L's head with its unused quantum. Boundary arrivals enter before requeuing an expired-quantum job. Simultaneous arrivals follow table order.
- At times 12, 24 and so on, all unfinished ready or running jobs return to H and their allotments reset. For a precise order, first enqueue arrivals at that instant into H; stop the current job and place it at the head of its own queue; concatenate H followed by L, preserving queue order, into a fresh H. This event grants a fresh quantum. Our next example finishes before the first boost.
Rule five is a priority boost. It differs from aging: aging can promote individual jobs according to their waiting, whereas this boost moves everyone upward at specified times. Choosing the period is a tradeoff; very frequent boosts can weaken distinctions between levels.
5. A complete MLFQ example
There are two jobs: A arrives at zero with burst seven; B arrives at one with burst two.
| Interval | Execution queue | Event and resulting state |
|---|---|---|
| 0–2 | H, job A | B joins H at 1; A consumes its two-unit allotment and moves to L with five left. |
| 2–4 | H, job B | B executes two units and finishes; H becomes empty. |
| 4–8 | L, job A | A executes four units; one remains, so it rejoins L's tail. |
| 8–9 | L, job A | As the only ready job, A executes its last unit and finishes. |
B's arrival at 1 does not interrupt A's turn in the same H queue; equal-level jobs use RR. A drops at 2 because it consumed its allotment, not because it waited. Continuing A at 8 is not a promotion: H is empty and L keeps working. A completes at 9, with waiting 9-0-7=2 and response zero. B completes at 4, with waiting and response 2-1=1.
With I/O, resetting an allotment after every voluntary early yield would let a job avoid demotion through short pauses. Accounting for cumulative CPU use at a level closes that opportunity: sleeping or waiting for I/O should not automatically erase consumed CPU. I/O return placement and boost rules still need specification. Our numerical example intentionally excludes I/O.
6. Independent practice and test traps
Questions are original. Write selection reasons beside the timeline before revealing answers.
1. In the priority example, why does P3 not interrupt P2 at time 2 despite its one-unit burst?
Answer 1
P3 has priority number two and P2 has one. One outranks two under our convention. This policy compares priority, not remaining execution length; the latter was SRTF's rule.
2. Under our aging rule, what number does a job starting at priority five have after six continuous Ready-waiting units?
Answer 2
Three promotions, one per two waiting units: five becomes four, then three, then two. A lower number means higher priority here. It has not yet reached the minimum of one.
3. In the MLFQ example, change A's burst from seven to five, keeping everything else. When does A finish?
Answer 3
A executes two H units from 0–2, then B finishes from 2–4. A's remaining three units execute in L from 4–7. A need not consume an entire four-unit quantum; it finishes at 7.
4. Original multiple choice: Which is essential to MLFQ? A) Exact future burst knowledge; B) Equal quantum at every level; C) Level changes under feedback rules; D) Larger priority numbers always meaning greater importance.
Test answer
C. Changeable membership distinguishes feedback from fixed classification. Future duration is generally unknown, quanta may differ, and priority-number direction is a convention.
5. Transfer: A job in H with cumulative allotment two has consumed one CPU unit, blocks for I/O, then becomes ready. Without a boost or reset rule, how much allotment remains?
Answer 5
One unit. I/O blocking alone does not erase previous CPU use. Its queue position on return depends on queue rules. A new turn does not necessarily provide a new cumulative allotment.
7. Retrieve and continue
Without looking, explain priority-number direction, what triggers preemption, what triggers demotion, and aging versus boosting. If a timeline is wrong, list H, L, remaining CPU and remaining allowance at every event separately. Applying the same rules to fresh arrivals is a practical continuation criterion. Later change one rule and predict its effect. Real-time scheduling and a comprehensive chapter review remain unfinished.
References and authorship
Source checked on 2026-11-23: Operating Systems: Three Easy Pieces, chapter 8: MLFQ, for feedback, CTSS history, cumulative accounting and periodic boosts. Examples, diagrams, the aging rule, queue conventions and questions are original. This two-queue model does not claim to implement a particular operating system.
Prerequisite: SRTF and Round Robin · Course contents · Start from foundations
Previous lesson: Preemptive scheduling: SRTF and Round Robin turns