Preemptive Priority CPU Scheduling Algortithm
Prerequisite – https://www.geeksforgeeks.org/cpu-scheduling-in-operating-systems/
Introduction:
This is a pre-emptive method of scheduling processes that works based on the priority of a process. In this algorithm, the scheduler schedules the tasks to work as per the priority. A higher priority process should be executed first. When we are having 2 processes with equal priorities then we choose to select the process based on FCFS (First Come First Serve). When it reaches to final process then we will execute the remaining processes based on priority.
Note: Lesser the Number, More Priority
For example: 1 is high priority, 10 – low priority
Procedure:
- Step-1: Select the first process whose arrival time will be 0, we need to select that process because that process is only be executing at time t=0.
- Step-2: Check the priority of the next available process.
Here we need to check for 3 conditions.
(i) if priority(current_process) > priority(prior_process) then execute the current process.
(ii) if priority(current_process) > priority(prior_process) then execute the prior process.
(iii) if priority(current_process) > priority(prior_process) then execute the process which arrives first i.e., arrival time
should be first.
- Step-3: Repeat Step-2 until it reaches the final process.
- Step-4: When it reaches the final process, choose the process which is having the highest priority & execute it. Repeat the same step until all processes completes their execution.
Note: A Process is completed when its burst time becomes 0.
Example-1: Consider the following table of arrival time, Priority and burst time for five processes P1, P2, P3, P4 and P5. Process Arrival Time Priority Burst Time P1 0 ms 3 3 ms P2 1 ms 2 4 ms P3 2 ms 4 6 ms P4 3 ms 6 4 ms P5 5 ms 10 2 ms
Working: (for input-1):
Step-1: At Arrival time t = 0, process P1 is executed for 1ms as its arrival time is from 0ms to 1ms, as it is the first process in the ready queue. Remaining Burst time (B.T) for P1 = 3-1 = 2 ms.
Step-2: At time t = 1, process P2 is having higher priority than P1, so we need to execute Process P2, for 1ms.
Remaining B.T for P2 = 4-1 = 3 ms.
Step-3: At time t = 2, Process P2 is having higher priority than P3, So again P2 will be executed for 1 ms.
Remaining B.T for P2 = 3-1 = 2 ms.
Step-4: At time t = 3, Process P2 is having higher priority than P4, So P2 now will execute for 2 ms as the arrival time of P4 is from 3 ms to 5 ms. Remaining B.T for P2 = 2-2 = 0 ms.
-> Here Process P2 is completed with its execution as the burst time for P2 is 0.
-> Here the Arrival time for Processes is now 5 so it has reached to all the Processes.
Step-5: Now we need to schedule the process with the highest priority, Here we are having P1 as the highest priority, So P1 is executed completely and the remaining B.T of P1 = 2-2 = 0 ms.
Step-6: Similarly repeat Step-5 for all the remaining processes based on priority until all processes complete their execution.
Gantt chart:
Now we need to calculate the completion time (C.T) & First Arrival Time (F.A.T) of each process through Gantt chart.
C.T -> P1-7 ms, P2-5 ms, P3-13 ms, P4-17 ms, P5-19 ms.
F.A.T -> P1-0 ms, P2-1 ms, P3-7 ms, P4-13 ms, P5-17 ms.
Turn Around Time (T.A.T) = (Completion Time) – (Arrival Time)
Waiting Time (W.T) = (Turn Around Time) – (Burst Time)
Response Time (R.T) = (First Arrival Time) – (Arrival Time)
After calculating the above fields, the final table looks like
Here, H – Higher Priority L – Least Priority
Output :
Total Turn Around Time = 7 + 4 + 11 + 14 + 14 = 50 ms
Average Turn Around Time = (Total Turn Around Time)/(no. of processes) = 50/5 = 10.00 msTotal Waiting Time = 4 + 0 + 15 + 10 + 12 = 41 ms
Average Waiting Time = (Total Waiting Time)/(no. of processes) = 41/5 = 8.20 msTotal Response Time = 0 + 0 + 5 + 10 + 12 = 27 ms
Average Waiting Time = (Total Waiting Time)/(no. of processes) = 27/5 = 5.40 ms
Example 2:
Consider the following table of arrival time, Priority and burst time for seven processes P1, P2, P3, P4, P5, P6 and P7Process Arrival Time Priority Burst Time P1 0 ms 3 8 ms P2 1 ms 4 2 ms P3 3 ms 4 4 ms P4 4 ms 5 1 ms P5 5 ms 2 6 ms P6 6 ms 6 5 ms P7 10 ms 1 1 ms
Working: (for input-2)
Step-1: At time t = 0, Process P1 is available in the ready queue, executing P1 for 1 ms.
Remaining B.T for P1 = 8-1 = 7 ms.
Step-2: At time t = 1, the priority of P1 is greater than P2, so we execute P1 for 2 ms, as the arrival time of P2 is from 1 ms to 3 ms. Remaining B.T for P1 = 7-2 = 5 ms.
Step-3: At time t = 3, the priority of P1 is greater than P3, so we execute P1 for 1 ms.
Remaining B.T for P1 = 5-1 = 4 ms.
Step-4: At time t = 4, the priority of P1 is greater than P4, so we execute P1 for 1 ms.
Remaining B.T for P1 = 4-1 = 3 ms.
Step-5: At time t = 5, the priority of P5 is greater than P1, so we execute P5 for 1 ms.
Remaining B.T for P5 = 6-1 = 5 ms.
Step-6: At time t = 6, the priority of P5 is greater than P6, so we execute P5 for 4 ms.
Remaining B.T for P5 = 5-4 = 1 ms.
Step-7: At time t = 7, the priority of P7 is greater than P5, so we execute P7 for 1 ms.
Remaining B.T for P7 = 1-1 = 0 ms.
-> Here Process P7 completes its execution.
Step-8: Now we take the process which is having the highest priority. Here we find P5 is having the highest priority & execute P5 completely and the remaining B.T of P5 = 1-1 = 0 ms.
Step-9: Similarly repeat Step-8 for all the remaining processes based on priority until all processes completes their execution.
Gantt chart:
Now we need to calculate the completion time (C.T) & First Arrival Time (F.A.T) of each process through Gantt chart.
C.T -> P1-15 ms, P2-17 ms, P3-21 ms, P4-22 ms, P5-12 ms, P6-27 ms, P7- 11 ms.
F.A.T -> P1-0 ms, P2-15 ms, P3-17 ms, P4-21 ms, P5-5 ms, P6-22 ms, P7-10 ms.
Turn Around Time (T.A.T) = (Completion Time) – (Arrival Time)
Waiting Time (W.T) = (Turn Around Time) – (Burst Time)
Response Time (R.T) = (First Arrival Time) – (Arrival Time)
After calculating the above fields, the final table looks like
Here, H – Higher Priority, L – Least Priority
Output:
Total Turn Around Time = 15 + 16 + 18 + 18 + 7 + 21 + 1 = 96 ms
Average Turn Around Time = (Total Turn Around Time)/(no. of processes) = 96/7 = 13.71 msTotal Waiting Time = 7 + 14 + 14 + 17 + 1 + 16 + 0 = 69 ms
Average Waiting Time = (Total Waiting Time)/(no. of processes) = 69/7 = 9.85 msTotal Response Time = 0 + 14 + 14 + 17 + 0 + 16 + 0 = 61 ms
Average Waiting Time = (Total Waiting Time)/(no. of processes) = 61/7 = 8.71 ms
Drawbacks of Pre-emptive Priority Scheduling Algorithm:
Starvation Problem: This is the problem in which a process has to wait for a longer amount of time to get scheduled into the CPU. This condition is called the starvation problem.
Example: In Example 2, we can see that process P1 is having Priority 3 and we have pre-empted the process P1 and allocated the CPU to P5. Here we are only having 7 processes.
Now, if suppose we have many processes whose priority is higher than P1, then P1 needs to wait for a longer time for the other process to be pre-empted and scheduled by the CPU. This condition is called as starvation problem.
Solution: The solution for this Starvation problem is Ageing.
Aging: This can be done by decrementing the priority number by a certain number of a particular process which is waiting for a longer period of time after a certain interval.
Example: After every 3 units of time, all the processes which are in waiting state, the priority of those processes will be decreased by 2, So, if there is a process P1 which is having priority 5, after waiting for 3 units of time its priority will be decreased from 5 to 3 so that if there is any process P2 which is having priority as 4 then that process P2 will wait and P1 will be scheduled and executed.
The implementation of this scheduling process is provided in this article.

