4.2.11Operating Systems

Multi-level feedback queue (MLFQ) — promotion, demotion rules

2,025 words9 min readdifficulty · medium

WHAT it is

Lower queues usually get longer time quanta (CPU-bound jobs run rarely but in big chunks → fewer context switches).


WHY each rule exists (HOW it solves a problem)


Figure — Multi-level feedback queue (MLFQ) — promotion, demotion rules

A small "formula" for behaviour (derive, don't memorize)


Worked Examples



Recall Feynman: explain to a 12-year-old

Imagine a line of slides at a playground, stacked tall to short. New kids start on the tallest slide (fastest fun). If a kid hogs a slide too long, the guard makes them move to a lower, slower slide. Kids who slide quick and run off to do something else stay near the top. Every few minutes the guard blows a whistle and says "everybody back to the top slide!" so nobody is stuck at the bottom forever — and a kid who used to hog but now plays nicely gets another chance up high.


Flashcards

What priority does a new job enter MLFQ at, and why?
Highest (top queue), Rule 3 — optimistic assumption it may be short/interactive; if wrong it drifts down.
What triggers demotion in MLFQ (correct version)?
When a job exhausts its total time allotment at a level (cumulative, across pieces), not just a single slice.
Why is "demote only on full single slice" a bad rule?
It's gameable — a job can yield just before slice end, never "use a full slice", and starve others.
What is the priority boost (Rule 5)?
Every period S, move ALL jobs back to the top queue.
Name the two problems the boost solves.
(1) Starvation of low-priority jobs; (2) jobs whose behaviour changed from CPU-bound to interactive regaining priority.
Why do lower queues use larger time quanta?
CPU-bound jobs run rarely but in big chunks → fewer context switches, better throughput.
Worst-case starvation bound with boost period S?
Approximately S — at the next boost any job returns to the top.
How does MLFQ approximate SJF without knowing run times?
By observing behaviour: jobs that consume full allotments are demoted (treated as long); short/interactive jobs stay high.
Within a single queue level, how are equal-priority jobs scheduled?
Round Robin using that level's quantum (Rule 2).

Connections

  • Round-Robin Scheduling — used within each MLFQ level.
  • Shortest Job First (SJF) — MLFQ approximates it without prior knowledge.
  • Priority Scheduling — MLFQ = adaptive, multi-level priority.
  • Starvation and Aging — boost = aging mechanism.
  • Context Switch Overhead — motivates larger low-queue quanta.
  • CPU-bound vs I/O-bound processes — the behaviour MLFQ detects.
  • Time Quantum / Time Slice — tuning knob per level.

Concept Map

solved by

includes

includes

includes

optimistic assume short

eats whole quantum

infers run time

tracks total allotment

boost all to top

fresh chance

run rarely in big chunks

Schedule without knowing job length

MLFQ: stacked RR queues

Rule 3: new job starts at top

Rule 4: demote on full allotment

Rule 5: periodic boost

Interactive jobs stay high

CPU-bound jobs drift down

Approximates SJF

Prevents starvation

Blocks gaming attack

Lower queues get longer quanta

Hinglish (regional understanding)

Intuition Hinglish mein samjho

MLFQ ka core idea simple hai: humein ek scheduler chahiye jo interactive jobs (jaise typing, clicking) ko fast response de, aur saath hi long CPU-heavy jobs ko bhi fairly chalaye — bina pehle se jaane ki kaunsa job kitna lamba hai. Iske liye MLFQ multiple queues banata hai, har queue ka apna priority level hota hai (Q0 sabse upar = sabse high priority). Naya job hamesha top queue se start hota hai (Rule 3) — optimistic assumption ki shayad ye chhota/interactive ho.

Ab observe karke seekhna: agar koi job apna pura time allotment kha jata hai (matlab CPU greedy hai), to use niche bhej dete hain (Rule 4, demotion). Important point — demotion "ek single slice full use" pe nahi, balki cumulative allotment pe hona chahiye, warna ek chalu process slice khatam hone se theek pehle thoda I/O karke system ko game kar sakta hai aur niche kabhi nahi jayega. Cumulative counting se ye cheating bandh ho jati hai.

Promotion ka sabse standard tareeka hai priority boost (Rule 5): har S time units ke baad saare jobs ko wapas top queue me utha do. Iska double fayda hai — ek to bottom wale jobs starve nahi karte, aur doosra agar koi job pehle CPU-bound tha lekin ab interactive ban gaya hai (jaise compile khatam, ab user input ka wait), to use dobara high priority mil jati hai. Worst-case starvation lagbhag S tak limited rehta hai. Lower queues me quantum bada rakhte hain taaki long jobs kam baar but bade chunks me chale aur context-switch overhead kam ho. Mnemonic yaad rakho: "Start High, Hog = Sink, Whistle = Rise."

Test yourself — Operating Systems

Connections