Priority Scheduling, Starvation and Ageing
Chapter Fifty
Syllabus topic Module 1, "CPU Scheduling - Scheduling Algorithms (... Priority ...)"
Pages 197 to 199 of 452
In one line
Every process carries a number saying how important it is, and the most important ready process runs.
The convention, stated
In this book, and in MU's text book, a SMALLER number means a HIGHER priority. So priority 1 beats priority 5.
There is no universal rule. Linux's nice value runs the other way, where a larger number means a lower priority, and some textbooks number upwards. An examination answer should say which convention it is using in one line, and then it cannot be marked wrong for the other.
The rule
| Chooses | the ready process with the highest priority, which is the smallest number here |
| Ties | broken by first come first served |
| Preemptive form | an arriving process of higher priority throws the running one off |
| Non-preemptive form | the arriving process waits at the head of the queue |
| Starvation | possible, and it is the defining problem of this algorithm |
Shortest job first is a special case of priority scheduling, where the priority is the inverse of the predicted next burst. That is worth knowing because it explains why the two share the same fault.
Where a priority comes from
| Kind | Set from | Example |
|---|---|---|
| Internal | something the system can measure | memory used, open files, the burst ratio, time limits |
| External | something outside the system | who is paying, which department, how urgent the work is |
Non-preemptive, worked in full
Schedule: priority
| Process | Arrival | Burst | Priority |
|---|---|---|---|
| P1 | 0 | 10 | 3 |
| P2 | 0 | 1 | 1 |
| P3 | 0 | 2 | 4 |
| P4 | 0 | 1 | 5 |
| P5 | 0 | 5 | 2 |
| Slice | Process | From | To |
|---|---|---|---|
| 1 | P2 | 0 | 1 |
| 2 | P5 | 1 | 6 |
| 3 | P1 | 6 | 16 |
| 4 | P3 | 16 | 18 |
| 5 | P4 | 18 | 19 |
| Process | Completion | Turnaround | Waiting |
|---|---|---|---|
| P1 | 16 | 16 | 6 |
| P2 | 1 | 1 | 0 |
| P3 | 18 | 18 | 16 |
| P4 | 19 | 19 | 18 |
| P5 | 6 | 6 | 1 |
Average turnaround time: 60 / 5 = 12
Average waiting time: 41 / 5 = 8.20
Context switches: 4
The five processes ran in priority order, 1, 2, 3, 4, 5, and P4 with the worst priority waited eighteen units for one unit of work.
Preemptive, worked in full
The same five processes and priorities, now arriving one unit apart so that preemption has something to do.
Schedule: priority (preemptive)
| Process | Arrival | Burst | Priority |
|---|---|---|---|
| P1 | 0 | 10 | 3 |
| P2 | 1 | 1 | 1 |
| P3 | 2 | 2 | 4 |
| P4 | 3 | 1 | 5 |
| P5 | 4 | 5 | 2 |
| Slice | Process | From | To |
|---|---|---|---|
| 1 | P1 | 0 | 1 |
| 2 | P2 | 1 | 2 |
| 3 | P1 | 2 | 4 |
| 4 | P5 | 4 | 9 |
| 5 | P1 | 9 | 16 |
| 6 | P3 | 16 | 18 |
| 7 | P4 | 18 | 19 |
| Process | Completion | Turnaround | Waiting |
|---|---|---|---|
| P1 | 16 | 16 | 6 |
| P2 | 2 | 1 | 0 |
| P3 | 18 | 16 | 14 |
| P4 | 19 | 16 | 15 |
| P5 | 9 | 5 | 0 |
Priority Scheduling, Starvation and Ageing
Average turnaround time: 54 / 5 = 10.80
Average waiting time: 35 / 5 = 7
Context switches: 6
P1 appears three times. It started, was thrown off at 1 by P2 of priority 1, resumed, was thrown off again at 4 by P5 of priority 2, and finally finished at 16. Its completion time is the end of its last slice, which is Chapter forty six's second warning, and its two extra appearances are two extra context switches.
Starvation, and the fix
Indefinite blocking, or starvation, is the problem of priority scheduling. A low priority process is ready, and a higher priority one is always ready too, so the low priority process never runs. On a loaded system it may never run at all.
The story every textbook tells is worth repeating because it fixes the idea: when an IBM 7094 at MIT was shut down in 1973 a low priority process was found that had been submitted in 1967 and had not yet run.
Ageing
Ageing is the fix: increase the priority of a process the longer it waits. However bad its priority starts, it improves while it waits, and eventually it is the best in the queue.
Worked as a sum. Priorities run from 127, the worst, to 0, the best, and a waiting process's priority number is reduced by 1 for every 15 minutes it waits.
minutes to reach priority 0 from 127 = 127 * 15 = 1905
hours = 1905 / 60 = 31.75
So the worst possible process is guaranteed to run within about thirty two hours, whatever else arrives. That turns "it might never run" into a number, which is Chapter thirty four's bounded waiting.
Ageing costs something. A process whose priority rises can overtake a process that genuinely is more important, so a real system usually resets a process's priority after it has run.
Distinctions that carry marks
| Non-preemptive priority | Preemptive priority | |
|---|---|---|
| A higher priority arrival | waits until the running process blocks or ends | throws the running process off |
| Context switches | fewer | more |
| Response for an urgent process | up to a whole burst | immediate |
| Used by | batch systems | every interactive and real time system |
| Starvation | Deadlock | |
|---|---|---|
| The process is | ready, and never chosen | blocked, waiting for something held |
| Others progress | yes | no |
| Fixed by | ageing | Chapters fifty six to sixty five |
What it does not mean
A priority is not a guarantee of speed. It decides order, not duration.
Preemptive priority is not unfair to the preempted process. It loses no work: its state is saved and it resumes exactly where it was.
Ageing is not the same as round robin. Round robin gives everybody a turn regardless of priority. Ageing keeps the priority order and makes the order change over time.
Priority Scheduling, Starvation and Ageing
Quick revision
- Priority scheduling runs the ready process with the highest priority.
In this book a smaller number is a higher priority, and an answer must say which convention it uses.
- Both forms exist. The preemptive form throws the running process off when a better one
arrives; the non-preemptive form makes it wait.
- Priorities are internal, from something the system can measure, or external, from
outside it.
- Shortest job first is priority scheduling with the priority set to the inverse of the next
burst.
- The problem is indefinite blocking, or starvation: a low priority process may never
run.
- The fix is ageing: raise a process's priority the longer it waits. From 127 to 0 at one
step per fifteen minutes is 1,905 minutes, about 31.75 hours, which is a bound.
- Under the preemptive form a process appears several times on the chart and its completion is
the end of the last one.
Test yourself
- State the rule and the convention you are using. The processor goes to the ready process
with the highest priority; in this answer a smaller number means a higher priority.
- Why must the convention be stated? Because books and systems differ: Linux's nice value
treats a larger number as a lower priority, and an answer is unreadable without saying which way round it is.
- Distinguish internal from external priorities. Internal priorities are computed from
quantities the system can measure, such as memory used or time limits. External ones come from outside, such as who is paying or how urgent the work is.
- How is shortest job first a priority algorithm? Its priority is the inverse of the
predicted next CPU burst: the shorter the burst, the higher the priority.
- What is the major problem with priority scheduling, and what is the solution? Indefinite
blocking, or starvation, of low priority processes. The solution is ageing: increasing a process's priority the longer it has waited. 6. Priorities run 127 to 0 and a waiting process gains one step every 15 minutes. What is the guaranteed bound? 127 times 15 equals 1,905 minutes, which is 1905 / 60 = 31.75 hours. 7. Under preemptive priority a process appears three times in the chart. Which slice gives its completion time, and what do the extra appearances cost? The last slice. Each extra appearance is an extra context switch, which is pure overhead.
The rest of this subject
These notes are cut from the University's printed syllabus. Open the syllabus itself, or the past papers, for the same subject.