Priority Scheduling — Step-by-Step With Animated Numericals
Press Next → or use ← → arrow keys
The Story That Explains Priority Scheduling
That is priority scheduling: every process carries a priority number, and the CPU always goes to the most important ready job — not the first to arrive.
Following Galvin, a smaller priority number means higher priority — priority 1 beats priority 5. (Some systems flip this, so always check the question's convention.) Fun fact: SJF is just priority scheduling where the priority is the next CPU burst.
The Two Flavours
TAT = CT − AT, WT = TAT − BT, RT = start − AT. Only the selection rule — highest priority — is new.AT arrival, BT burst, CT completion, TAT turnaround, WT waiting, PR priority number. In a preemptive tie between equal priorities, fall back to arrival order (FCFS).
Non-Preemptive Priority
| Proc | AT | BT | PR | CT | TAT | WT |
|---|---|---|---|---|---|---|
| P1 | 0 | 4 | 2 | 4 | 4 | 0 |
| P2 | 1 | 3 | 3 | 9 | 8 | 5 |
| P3 | 2 | 1 | 4 | 10 | 8 | 7 |
| P4 | 3 | 5 | 5 | 15 | 12 | 7 |
| P5 | 4 | 2 | 1 | 6 | 2 | 0 |
| Averages → | 6.80 | 3.80 | ||||
P1 runs 0→4 (it was alone). At t=4 the ready set is {P2·3, P3·4, P4·5, P5·1} — P5 has the best priority (1), so it runs next, then P2, P3, P4. Avg WT = 3.80 ms.
Preemptive — But Nothing Preempts
Same data, now preemptive — yet the chart is unchanged. The reason: the only high-priority job, P5 (priority 1), arrives at t=4 — exactly when P1 finishes. There's nothing to interrupt, so no preemption ever fires. A great reminder that "preemptive" describes the rule, not a guarantee that preemption happens. Avg WT = 3.80 ms.
Preemption in Action
| Proc | AT | BT | PR | CT | TAT | WT |
|---|---|---|---|---|---|---|
| P1 | 0 | 6 | 4 | 15 | 15 | 9 |
| P2 | 1 | 4 | 2 | 8 | 7 | 3 |
| P3 | 3 | 3 | 1 | 6 | 3 | 0 |
| P4 | 5 | 2 | 3 | 10 | 5 | 3 |
| Averages → | 7.50 | 3.75 | ||||
P2 (pr 2) preempts P1 (pr 4) at t=1; then P3 (pr 1, the best) preempts P2 at t=3. P3 finishes, P2 resumes, P4 runs, and the low-priority P1 finally completes last. Avg WT = 3.75 ms.
The Preemption Timeline
Notice P1 — the lowest priority — arrived first yet finished last, waiting 9 units. That asymmetry is exactly what leads to the starvation problem next.
Where Do Priorities Come From?
| Internal Priority | External Priority | |
|---|---|---|
| Set by | The OS, automatically | A system administrator / policy |
| Based on | Measurable quantities — memory needs, time limits, open files, I/O-to-CPU ratio | Importance of the user, payment tier, department, politics |
| Nature | Objective & dynamic | Subjective & static |
A scheduler often combines an internal component the OS computes from resource usage with an external component reflecting business importance — a paid job may outrank a free one even with identical resource profiles.
The Starvation Problem
If a steady stream of high-priority jobs keeps arriving, a low-priority process can wait forever — this is starvation. It's the same danger SJF and SRTF face for long jobs, and it needs the same cure.
The Solution — Aging
Aging gradually raises the priority of a waiting process — e.g. improve its number by one every few time units. Eventually even the lowest-priority job climbs high enough to be scheduled, so nothing starves. It's the standard fix, and it preserves priority scheduling's responsiveness for important jobs.
Priority vs FCFS vs SJF vs Round Robin
| Feature | Priority | FCFS | SJF | Round Robin |
|---|---|---|---|---|
| Selection rule | Highest priority | First arrived | Shortest burst | Time-slice rotation |
| Preemption | Either variant | No | Either (SJF/SRTF) | Always |
| Starvation | Yes (fix: aging) | No | Yes (long jobs) | No |
| Overhead | Medium | Lowest | Needs burst prediction | Switch per slice |
| Real-time fit | Excellent | Poor | Poor | Good (soft RT) |
| Special case of | — | Priority = arrival | Priority = burst | — |
FCFS is priority scheduling with priority = arrival time; SJF is priority scheduling with priority = burst. Priority scheduling is the umbrella they both live under.
Advantages, Disadvantages & Uses
Priority Inversion — A Cautionary Tale
On Mars, a high-priority bus task blocked waiting for a lock held by a low-priority task — while a medium-priority task ran freely and never let the low one release it. The watchdog thought the system had hung and kept rebooting the rover. JPL fixed it remotely with priority inheritance: the lock-holder temporarily inherits the waiter's high priority.
Seven Rules for Priority Scheduling
Priority Scheduling — Mastered
Pick the best priority, draw the Gantt chart (marking preemptions), and compute the metrics. You've seen non-preemptive, "preemptive but nothing preempts," and real mid-execution preemption — plus starvation, aging, and priority inversion.
The last big single-queue algorithm is Round Robin (fair time-slicing), and then Multilevel Queue and Multilevel Feedback Queue combine priority with round-robin for real production schedulers.
🏅 End of tutorial · Press ← to review, or click Restart