Priority, starvation, and aging

See how priority picks ready work, why a ready task starves and how aging helps, and how a lock makes a high-priority task wait on a medium one. Advanced scheduling · Operating systems.

A nightly backup is given the lowest priority so that it never slows the website. A week later someone asks why it has not finished once. On another machine the most urgent task sits idle for an hour while a task of modest importance keeps the CPU. Both machines obey their priorities exactly. Why does a priority scheduler leave a task waiting, and is the fix the same in both cases?

Priority is a rule for choosing ready work

A priority scheduler gives every task a number and runs the ready task (one that could run now if it had the CPU) with the best number. This lesson uses one CPU, a lower number means a higher priority, and a tie goes to the task that arrived first. Systems differ on which direction wins, so every example has to say which. The scheduler is preemptive when a task that becomes ready can take the CPU from the running one, and non-preemptive when the running task keeps the CPU until it finishes or gives it up. Here a newcomer preempts only if its number is strictly better; an equal number never interrupts.

Take three tasks. Bak, a backup, has priority 6, arrives at 0 and needs 4 units of CPU. Alrt, an alert, has priority 1, arrives at 1 and needs 2. Rpt, a report, has priority 1, arrives at 2 and needs 2. Bak runs alone until 1, when Alrt's 1 beats 6 and takes the CPU with 3 units of Bak left. At 2 Rpt arrives with the same 1, which is not strictly better, so Alrt keeps running and Rpt waits. Alrt finishes at 3, Rpt runs from 3 to 5, and Bak resumes and finishes at 8. Bak waited 4 units: its finish time, minus its arrival, minus the 4 units of CPU it needed.

A strictly better arrival takes the CPU

Read the steps as text

  1. A lower number wins the CPU, and only a strictly better priority preempts. Priorities stay fixed.
  2. Process Bak arrives. Bak takes the CPU.
  3. Process Alrt arrives. Alrt preempts process Bak: priority 1 beats 6.
  4. Process Rpt arrives. Process Alrt keeps the CPU. Alrt finishes.
  5. Process Rpt takes the CPU.
  6. Process Rpt keeps the CPU. Rpt finishes.
  7. Process Bak takes the CPU.
  8. Process Bak keeps the CPU.
  9. Process Bak keeps the CPU. Bak finishes.

Starvation: ready, and passed over

Starvation means a ready task keeps losing the comparison and nothing bounds how long that goes on. Strict priority allows it, because the task with the worst number reaches the CPU only when nothing better is ready.

Take five tasks. Arc, an archive job, has priority 8, arrives at 0 and needs 2 units. Four more tasks, Hi1 to Hi4, each have priority 2 and need 2 units, and they arrive at 0, 2, 4 and 6. Each one finishes as the next arrives, so a priority-2 task is always ready when the CPU frees up and Arc cannot win while that stream lasts. It first runs at 8 and waits 8 units in a run of 10. That is a delay, not yet starvation: starvation is the claim that the wait has no bound, and it holds here only if the stream of priority-2 tasks never ends.

Aging raises the priority of a task as it waits. Our rule: every 2 units a task waits in the ready line, its number falls by 2, and it never goes below 0. Arc's number goes 8, 6, 4, 2 at times 0, 2, 4 and 6. At 6 Hi4 arrives with priority 2, which ties with Arc's 2, and Arc arrived first, so Arc runs from 6 to 8. It now waits 6 units instead of 8. The price is paid by Hi4, which waits 2 units instead of 0, because aging applies to every waiting task.

The same jobs without and with aging

Read the steps as text

  1. A lower number wins the CPU, and only a strictly better priority preempts. No aging: priorities stay fixed. With aging: a waiting priority moves 2 toward 0 every 2 units.
  2. Both runs: Processes Hi1 and Arc arrive. Hi1 takes the CPU.
  3. Both runs: Process Hi1 keeps the CPU. Hi1 finishes. With aging: Aging lifts process Arc from 8 to 6.
  4. Both runs: Process Hi2 arrives. Hi2 takes the CPU.
  5. Both runs: Process Hi2 keeps the CPU. Hi2 finishes. With aging: Aging lifts process Arc from 6 to 4.
  6. Both runs: Process Hi3 arrives. Hi3 takes the CPU.
  7. Both runs: Process Hi3 keeps the CPU. Hi3 finishes. With aging: Aging lifts process Arc from 4 to 2.
  8. Both runs: Process Hi4 arrives. No aging: Hi4 takes the CPU. With aging: Process Arc takes the CPU.
  9. No aging: Process Hi4 keeps the CPU. Hi4 finishes. With aging: Process Arc keeps the CPU. Arc finishes. Aging lifts Hi4 from 2 to 0.
  10. No aging: Process Arc takes the CPU. With aging: Process Hi4 takes the CPU.
  11. No aging: Process Arc keeps the CPU. Arc finishes. With aging: Process Hi4 keeps the CPU. Hi4 finishes.

Aging is a policy added to priority scheduling, not part of it, and it works only if the limit is good enough. With a limit of 5, Arc's number would stop at 5, which never beats a 2, and Arc would starve as before. If a larger number meant a higher priority, aging would add to the number instead of subtracting.

Priority inversion: blocked, not passed over

A lock lets one task at a time use shared data. A task that asks for a lock somebody else holds is blocked: it leaves the ready line and cannot be chosen until the lock is released. Priority ranks ready tasks only, so a blocked task has no say in the comparison.

Take three tasks and one lock. Lo has priority 8, arrives at 0, takes the lock at once and needs 3 units of CPU while holding it, 4 in all. Hi has priority 1, arrives at 1, preempts Lo and asks for the same lock at once, so it blocks and Lo runs again.

At 2 Med arrives with priority 5. It needs no lock, and 5 beats Lo's 8, so Med takes the CPU from Lo and runs from 2 to 5. Lo cannot release the lock until it runs again, and Hi cannot run until Lo releases it. Lo runs from 5 to 6 and releases the lock, and Hi runs from 6 to 8.

Hi was blocked for 5 units, 3 of them while Med ran and the rest while Lo finished its work under the lock, a wait no policy can remove. Hi finished at 8. Hi's block on Lo is priority inversion. A medium-priority task can extend that block: here Med, which ranks below Hi, delayed Lo and therefore held Hi back longer. If medium tasks kept arriving, Hi's wait would have no bound.

Under priority inheritance a lock holder runs at the better of its own priority and the best priority among the tasks it blocks, until it releases the lock. When Hi blocks at 1, Lo takes priority 1. Med's 5 does not beat that, so Lo keeps the CPU from 1 to 3, releases the lock, and goes back to 8. Hi runs from 3 to 5. Hi was blocked for 2 units and finished at 5 instead of 8. Med pays for it, finishing at 8 instead of 5.

A lock turns the order upside down

Read the steps as text

  1. A lower number wins the CPU, and only a strictly better priority preempts. One lock is shared. No inheritance: priorities stay fixed. Inheritance: the lock holder takes the best priority among the processes it blocks.
  2. Both runs: Process Lo arrives. Lo takes the CPU. Lo takes the lock.
  3. Both runs: Process Hi arrives. Hi preempts process Lo: priority 1 beats 8. Hi blocks on the lock that Lo holds. No inheritance: Lo takes the CPU. Inheritance: Lo inherits priority 1 from Hi. Lo takes the CPU.
  4. Both runs: Process Med arrives. No inheritance: Med preempts process Lo: priority 5 beats 8. Inheritance: Lo keeps the CPU. Lo releases the lock; process Hi gets it. Lo goes back to priority 8.
  5. No inheritance: Process Med keeps the CPU. Inheritance: Process Hi preempts process Lo: priority 1 beats 8. Hi releases the lock.
  6. No inheritance: Process Med keeps the CPU. Med finishes. Inheritance: Process Hi keeps the CPU. Hi finishes.
  7. No inheritance: Process Lo takes the CPU. Lo releases the lock; process Hi gets it. Inheritance: Process Med takes the CPU.
  8. No inheritance: Process Hi preempts process Lo: priority 1 beats 8. Hi releases the lock. Inheritance: Process Med keeps the CPU.
  9. No inheritance: Process Hi keeps the CPU. Hi finishes. Inheritance: Process Med keeps the CPU. Med finishes.
  10. Both runs: Process Lo takes the CPU. Lo finishes.

Recap

  • A priority scheduler runs the ready task with the best number; here only a strictly better arrival preempts, and the number direction and tie rule must be stated.
  • Starvation is a ready task passed over without bound, and aging helps only if its limit lets the waiting task tie or win.
  • Priority inversion is a high-priority task blocked on a lock held by a lower one, and inheritance shortens the block by lending the holder the higher priority.
Another way to limit inversion

One ceiling policy assigns each lock the best priority of any task that may use it, and immediately raises a holder to at least that ceiling. This lesson traces only inheritance. Livelock, where tasks keep changing state and make no progress, is a third kind of failure, covered in starvation, livelock, and priority inversion.

Next: Multilevel queues and feedback.