Q2 · 10 MS SLICEQ1 · 20 MS SLICECPUQ0 · 40 MS SLICETIME0 msBOOST IN100 msWAITING FOR I/OFINISHEDABC
Left: three ready queues, top priority at the backRight: the CPU, the shelf of jobs waiting for I/O; front: the clockBlocks: jobs A (long), B (interactive) and C (short)Three ready queues on the left: Q2 at the back is the top priority with 10 ms slices, Q1 has 20 ms, Q0 at the front has 40 ms. The CPU and the waiting-for-I/O shelf are on the right, the clock in front. Long job A sinks one queue each time it uses up its allotment. Interactive job B runs 1 ms, waits for input and keeps Q2. Short job C enters at the top and finishes at 66 ms. At 100 ms the boost lifts every job back to Q2. 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.

Queues and rules

  1. 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.

Jobs that stay high

Keeping it fair

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.