Queues and rules
The scheduler cannot know how long a job will run. MLFQ keeps several ready queues, one per priority. Rule 1: a job in a higher queue runs first. Rule 2: jobs in the same queue take turns, round robin.
Job A arrives at 0 ms. Rule 3: every new job starts in the top queue, because it might be short. Nothing else is waiting, so A goes straight onto the CPU.
A runs its whole 10 ms without stopping. Rule 4: a job that uses up its allotment at a level moves down one queue. So at 10 ms A drops to Q1, where the slice is 20 ms.
A uses all 20 ms in Q1 too, so at 30 ms it sinks to Q0, the bottom. Down here the slice is 40 ms: long CPU-bound jobs switch less often, which wastes less time on context switches.
Jobs that stay high
Job B arrives at 35 ms in Q2. Rule 1: it outranks A, so it runs at once. After 1 ms of work it waits for input and gives up the CPU. It keeps its place in Q2, and A runs again.
Short job C arrives at 40 ms. It starts in Q2, so it outranks A and runs at once. It uses its 10 ms allotment, so at 50 ms it moves down to Q1. Q2 is empty, so C keeps running.
At 55 ms B's input arrives; B outranks C and runs 1 ms. C then finishes at 66 ms, just 26 ms after it arrived, while A still waits. Without knowing job lengths, MLFQ behaves almost like shortest job first.
Keeping it fair
B returns every 20 ms to run 1 ms. Rule 4 counts it all: by 96 ms B has used 4 of its 10 ms in Q2. A job cannot stay on top by giving up the CPU just before its slice ends.
With many interactive jobs, A could starve in Q0. Rule 5: every S ms (here 100) the scheduler moves every job to the top queue. At 100 ms A is back in Q2 and runs. B is reset to Q2 too, though it still waits.
MLFQ watches what each job does instead of asking for its length. Short and interactive jobs stay high and answer quickly; long jobs sink and use long slices; the boost keeps everyone moving.
What happens
;; step 1/10 · queues queue priority time slice (= allotment) Q2 top 10 ms Q1 middle 20 ms Q0 bottom 40 ms Rule 1 higher queue runs first Rule 2 same queue: round robin, using that queue's slice one CPU core: one job runs at a time
The figure plays a 1 ms-step simulation of the five rules. When a job is preempted or its slice ends, it goes to the back of its queue. A context switch is taken to cost no time. The five rules follow OSTEP chapter 8. The example combines its settings: 10, 20 and 40 ms slices from Figure 8.6, an allotment equal to one slice as in the chapter’s first examples (Figure 8.6 itself gives the top queue two 10 ms slices), and a boost every 100 ms as in Figure 8.4. Real MLFQ schedulers tune these numbers: Solaris by default has 60 queues, slices from 20 ms at the top to a few hundred ms at the bottom, and a boost about every second.