OS Notes
Complete guide to Priority CPU scheduling algorithm covering preemptive and non-preemptive versions, starvation problem, aging solution, and priority assignment methods.
Introduction
In real life, not all tasks are equally important. When a hospital emergency room receives patients, they do not treat them in arrival order — a heart attack patient gets immediate attention over someone with a sprained ankle, regardless of who arrived first. This is priority scheduling: assigning importance levels to tasks and serving the most important ones first.
Priority scheduling is a CPU scheduling algorithm where each process is assigned a priority number. The CPU is allocated to the process with the highest priority. It can be either preemptive (a higher-priority process interrupts the current one) or non-preemptive (the current process finishes its burst before the higher-priority process runs).
How Priority is Assigned
Priorities can be determined by various factors:
Internal factors (set by OS):
- Memory requirements (less memory = higher priority)
- CPU burst time (shorter burst = higher priority, making it similar to SJF)
- Number of open files
- Ratio of I/O burst to CPU burst
External factors (set by administrator/user):
- Process importance to the organization
- Paid priority (cloud computing tiers)
- Political factors (department importance)
- User preference
Note: In some systems, a lower number means higher priority (Unix/Linux uses 0 = highest), while in others, a higher number means higher priority. Always check the convention being used.
Non-Preemptive Priority Scheduling Example
| Process | Arrival Time | Burst Time | Priority (lower = higher) |
|---|---|---|---|
| P1 | 0 | 10 | 3 |
| P2 | 1 | 1 | 1 |
| P3 | 2 | 2 | 4 |
| P4 | 3 | 1 | 5 |
| P5 | 4 | 5 | 2 |
Gantt Chart (Non-Preemptive)
Since it is non-preemptive, P1 runs to completion first (it started at time 0). Then the scheduler picks from available processes by priority.
After P1 finishes at time 10: Available = P2(pri=1), P3(pri=4), P4(pri=5), P5(pri=2). P2 has highest priority (1), runs next. Then P5 (pri=2), then P3 (pri=4), then P4 (pri=5).
Calculations
| Process | Completion | Turnaround | Waiting |
|---|---|---|---|
| P1 | 10 | 10 | 0 |
| P2 | 11 | 10 | 9 |
| P3 | 18 | 16 | 14 |
| P4 | 19 | 16 | 15 |
| P5 | 16 | 12 | 7 |
Average Waiting Time = (0 + 9 + 14 + 15 + 7) / 5 = 9 ms
Preemptive Priority Scheduling Example
With preemption, a newly arriving higher-priority process immediately takes over the CPU.
Using the same processes:
| Time 0 | P1 starts (only process, pri=3) |
| Time 1 | P2 arrives (pri=1). 1 < 3, so preempt P1. P2 runs. |
| Time 2 | P2 finishes. P3 arrives (pri=4). Ready: P1(pri=3,rem=9), P3(pri=4). P1 runs (higher priority) |
| Time 4 | P5 arrives (pri=2). 2 < 3, so preempt P1. P5 runs. |
| Time 9 | P5 finishes. Ready: P1(pri=3,rem=7), P3(pri=4), P4(pri=5). P1 runs. |
| Time 16 | P1 finishes. Ready: P3(pri=4), P4(pri=5). P3 runs. |
| Time 18 | P3 finishes. P4 runs. |
| Time 19 | P4 finishes. |
Gantt Chart (Preemptive)
The Starvation Problem
The biggest issue with priority scheduling is starvation (also called indefinite blocking). A low-priority process may never execute if higher-priority processes keep arriving. Imagine a low-priority print job in a system where interactive processes constantly need the CPU — the print job might wait hours or even days.
A real historical example: when they shut down the IBM 7094 at MIT in 1973, they found a low-priority process that had been submitted in 1967 and never got to run — it starved for 6 years!
Solution: Aging
Aging is the standard solution to starvation. The idea is simple: gradually increase the priority of processes that have been waiting a long time. For example, every 15 minutes of waiting, boost the process's priority by 1 level. Eventually, even the lowest-priority process will have its priority raised high enough to execute.
Priority Inversion Problem
Priority inversion occurs when a high-priority process is indirectly blocked by a low-priority process. This happens when they share a resource:
- Low-priority process L acquires a lock on resource R
- High-priority process H arrives and needs resource R
- H is blocked waiting for L to release R
- Medium-priority process M arrives and preempts L (since M has higher priority than L)
- Now H is effectively blocked by M, even though H has higher priority than M
This exact bug caused the Mars Pathfinder mission computer to repeatedly reset in 1997. The solution is priority inheritance: temporarily boost L's priority to H's level while L holds the resource H needs, preventing M from preempting L.
Implementation in C
Advantages and Disadvantages
Advantages
- Handles urgency — Critical processes get immediate attention
- Flexible — Priorities can be assigned based on any criteria
- Good for real-time systems — Time-critical tasks get guaranteed service
Disadvantages
- Starvation — Low-priority processes may never execute (solved by aging)
- Priority inversion — Can cause unpredictable blocking (solved by priority inheritance)
- Difficulty in assigning priorities — Subjective and may lead to gaming the system
Real-World Analogy
Priority scheduling is like an airport with different boarding groups. First-class passengers board first (highest priority), then business class, then economy. If the plane is nearly full and more first-class passengers keep arriving, economy passengers might never board. Aging is like the airline rule that bumped passengers get priority on the next flight — the longer you wait, the higher your priority becomes.
Key Takeaways
- Priority scheduling assigns importance levels and serves highest-priority processes first
- It can be preemptive (interrupt current process) or non-preemptive (wait for current to finish)
- Starvation is the main problem — solved by aging (gradually increasing waiting processes' priority)
- Priority inversion can cause high-priority processes to be blocked by low-priority ones
- Priority inheritance solves inversion by temporarily boosting the blocking process's priority
- Most real OS combine priority scheduling with other algorithms (e.g., Round Robin within each priority level)
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Priority Scheduling.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Operating Systems topic.
Search Terms
operating-systems, operating systems, operating, systems, cpu, scheduling, priority, priority scheduling
Related Operating Systems Topics