Overview
Priority Scheduling is a CPU scheduling algorithm where each process is assigned a priority value, and the CPU executes the highest-priority process among those ready to run. Lower priority numbers indicate higher priority (priority 0 is highest).This implementation is non-preemptive. Once a process starts execution, it runs to completion even if a higher-priority process arrives.
A preemptive variant exists where a running process can be interrupted if a higher-priority process arrives. This implementation uses the non-preemptive version.
How It Works
The Priority Scheduling algorithm follows these steps:- At each decision point, identify all processes that have arrived and are waiting
- Among available processes, select the one with the highest priority (lowest number)
- Execute that process to completion (non-preemptive)
- If multiple processes have the same priority, use arrival time as tie-breaker
- Repeat until all processes complete
Algorithm Parameters
| Parameter | Type | Description |
|---|---|---|
llegada | Array | Arrival times for each process |
cpu | Array | CPU burst times (execution duration) |
prioridad | Array | Priority values (0 = highest priority) |
Implementation
Here’s the core Priority Scheduling algorithm logic:Example Walkthrough
Let’s simulate Priority Scheduling with four processes:| Process | Arrival Time | CPU Time | Priority |
|---|---|---|---|
| P0 | 0 | 4 | 2 |
| P1 | 1 | 3 | 0 |
| P2 | 2 | 1 | 1 |
| P3 | 3 | 5 | 3 |
Execution Timeline:
Step-by-Step:
-
Time 0: P0 arrives and starts (only process available)
- P0 priority: 2
- Runs to completion (non-preemptive)
-
Time 4: P0 completes
- Available: P1 (priority 0), P2 (priority 1), P3 (priority 3)
- P1 selected (highest priority: 0)
- Runs to completion
-
Time 7: P1 completes
- Available: P2 (priority 1), P3 (priority 3)
- P2 selected (highest priority: 1)
- Runs to completion
-
Time 8: P2 completes
- Available: P3 (priority 3)
- P3 selected (only process left)
- Runs to completion
- Time 13: P3 completes, all done
Results:
| Process | Priority | Start | Finish | Waiting | Turnaround |
|---|---|---|---|---|---|
| P0 | 2 | 0 | 4 | 0 | 4 |
| P1 | 0 | 4 | 7 | 3 | 6 |
| P2 | 1 | 7 | 8 | 5 | 6 |
| P3 | 3 | 8 | 13 | 5 | 10 |
Notice how P1 (highest priority) waits for P0 to finish because this is non-preemptive. In a preemptive version, P1 would interrupt P0.
Key Characteristics
Advantages
- Flexible control - Explicit priority assignment
- Critical tasks first - High-priority processes get CPU quickly
- Real-time support - Can guarantee critical deadlines
- Better than FIFO for mixed-importance workloads
- Simple to implement and understand
Disadvantages
- Starvation risk - Low-priority processes may never execute
- Priority assignment can be difficult to determine
- Not optimal for average waiting time
- Requires priority knowledge - may not be available
- Unfair to low-priority processes
Tie-Breaking Rules
When multiple processes have the same priority:- First tie-breaker: Earliest arrival time (FIFO within priority)
- Second tie-breaker: Lowest process ID
Preventing Starvation
In production systems, starvation is prevented using aging:Preemptive vs Non-Preemptive
Non-Preemptive (This Implementation)
- Process runs to completion once started
- Simpler to implement
- Lower overhead (fewer context switches)
- Can delay critical processes if long process runs first
Preemptive Variant
- Running process can be interrupted
- Better response time for high-priority arrivals
- Higher overhead (more context switches)
- More complex to implement
Performance Characteristics
- Time Complexity: O(n²) worst case (n iterations × sorting)
- Space Complexity: O(n) for process data
- Response Time: Excellent for high-priority, poor for low-priority
- Throughput: Depends on priority distribution
- Fairness: Unfair by design (priority-based)
Priority Assignment Strategies
How to assign priorities in real systems:Static Priorities
- Set at process creation
- Never change during execution
- Simple but inflexible
- Examples: system processes (priority 0), user processes (priority 10)
Dynamic Priorities
- Adjust during execution based on behavior
- More flexible and adaptive
- Examples:
- I/O-bound processes get higher priority
- CPU-bound processes get lower priority
- Aging increases priority over time
User-Assigned
- Users specify importance (e.g.,
nicevalues in Unix) - System may adjust based on user permissions
- Balance user control with system fairness
Use Cases
Priority Scheduling works best for:
- Real-time operating systems
- Systems with critical/non-critical task mix
- Embedded systems with interrupt handlers
- Background vs foreground task management
- Any scenario requiring explicit importance control
Real-World Examples
- Operating Systems: Kernel processes have higher priority than user processes
- Network Routers: Voice/video packets get higher priority than file transfers
- Embedded Systems: Interrupt handlers run at highest priority
- Databases: Transaction commits get higher priority than reads
Comparison with Other Algorithms
| Metric | FIFO | SJF | Priority |
|---|---|---|---|
| Flexibility | None | None | High |
| Starvation Risk | No | Yes | Yes |
| Avg Waiting Time | Medium | Optimal | Depends |
| Real-time Support | Poor | Poor | Excellent |
| Implementation | Simplest | Simple | Moderate |
Try It Out
Use the simulator to experiment with Priority Scheduling:- Assign different priorities to processes
- Observe how high-priority processes jump ahead
- Test starvation scenarios (many high-priority arrivals)
- Compare with SJF by treating CPU time as priority
- Try equal priorities to see FIFO behavior
Related Algorithms
- SJF - Special case where priority = CPU time
- MLFQ - Dynamic priority adjustment with multiple queues
- Round Robin - Can be combined with priority levels