Sub topic 15
Priority Scheduling, Starvation, and Aging
This chapter assumes you know the ready queue, the dispatcher, preemption, and how to compute waiting, turnaround, and response time from a Gantt chart. It also assumes you have seen FCFS and SJF. Priority scheduling generalizes both, and it introduces a failure mode they lack: starvation.
The idea
Each process carries a priority number. The scheduler always gives the CPU to the ready process with the highest priority. Equal priorities are usually broken by FCFS order (earlier arrival first).
Conventions differ between books and exam questions:
- Some sources say a lower number means higher priority, and others say the opposite.
- A GATE question normally states its convention. Read it before you draw anything.
- This chapter uses lower number = higher priority.
Priorities come from two kinds of source:
- Internal priorities are computed by the OS from measurable quantities, such as time limits, memory needs, the number of open files, or the ratio of I/O burst to CPU burst.
- External priorities are set outside the OS, for example by the user's role or the importance of the job.
SJF is a special case of priority scheduling. The priority is the predicted next CPU burst, so a shorter burst means a higher priority. SRTF is the preemptive version of the same rule.
Non-preemptive and preemptive variants
| Aspect | Non-preemptive | Preemptive |
|---|---|---|
| When the choice is made | Only when the running process finishes or blocks | Also whenever a process enters the ready queue |
| Effect of a high-priority arrival | It waits for the current burst to end | It takes the CPU immediately if its priority is strictly better |
| Context switches | Fewer | More |
| Response time for urgent work | Can be poor | Good |
Preemption happens only when the new process has a strictly better priority than the running one. A tie does not preempt.
Worked example 1: non-preemptive, all arrive at time 0
| Process | Burst | Priority |
|---|---|---|
| P1 | 10 | 3 |
| P2 | 1 | 1 |
| P3 | 2 | 4 |
| P4 | 1 | 5 |
| P5 | 5 | 2 |
Run order is by priority: P2, P5, P1, P3, P4.
Gantt chart: P2 [0–1], P5 [1–6], P1 [6–16], P3 [16–18], P4 [18–19].
Every arrival time is 0, so waiting time equals the start time, and turnaround time equals the finish time.
- Waiting times: P1 = 6, P2 = 0, P3 = 16, P4 = 18, P5 = 1. The average is 41/5 = 8.2.
- Turnaround times: 16, 1, 18, 19, 6. The average is 60/5 = 12.
Worked example 2: preemptive with arrivals
| Process | Arrival | Burst | Priority |
|---|---|---|---|
| P1 | 0 | 4 | 3 |
| P2 | 1 | 3 | 1 |
| P3 | 2 | 2 | 2 |
- At t = 0 only P1 is ready, so it runs.
- At t = 1 P2 arrives with priority 1, which beats 3, so P2 preempts P1. P1 has 3 units left.
- At t = 2 P3 arrives with priority 2. P2 holds priority 1, which is better, so there is no preemption.
- P2 finishes at t = 4. P3 (priority 2) beats P1 (priority 3), so P3 runs from 4 to 6.
- P1 resumes from 6 and finishes at 9.
Gantt chart: P1 [0–1], P2 [1–4], P3 [4–6], P1 [6–9].
| Process | Finish | Turnaround = finish − arrival | Waiting = turnaround − burst | Response = first run − arrival |
|---|---|---|---|---|
| P1 | 9 | 9 | 5 | 0 |
| P2 | 4 | 3 | 0 | 0 |
| P3 | 6 | 4 | 2 | 2 |
Waiting time must include the gap while P1 sat preempted. Taking only "first start minus arrival" gives the response time, not the waiting time.
Starvation
A process is starved (also called indefinitely blocked) when it is ready to run but never gets the CPU. In priority scheduling, starvation occurs if higher-priority processes keep arriving. A low-priority process then waits as long as the stream continues.
Folklore says that when MIT shut down its IBM 7094 in 1973, it found a low-priority process that had been submitted in 1967 and had never run.
Starvation is different from the convoy effect:
- The convoy effect is a temporary slowdown. Short jobs queue behind one long job, and they all finish once that job ends.
- Starvation has no guaranteed end.
Aging
Aging is the standard fix. The scheduler gradually improves the priority of a process that has waited a long time. Eventually its priority is good enough to win the CPU.
The simulation below uses preemptive priority scheduling in unit time steps. With age_step, every age_step ticks a process spends waiting in the ready queue improves its effective priority by 1. Process L has a bad priority (9), and a stream of priority-1 jobs arrives back to back.
def simulate(procs, age_step=None):
"""procs: name -> (arrival, burst, priority). Lower number = higher priority.
Returns finish times. Preemptive; ties go to the earlier arrival."""
remaining = {n: b for n, (a, b, p) in procs.items()}
waited = {n: 0 for n in procs}
finish = {}
t = 0
while len(finish) < len(procs):
ready = [n for n, (a, _, _) in procs.items()
if a <= t and n not in finish]
if not ready:
t += 1
continue
def key(n):
a, _, p = procs[n]
boost = waited[n] // age_step if age_step else 0
return (max(0, p - boost), a, n)
run = min(ready, key=key)
remaining[run] -= 1
for n in ready:
if n != run:
waited[n] += 1
t += 1
if remaining[run] == 0:
finish[run] = t
return finish
procs = {
"L": (0, 2, 9),
"H1": (0, 3, 1),
"H2": (3, 3, 1),
"H3": (6, 3, 1),
"H4": (9, 3, 1),
"H5": (12, 3, 1),
}
print(simulate(procs)["L"]) # 17: L runs after all five H jobs
print(simulate(procs, age_step=1)["L"]) # 10: L wins at t = 8Without aging, L finishes at time 17, so its waiting time is 17 − 2 = 15. With aging, L has waited 8 ticks by t = 8, and its effective priority has improved from 9 to 1. That ties with H3, and the earlier arrival wins, so L finishes at 10.
Related problem: priority inversion
Suppose a low-priority process holds a lock that a high-priority process needs. A medium-priority process then preempts the low one, so the lock is never released. The high-priority process is effectively blocked behind the medium one. This is priority inversion. Priority inheritance is the usual remedy: the lock holder temporarily takes the priority of the highest-priority process waiting on the lock.
Common errors
- Reading the priority direction backwards.
- Preempting on a tie. Preempt only when the new priority is strictly better.
- Forgetting to take the CPU idle when no process has arrived. This shifts every later time.
- Dropping the preempted time from waiting time. Use waiting = turnaround − burst.
- Calling any long wait "starvation". Starvation requires waiting that is unbounded because of a continuing stream of better processes.
Exercise
Using preemptive priority scheduling (lower number = higher priority), find the waiting time of P1. P1 has arrival 0, burst 3, priority 2. P2 has arrival 1, burst 2, priority 1. P3 has arrival 2, burst 1, priority 3.
Show answer
P1 runs [0–1]. P2 arrives at 1 with a better priority and runs [1–3]. P1 resumes [3–5], and P3 runs [5–6]. P1 finishes at 5, so its turnaround is 5 − 0 = 5 and its waiting time is 5 − 3 = 2.
Exercise
Priorities run from 0 (best) to 127 (worst). Aging improves a waiting process's priority by 1 every 15 minutes. How long does a process at priority 127 need to reach priority 0?
Show answer
It needs 127 improvements of 15 minutes each, which is 127 × 15 = 1905 minutes, or about 31.75 hours.