Operating Systems Module 3: CPU scheduling Lesson 3.4 14 min

Priorities and feedback queues

Priority scheduling and the starvation it causes, aging as the cure, and the multi-level feedback queue, which learns from each job’s behaviour whether it is interactive.

Your music player stutters while a big compile runs in the background. Both are just processes to the kernel, but you care about them very differently: a skip in the music is annoying at once, while the compile finishing a second later goes unnoticed. Some processes matter more than others, and a scheduler should be able to say so.

Priority scheduling

In priority scheduling every process has a priority, and the scheduler always runs the ready process with the best one. In this course a smaller number means a more important process: priority 1 beats priority 5. (Real systems disagree on the direction; Linux nice values work the same way as here, smaller is more important.) Priority scheduling can be non-preemptive, where a running job keeps the CPU until it blocks or ends, or preemptive, where a more important arrival takes the CPU at once.

SJF from Lesson 3.2 is really a priority scheduler whose priority is the burst length. And it shares SJF’s problem.

Starvation, and aging

L is a low-priority job (priority 5) that needs only 3 ms. A stream of important jobs, H1 to H5 (priority 1), keeps arriving, each one before the last has finished. The top pane is plain priority scheduling. The bottom pane uses aging: every 2 ms a process spends waiting in the ready queue, its priority improves by one step.

A low-priority job waits behind a stream of important ones

  1. L and H1 arrive L and H1 arrive and join the ready queue. Ready queue: H1 → L.
    Time
    0 ms
    Running
    idle
    Ready queue
    H1 → L
    L
    3 of 3 ms left, priority 5
    H1
    4 of 4 ms left, priority 1
    H2
    arrives at 3
    H3
    arrives at 7
    H4
    arrives at 11
    H5
    arrives at 15
  2. H1 runs The scheduler picks H1, because its priority, 1, is the most important (a smaller number wins).
    Time
    0 ms
    Running
    H1
    Ready queue
    L
    L
    3 of 3 ms left, priority 5
    H1
    4 of 4 ms left, priority 1
    H2
    arrives at 3
    H3
    arrives at 7
    H4
    arrives at 11
    H5
    arrives at 15
  3. H2 arrives H2 arrives and joins the ready queue. Ready queue: H2 → L.
    Time
    3 ms
    Running
    H1
    Ready queue
    H2 → L
    L
    3 of 3 ms left, priority 5
    H1
    1 of 4 ms left, priority 1
    H2
    4 of 4 ms left, priority 1
    H3
    arrives at 7
    H4
    arrives at 11
    H5
    arrives at 15
  4. H1 finishes, H2 runs H1 finishes at 4 ms. It waited 0 ms in all, and its turnaround (arrival to finish) was 4 ms. The scheduler picks H2, because its priority, 1, is the most important (a smaller number wins).
    Time
    4 ms
    Running
    H2
    Ready queue
    L
    L
    3 of 3 ms left, priority 5
    H1
    done at 4: waited 0, turnaround 4
    H2
    4 of 4 ms left, priority 1
    H3
    arrives at 7
    H4
    arrives at 11
    H5
    arrives at 15
  5. H3 arrives H3 arrives and joins the ready queue. Ready queue: H3 → L.
    Time
    7 ms
    Running
    H2
    Ready queue
    H3 → L
    L
    3 of 3 ms left, priority 5
    H1
    done at 4: waited 0, turnaround 4
    H2
    1 of 4 ms left, priority 1
    H3
    4 of 4 ms left, priority 1
    H4
    arrives at 11
    H5
    arrives at 15
  6. H2 finishes, H3 runs H2 finishes at 8 ms. It waited 1 ms in all, and its turnaround (arrival to finish) was 5 ms. The scheduler picks H3, because its priority, 1, is the most important (a smaller number wins).
    Time
    8 ms
    Running
    H3
    Ready queue
    L
    L
    3 of 3 ms left, priority 5
    H1
    done at 4: waited 0, turnaround 4
    H2
    done at 8: waited 1, turnaround 5
    H3
    4 of 4 ms left, priority 1
    H4
    arrives at 11
    H5
    arrives at 15
  7. H4 arrives H4 arrives and joins the ready queue. Ready queue: H4 → L.
    Time
    11 ms
    Running
    H3
    Ready queue
    H4 → L
    L
    3 of 3 ms left, priority 5
    H1
    done at 4: waited 0, turnaround 4
    H2
    done at 8: waited 1, turnaround 5
    H3
    1 of 4 ms left, priority 1
    H4
    4 of 4 ms left, priority 1
    H5
    arrives at 15
  8. H3 finishes, H4 runs H3 finishes at 12 ms. It waited 1 ms in all, and its turnaround (arrival to finish) was 5 ms. The scheduler picks H4, because its priority, 1, is the most important (a smaller number wins).
    Time
    12 ms
    Running
    H4
    Ready queue
    L
    L
    3 of 3 ms left, priority 5
    H1
    done at 4: waited 0, turnaround 4
    H2
    done at 8: waited 1, turnaround 5
    H3
    done at 12: waited 1, turnaround 5
    H4
    4 of 4 ms left, priority 1
    H5
    arrives at 15
  9. H5 arrives H5 arrives and joins the ready queue. Ready queue: H5 → L.
    Time
    15 ms
    Running
    H4
    Ready queue
    H5 → L
    L
    3 of 3 ms left, priority 5
    H1
    done at 4: waited 0, turnaround 4
    H2
    done at 8: waited 1, turnaround 5
    H3
    done at 12: waited 1, turnaround 5
    H4
    1 of 4 ms left, priority 1
    H5
    4 of 4 ms left, priority 1
  10. H4 finishes, H5 runs H4 finishes at 16 ms. It waited 1 ms in all, and its turnaround (arrival to finish) was 5 ms. The scheduler picks H5, because its priority, 1, is the most important (a smaller number wins).
    Time
    16 ms
    Running
    H5
    Ready queue
    L
    L
    3 of 3 ms left, priority 5
    H1
    done at 4: waited 0, turnaround 4
    H2
    done at 8: waited 1, turnaround 5
    H3
    done at 12: waited 1, turnaround 5
    H4
    done at 16: waited 1, turnaround 5
    H5
    4 of 4 ms left, priority 1
  11. H5 finishes, L runs H5 finishes at 20 ms. It waited 1 ms in all, and its turnaround (arrival to finish) was 5 ms. The scheduler picks L, because it is the only process ready.
    Time
    20 ms
    Running
    L
    Ready queue
    empty
    L
    3 of 3 ms left, priority 5
    H1
    done at 4: waited 0, turnaround 4
    H2
    done at 8: waited 1, turnaround 5
    H3
    done at 12: waited 1, turnaround 5
    H4
    done at 16: waited 1, turnaround 5
    H5
    done at 20: waited 1, turnaround 5
  12. L finishes L finishes at 23 ms. It waited 20 ms in all, and its turnaround (arrival to finish) was 23 ms. Every process is done at 23 ms. The average wait is 4 ms.
    Time
    23 ms
    Running
    idle
    Ready queue
    empty
    L
    done at 23: waited 20, turnaround 23
    H1
    done at 4: waited 0, turnaround 4
    H2
    done at 8: waited 1, turnaround 5
    H3
    done at 12: waited 1, turnaround 5
    H4
    done at 16: waited 1, turnaround 5
    H5
    done at 20: waited 1, turnaround 5
    Average wait
    4 ms
    Average turnaround
    7.83 ms
    Average response
    4 ms

Without aging, L waited 20 ms: it only ran once every important job was done. If important jobs kept coming for ever, L would never run. That is starvation (Lesson 3.2) again.

With aging, L’s priority crept up while it waited, until it reached 1 and drew level with the important jobs. Ties go to the job that arrived first, which was L, so L ran and waited only 8 ms. The important jobs paid a little (H3, for example, waited 4 ms instead of 1 ms), but nobody was left behind.

The multi-level feedback queue

Priorities raise a new question: who sets them? A user can mark a few processes, but most programs never say whether they are interactive. The multi-level feedback queue (MLFQ) works it out by watching each job.

MLFQ keeps several ready queues, one per priority level. It always runs a job from the highest level that has one, round robin within the level. Higher levels get short slices, lower levels longer ones. Then it follows a few rules:

  1. A new job starts at the top level.
  2. A job that uses up its whole slice moves down one level. It behaves like a long, CPU-hungry job.
  3. A job that gives up the CPU before its slice ends, for example to wait for I/O, stays at its level. It behaves like an interactive job.
  4. Every so often, a priority boost moves every job back to the top.

Here Long is a CPU-bound job that needs 20 ms. Edit is a text editor: it computes for 1 ms at a time, then waits 3 ms for the next key press. The levels have slices of 2, 4 and 8 ms. The bottom pane runs the same two jobs under round robin with a 4 ms slice. Switch to the Queues view to see the levels.

MLFQ learns which job is interactive

  1. Long arrives Long arrives and joins the ready queue at the top level. Ready queue: Long.
    Time
    0 ms
    Running
    idle
    Ready queue
    Long
    Waiting for I/O
    none
    Long
    20 of 20 ms left, level 1
    Edit
    arrives at 1
  2. Long runs The scheduler picks Long, because it is the only process ready.
    Time
    0 ms
    Running
    Long
    Ready queue
    empty
    Waiting for I/O
    none
    Long
    20 of 20 ms left, level 1
    Edit
    arrives at 1
  3. Edit arrives Edit arrives and joins the ready queue at the top level. Ready queue: Edit.
    Time
    1 ms
    Running
    Long
    Ready queue
    Edit
    Waiting for I/O
    none
    Long
    19 of 20 ms left, level 1
    Edit
    6 of 6 ms left, level 1
  4. Slice over: Long → Edit Long has used its whole 2 ms slice. The timer interrupt gives the CPU back to the kernel, and Long moves down to level 2. The scheduler picks Edit, because it is first in the highest level that has a job (level 1, slice 2 ms).
    Time
    2 ms
    Running
    Edit
    Ready queue
    Long
    Waiting for I/O
    none
    Long
    18 of 20 ms left, level 2
    Edit
    6 of 6 ms left, level 1
  5. Edit starts I/O, Long runs Edit asks for I/O (3 ms) and gives up the CPU, keeping its level. The scheduler picks Long, because it is the only process ready.
    Time
    3 ms
    Running
    Long
    Ready queue
    empty
    Waiting for I/O
    Edit until 6
    Long
    18 of 20 ms left, level 2
    Edit
    5 of 6 ms left, level 1, in I/O until 6
  6. Edit's I/O is done Edit finishes its I/O and is ready to run again. Ready queue: Edit.
    Time
    6 ms
    Running
    Long
    Ready queue
    Edit
    Waiting for I/O
    none
    Long
    15 of 20 ms left, level 2
    Edit
    5 of 6 ms left, level 1
  7. Edit preempts Long Edit is at a higher level than Long, so Long is preempted. The scheduler picks Edit, because it is first in the highest level that has a job (level 1, slice 2 ms).
    Time
    6 ms
    Running
    Edit
    Ready queue
    Long
    Waiting for I/O
    none
    Long
    15 of 20 ms left, level 2
    Edit
    5 of 6 ms left, level 1
  8. Edit starts I/O, Long runs Edit asks for I/O (3 ms) and gives up the CPU, keeping its level. The scheduler picks Long, because it is the only process ready.
    Time
    7 ms
    Running
    Long
    Ready queue
    empty
    Waiting for I/O
    Edit until 10
    Long
    15 of 20 ms left, level 2
    Edit
    4 of 6 ms left, level 1, in I/O until 10
  9. Long moves down Long has used its whole slice, so it moves down to level 3. Nothing else is ready, so it keeps the CPU.
    Time
    8 ms
    Running
    Long
    Ready queue
    empty
    Waiting for I/O
    Edit until 10
    Long
    14 of 20 ms left, level 3
    Edit
    4 of 6 ms left, level 1, in I/O until 10
  10. Edit's I/O is done Edit finishes its I/O and is ready to run again. Ready queue: Edit.
    Time
    10 ms
    Running
    Long
    Ready queue
    Edit
    Waiting for I/O
    none
    Long
    12 of 20 ms left, level 3
    Edit
    4 of 6 ms left, level 1
  11. Edit preempts Long Edit is at a higher level than Long, so Long is preempted. The scheduler picks Edit, because it is first in the highest level that has a job (level 1, slice 2 ms).
    Time
    10 ms
    Running
    Edit
    Ready queue
    Long
    Waiting for I/O
    none
    Long
    12 of 20 ms left, level 3
    Edit
    4 of 6 ms left, level 1
  12. Edit starts I/O, Long runs Edit asks for I/O (3 ms) and gives up the CPU, keeping its level. The scheduler picks Long, because it is the only process ready.
    Time
    11 ms
    Running
    Long
    Ready queue
    empty
    Waiting for I/O
    Edit until 14
    Long
    12 of 20 ms left, level 3
    Edit
    3 of 6 ms left, level 1, in I/O until 14
  13. Edit's I/O is done Edit finishes its I/O and is ready to run again. Ready queue: Edit.
    Time
    14 ms
    Running
    Long
    Ready queue
    Edit
    Waiting for I/O
    none
    Long
    9 of 20 ms left, level 3
    Edit
    3 of 6 ms left, level 1
  14. Edit preempts Long Edit is at a higher level than Long, so Long is preempted. The scheduler picks Edit, because it is first in the highest level that has a job (level 1, slice 2 ms).
    Time
    14 ms
    Running
    Edit
    Ready queue
    Long
    Waiting for I/O
    none
    Long
    9 of 20 ms left, level 3
    Edit
    3 of 6 ms left, level 1
  15. Edit starts I/O, Long runs Edit asks for I/O (3 ms) and gives up the CPU, keeping its level. The scheduler picks Long, because it is the only process ready.
    Time
    15 ms
    Running
    Long
    Ready queue
    empty
    Waiting for I/O
    Edit until 18
    Long
    9 of 20 ms left, level 3
    Edit
    2 of 6 ms left, level 1, in I/O until 18
  16. Edit's I/O is done Edit finishes its I/O and is ready to run again. Ready queue: Edit.
    Time
    18 ms
    Running
    Long
    Ready queue
    Edit
    Waiting for I/O
    none
    Long
    6 of 20 ms left, level 3
    Edit
    2 of 6 ms left, level 1
  17. Slice over: Long → Edit Long has used its whole 8 ms slice. The timer interrupt gives the CPU back to the kernel, and Long goes to the back of the ready queue. The scheduler picks Edit, because it is first in the highest level that has a job (level 1, slice 2 ms).
    Time
    18 ms
    Running
    Edit
    Ready queue
    Long
    Waiting for I/O
    none
    Long
    6 of 20 ms left, level 3
    Edit
    2 of 6 ms left, level 1
  18. Edit starts I/O, Long runs Edit asks for I/O (3 ms) and gives up the CPU, keeping its level. The scheduler picks Long, because it is the only process ready.
    Time
    19 ms
    Running
    Long
    Ready queue
    empty
    Waiting for I/O
    Edit until 22
    Long
    6 of 20 ms left, level 3
    Edit
    1 of 6 ms left, level 1, in I/O until 22
  19. Edit's I/O is done Edit finishes its I/O and is ready to run again. Ready queue: Edit.
    Time
    22 ms
    Running
    Long
    Ready queue
    Edit
    Waiting for I/O
    none
    Long
    3 of 20 ms left, level 3
    Edit
    1 of 6 ms left, level 1
  20. Edit preempts Long Edit is at a higher level than Long, so Long is preempted. The scheduler picks Edit, because it is first in the highest level that has a job (level 1, slice 2 ms).
    Time
    22 ms
    Running
    Edit
    Ready queue
    Long
    Waiting for I/O
    none
    Long
    3 of 20 ms left, level 3
    Edit
    1 of 6 ms left, level 1
  21. Edit finishes, Long runs Edit finishes at 23 ms. It waited 1 ms in all, and its turnaround (arrival to finish) was 22 ms. The scheduler picks Long, because it is the only process ready.
    Time
    23 ms
    Running
    Long
    Ready queue
    empty
    Waiting for I/O
    none
    Long
    3 of 20 ms left, level 3
    Edit
    done at 23: waited 1, turnaround 22
  22. Long finishes Long finishes at 26 ms. It waited 6 ms in all, and its turnaround (arrival to finish) was 26 ms. Every process is done at 26 ms. The average wait is 3.5 ms.
    Time
    26 ms
    Running
    idle
    Ready queue
    empty
    Waiting for I/O
    none
    Long
    done at 26: waited 6, turnaround 26
    Edit
    done at 23: waited 1, turnaround 22
    Average wait
    3.5 ms
    Average turnaround
    24 ms
    Average response
    0.5 ms

Edit never used a whole slice, so it never left the top level, and each time it woke up it got the CPU almost at once: in total it waited 1 ms. Long sank to the bottom level, where it ran in long slices whenever Edit was waiting. Under plain round robin, Edit had to queue behind Long’s slices and waited 7 ms. MLFQ gave the interactive job fast responses without being told it was interactive.

Press Try it to change the levels. Give the top level a 1 ms slice and see what happens to Edit; set a boost and watch Long come back to the top.

Why the boost, and how to game the scheduler

Without rule 4, two things go wrong. If many interactive jobs keep the top levels busy, a job at the bottom may never run: starvation again. And a job that was CPU-bound for a while and then became interactive would be stuck at the bottom for ever. The periodic boost fixes both: everyone gets a fresh start.

Rule 3 has a weakness. A clever program could compute for almost a whole slice, then do a tiny I/O just before the slice ends. It never uses a whole slice, so it stays at the top and takes most of the CPU. This is gaming the scheduler. The fix in modern MLFQ designs is to count the total time a job has used at its level, across all its turns, and move it down when that total reaches the level’s allowance, whether or not it gave up the CPU in between.

In the real world

Windows schedules threads by priority levels, and the levels move. A thread that has been waiting for input gets a temporary boost when the input arrives, and the threads of the window in the foreground get a boost too, so the program you are using reacts quickly. The boost wears off as the thread uses CPU time. Starving threads get a boost from a background task that looks for threads that have not run for a long time: aging, by another name.

macOS and iOS do something similar with “quality of service” classes: a program marks its work as user-interactive, utility or background, and the system schedules it accordingly.

Recap

  • Priority scheduling always runs the most important ready process; in this course a smaller number is more important.
  • Low-priority jobs can starve. Aging raises a waiting job’s priority over time until it gets a turn.
  • MLFQ has several levels: new jobs start at the top, using a whole slice moves a job down, giving up the CPU early keeps it where it is.
  • So CPU-bound jobs sink and interactive jobs stay high, without anyone setting priorities.
  • A periodic boost prevents starvation; counting total time at a level stops programs from gaming the scheduler.

Quick check

Quick check

  1. How does MLFQ decide that a job is probably interactive?
  2. Why does MLFQ boost every job back to the top level from time to time?
  3. A program does a tiny I/O just before every slice ends, so it never uses a whole slice. What is it doing?
  4. What does aging do in priority scheduling?

Your progress is saved in this browser. Sign in to keep it on every device. Free, no password.

Join the discussion