- OS - Home
- OS - Needs
- OS - Overview
- OS - History
- OS - Components
- OS - Structure
- OS - Architecture
- OS - Services
- OS - Properties
- Process Management
- Operating System Processes
- Process Control Block
- Operations on Processes
- Inter Process Communication
- Context Switching
- Multi-threading
- Scheduling Algorithms
- Process Scheduling
- Preemptive and Non-Preemptive Scheduling
- Scheduling Algorithms Overview
- FCFS Scheduling Algorithm
- SJF Scheduling Algorithm
- Round Robin Scheduling Algorithm
- HRRN Scheduling Algorithm
- Priority Scheduling Algorithm
- Multilevel Queue Scheduling
- Lottery Scheduling Algorithm
- Turn Around Time & Waiting Time
- Burst Time in SJF Scheduling
- Process Synchronization
- Process Synchronization
- Critical Section Problem
- Critical Section Synchronization
- Mutual Exclusion Synchronization
- Semaphores
- Counting Semaphores
- Mutex
- Turn Variable
- Bounded Buffer Problem
- Reader Writer Locks
- Test and Set Lock
- Peterson's Solution
- Monitors
- Sleep and Wake
- Race Condition
- OS Deadlock
- Introduction to Deadlock
- Conditions for Deadlock
- Deadlock Handling
- Deadlock Prevention
- Deadlock Avoidance (Banker's Algorithm)
- Deadlock Detection and Recovery
- Deadlock Ignorance
- Memory Management
- Memory Management
- Contiguous Memory Allocation
- Non-Contiguous Memory Allocation
- First Fit Algorithm
- Next Fit Algorithm
- Best Fit Algorithm
- Worst Fit Algorithm
- Fragmentation
- Virtual Memory
- Segmentation
- Buddy System
- Slab Allocation
- Overlays
- Paging and Page Replacement
- Paging
- Demand Paging
- Page Table
- Page Replacement Algorithms
- Optimal Page Replacement Algorithm
- Belady's Anomaly
- Thrashing
- Storage and File Management
- File Systems
- File Attributes
- Structures of Directory
- Linked Index Allocation
- Indexed Allocation
- Disk Scheduling Algorithms
- FCFS Disk Scheduling
- SSTF Disk Scheduling
- SCAN Disk Scheduling
- LOOK Disk Scheduling
- I/O Systems
- I/O Hardware
- I/O Software
- OS Types
- OS - Types
- OS - Batch Processing
- OS - Multiprocessing
- OS - Hybrid
- OS - Monolithic
- OS - Zephyr
- OS - Nix
- OS - Linux
- OS - Blackberry
- OS - Garuda
- OS - Tails
- OS - Clustered
- OS - Haiku
- OS - AIX
- OS - Solus
- OS - Tizen
- OS - Bharat
- OS - Fire
- OS - Bliss
- OS - VxWorks
- OS - Embedded
- OS - Single User
- Miscellaneous Topics
- OS - Security
- OS Questions Answers
- OS - Questions Answers
- OS Useful Resources
- OS - Quick Guide
- OS - Useful Resources
- OS - Discussion
Operating System - Priority Scheduling Algorithm
Priority scheduling is a CPU scheduling strategy that decides which process in the ready queue should execute next based on the priorities assigned to the process. It is commonly used in systems where the execution of the processes are made in batches.
Priority Scheduling
In systems where Priority Scheduling strategies are implemented, each process is assigned a priority value. Some systems follow the scheme that lower the priority value, higher the priority; while other systems follow the scheme of higher the priority value, higher the priority. The process with the highest priority value is selected for execution.
There are two variants of Priority scheduling −
Non-preemptive Priority Scheduling
In the non-preemptive version, once a process is assigned to the CPU, it runs into completion. Here, the scheduler is invoked when a process completes its execution or when a new process(es) arrives in an empty ready queue. The scheduler chooses the process with highest priority for execution.
Preemptive Priority Scheduling
In the preemptive version, if a high priority process enters the system while a lower priority process is executing, process switch occurs by which the executing process is swapped out while the newly arrived higher priority process starts to execute. Thus the scheduler is invoked either when a new process arrives in the system or an existing process completes its execution.
Priority values can be also categorized into two forms −
- Static Priority: In this system, once a priority value is assigned to a process, it remains constant as long as the process remains in the system.
- Dynamic Priority: Here, the priority values changes according to the nature of the process or the waiting time of the process in the system.
Features of Priority Scheduling
- The process with the highest priority is assigned to the CPU.
- Non-preemptive priority scheduling that uses static priorities, are typically used in batch processes.
- Preemptive priority scheduling that uses dynamic priority is used in most operating systems.
- If two processes are of same highest priority, then the scheduler arbitrates between them on first come first serve basis.
- Since most systems have some high priority system processes, priority scheduling finds its wide implementation, often in conjunction with other scheduling algorithms.
We can understand the workings Priority scheduling algorithm through the aid of the following examples −
Examples of Non-Preemptive Priority Scheduling
Example 1
Suppose that we have a set of four processes that have arrived at the same time in the order P1, P2, P3 and P4. The burst time in milliseconds and their priorities of the processes is given by the following table −
| Process | CPU Burst Time in ms | Priority |
|---|---|---|
| P1 | 6 | 2 |
| P2 | 10 | 1 |
| P3 | 4 | 3 |
| P4 | 6 | 2 |
Considering that a lower priority value means higher priority, let us perform non-preemptive priority scheduling on it. We will draw the GANTT chart and find the average turnaround time and average waiting time.
GANTT Chart
Process P2 has the highest priority and so it executes first. Then we find that both P1 and P4 have equal priority value of 2. Since P1 arrived before, CPU is allocated to P1 and then to P4. Finally P3 executes. Thus the order of execution is P2, P1, P4, P3 and is given by the following
Let us compute the average turnaround time and average waiting time from the above chart.
Average Turnaround Time
= Sum of Turnaround Time of each Process/ Number of Processes
= ( TATP1 + TATP2 + TATP3 + TATP4) / 4
= ( 16 + 10 + 26 + 22) / 4 = 18.5 ms
Average Waiting Time
= Sum of Waiting Time of Each Process/ Number of processes
= ( WTP1 + WTP2 + WTP3 + WTP4) / 4
= ( 10 + 0 + 22 + 16) / 4 = 10.5 ms
Example 2
In this example, we consider a situation when the processes arrive at different times. Suppose we have set of four processes whose arrival times, CPU burst times and priorities are as follows −
| Process | Arrival Time | CPU Burst Time | Priority |
|---|---|---|---|
| P1 | 0 | 6 | 2 |
| P2 | 4 | 10 | 1 |
| P3 | 4 | 4 | 2 |
| P4 | 8 | 3 | 1 |
Let us draw the GANTT chart and find the average turnaround time and average waiting time using non-preemptive priority scheduling, considering lower priority value as higher priority.
GANTT Chart
At time 0ms, only P1 is there and so it is assigned to CPU. P1 completes execution at 6ms and at that time P2 and P3 have arrived. P2 has higher priority and hence assigned to CPU. P2 completes execution at time 10ms. By that time P4 has arrived having priority value 1 and so it is assigned to CPU. Once P4 completes execution, P3 is assigned to CPU. So the order of execution is P1, P2, P4, P3 as shown in the following GANTT chart −
Let us calculate the turnaround time of each process and hence the average.
Turnaround Time of a process = Completion Time Arrival Time
TATP1 = CTP1 - ATP1 = 6 - 0 = 6ms
TATP2 = CTP2 - ATP2 = 16 - 4 = 12ms
TATP3 = CTP3 - ATP3 = 23 - 4 = 19ms
TATP4 = CTP4 - ATP4 = 19 - 8 = 11ms
Average Turnaround Time
= Sum of Turnaround Time of each Process / Number of Processes
= ( 6 + 12+ 19 + 11) / 4 = 12 ms
The waiting time is given by the time that each process waits in the ready queue. For a non-preemptive scheduling algorithm, waiting time of each process can be simply calculated as −
Waiting Time of any process = Time of admission to CPU Arrival Time
WTP1 = 0 - 0 = 0ms
WTP2 = 6 - 4 = 2ms
WTP3 = 19 - 4 = 15ms
WTP4 = 16 - 8 = 8ms
Average Waiting Time
= Sum of Waiting Time of Each Process / Number of processes
= (WTP1 + WTP2 + WTP3 + WTP4) / 4
= ( 0 + 2 +15 + 8) / 4 = 6.25 ms
Example of Preemptive Priority Scheduling
In preemptive priority scheduling, if a process arrives that has higher priority than the executing process, then the higher priority process pre-empts the lower priority process. Let us consider the following set of processes whose arrival times, burst times and priorities are give in the following table −
| Process | Arrival Time | CPU Burst Time | Priority |
|---|---|---|---|
| P1 | 0 | 8 | 3 |
| P2 | 4 | 10 | 2 |
| P3 | 4 | 3 | 3 |
| P4 | 10 | 4 | 1 |
GANTT Chart
At time 0ms, P1 is the only process and so it starts executing. At time 4ms, P2 and P3 arrive. Since P2 has higher priority than P1, P2 preempts P1. At 10ms, P4 arrives which has higher priority than P2 and so pre-empts P2. P4 completes execution at 14ms and leaves. The processes in the system are P1, P2 and P3, among which P2 has highest priority and so is assigned to CPU. At 18ms, P2 completes execution and so P1 and P3 are the processes in the system. Since both processes are of same priority, the scheduler selects P1 by FCFS method. When P1 completes execution, finally P3 executes. The following GANTT chart shows the order of execution −
From the GANTT chart, we compute the average turnaround time and the average waiting time.
Average Turnaround Time
= Sum of Turnaround Time of each Process/ Number of Processes
= (TATP1 + TATP2 + TATP3 + TATP4) / 4
= ((22-0) + (18-4) + (25-4) + (14-10)) / 4 = 15.25 ms
Average Waiting Time
= Sum of Waiting Time of Each Process/ Number of processes
= (WTP1 + WTP2 + WTP3 + WTP4) / 4
= (14 + 4 + 18 +0) / 4 = 7 ms
Advantages of Priority Scheduling
- Implementation is simple since scheduler doesnt require doing any prior calculations.
- Once CPU defines the relative relevance (priorities) of the processes, the order of execution is easily predictable.
- Higher priority processes are almost served immediately.
- Priority scheduling is particularly helpful in systems that have variety of processes each with their own needs.
Disadvantages of Priority Scheduling
- In static priority systems, lower priority processes may need to wait indefinitely since the system is busy executing higher priority processes. This results in stagnation.
- Dynamic priority solves the stagnation problem. However, the added logic of dynamically updating priority values according to the system requires additional CPU cycles and thus increases load on the system.
- In non-preemptive priority scheduling, often a large process keeps shorter processes waiting for long time.
- In preemptive priority scheduling, a low priority process may be repeatedly pre-empted by intermittent streams of high priority processes requiring frequent context switches.
Conclusion
Priority scheduling algorithm paves way for more complex scheduling methods like multilevel queue scheduling. Assigning priorities to the processes helps the CPU to complete its important work first and leave the rest of the time for background processes. Priorities can be assigned to the processes as a function of their burst time, type of process, waiting time etc. and can thus incorporate the advantages of other basic scheduling algorithms.