Priority Scheduling in OS
Priority scheduling is a CPU scheduling algorithm in OS, where
- Each process is assigned a priority
- CPU scheduler always selects the process that has the highest priority in the ready queue.
Note: In most systems, a smaller priority number indicates a higher priority (for example, Priority 1 is higher than Priority 3), though some operating systems use the opposite convention.
How Priority Scheduling Works
The working steps of Priority Scheduling are:
- All processes enter the ready queue based on their arrival time.
- Each process is assigned a priority.
- The scheduler selects the process with the highest priority.
- If two processes have the same priority, FCFS (First-Come, First-Served) is used to break the tie.
- The selected process executes until it completes (Non-Preemptive) or until a higher-priority process arrives (Preemptive).
- The process repeats until all processes are completed.
Types of Priority Scheduling
1. Non-Preemptive Priority Scheduling
- Once a process gets the CPU, it continues until it completes.
- A newly arrived higher-priority process must wait.
2. Preemptive Priority Scheduling
- If a higher-priority process arrives while another process is executing, the current process is interrupted.
- The higher-priority process immediately gets the CPU.
Example
| Process | Arrival Time | Burst Time | Priority |
|---|---|---|---|
| P1 | 0 | 6 | 3 |
| P2 | 2 | 4 | 1 |
| P3 | 3 | 2 | 2 |
Assuming smaller number = higher priority:
- At time 0, P1 starts.
- At time 2, P2 arrives with higher priority, so:
- Preemptive: P1 is interrupted, and P2 executes.
- Non-Preemptive: P1 continues until completion.
Characteristics of Priority Scheduling
- Executes the highest-priority process first.
- Can be preemptive or non-preemptive.
- Every process has a priority value.
- FCFS is used when priorities are equal.
- Suitable for systems where important tasks must run first.
Advantages
- Fast execution of important processes.
- Flexible because priorities can be changed.
- Suitable for real-time and critical applications.
- Better response for high-priority tasks.
Disadvantages
- Low-priority processes may wait for a long time (starvation).
- Priority assignment may be difficult.
- Frequent preemption can increase context switching.
- Less fair than Round Robin scheduling.
Starvation
Starvation occurs when a low-priority process keeps waiting because higher-priority processes continue to arrive.
Solution: Aging
Aging gradually increases the priority of a waiting process over time so that every process eventually gets CPU time.
Performance Measures
The performance of Priority Scheduling is evaluated using:
- Completion Time (CT): Time when a process finishes execution.
- Waiting Time (WT): Total time a process waits in the ready queue.
- Turnaround Time (TAT): Total time from arrival to completion.
- Response Time (RT): Time from arrival until the process first gets the CPU.
- Average Waiting Time (AWT): Average waiting time of all processes.
- Average Turnaround Time (ATAT): Average turnaround time of all processes.
Applications
- Real-time operating systems.
- Embedded systems.
- Medical monitoring systems.
- Industrial control systems.
- Network packet scheduling.
- Emergency and mission-critical applications.
When to Use Priority Scheduling
Use Priority Scheduling when:
- Some tasks are more important than others.
- Critical processes need immediate CPU access.
- Real-time response is required.
- System performance depends on executing high-priority tasks first.
FAQs
1. What is Priority Scheduling in OS?
Priority Scheduling is a CPU scheduling algorithm that executes the process with the highest priority before lower-priority processes.
2. What are the types of Priority Scheduling?
There are two types:
- Preemptive Priority Scheduling
- Non-Preemptive Priority Scheduling
3. What is starvation in Priority Scheduling?
Starvation occurs when low-priority processes wait indefinitely because higher-priority processes keep getting the CPU.
4. How can starvation be prevented?
Starvation is prevented using aging, which gradually increases the priority of waiting processes.
5. Is Priority Scheduling preemptive or non-preemptive?
It can be implemented as either preemptive or non-preemptive, depending on the operating system.