Skip to content
Related Articles

Related Articles

Preemptive Priority CPU Scheduling Algorithm

View Discussion
Improve Article
Save Article
Like Article
  • Last Updated : 19 Apr, 2022

Preemptive Priority CPU Scheduling Algorithm is a pre-emptive method of CPU scheduling algorithm that works based on the priority of a process. In this algorithm, the scheduler schedules the tasks to work as per the priority, which means that a higher priority process should be executed first. In case of any conflict, i.e., when there are more than one processes with equal priorities, then pre-emptive priority CPU scheduling algorithm works on the basis of FCFS (First Come First Serve) algorithm.

How Preemptive Priority CPU Scheduling Algorithm decides Priority of a Process?

Preemptive Priority CPU Scheduling Algorithm uses a rank based system to define a rank for each process, where lower rank processes have higher priority and higher rank processes have lower priority. For instance, if there are 10 processes to be executed using this Preemptive Algorithm, then process with rank 1 will have highest priority, process with rank 2 will have comparatively lesser priority, and process with rank 10 will have least priority.  

How Preemptive Priority CPU Scheduling Algorithm works?

  • 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.
  1. if priority(current_process) > priority(prior_process) :- then execute the current process.
  2. if priority(current_process) > priority(prior_process) :- then execute the prior process.
  3. 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.

Program for Preemptive Priority CPU Scheduling

Preemptive Priority algorithm can be implemented using Min Heap data structure. For detailed implementation of Preemptive priority scheduling algorithm, please refer: Program for Preemptive Priority CPU Scheduling
 

Examples to show working of Preemptive Priority CPU Scheduling Algorithm

Example-1: Consider the following table of arrival time, Priority and burst time for five processes P1, P2, P3, P4 and P5

ProcessArrival Time PriorityBurst Time
 P1   0 ms33 ms
 P2 1 ms24 ms
 P3 2 ms4   6 ms
 P4 3 ms6   4 ms
 P5 5 ms10   2 ms

The Preemptive Priority CPU Scheduling Algorithm will work on the basis of steps as mentioned below:

  • At time t = 0, 
    • Process P1 is the only process available in the ready queue, as its arrival time is 0ms.
    • Hence Process P1 is executed first for 1ms, from 0ms to 1ms, irrespective of its priority. 
    • Remaining Burst time (B.T) for P1 = 3-1 = 2 ms.
Time InstanceProcessArrival TimePriorityExecution TimeInitial Burst TimeFinal Burst Time
0 ms – 1 ms P1   0 ms31ms3 ms2 ms
  • At time t = 1 ms, 
    • There are 2 processes available in the ready queue: P1 and P2.
    • Since the priority of process P2 is higher than the priority of process P1, therefore Process P2 will get executed first.
    • Hence Process P2 is executed for 1ms, from 1ms to 2ms. 
    • Remaining Burst time (B.T) for P2 = 4-1 = 3 ms.
Time InstanceProcessArrival TimePriorityExecution TimeInitial Burst TimeFinal Burst Time
1 ms – 2 ms P1   0 ms302 ms2 ms
 P2 1 ms214 ms3 ms
  • At time t = 2 ms, 
    • There are 3 processes available in the ready queue: P1, P2 and P3.
    • Since the priority of process P2 is higher than the priority of process P1 and P3, therefore Process P2 will get executed first.
    • Hence Process P2 is executed for 1ms, from 2ms to 3ms. 
    • Remaining Burst time (B.T) for P2 = 3-1 = 2 ms.
Time InstanceProcessArrival TimePriorityExecution TimeInitial Burst TimeFinal Burst Time
2 ms – 3 ms P1   0 ms302 ms2 ms
 P2 1 ms213 ms2 ms
 P3 2 ms4   06 ms6 ms
  • At time t = 3 ms, 
    • There are 4 processes available in the ready queue: P1, P2, P3 and P4.
    • Since the priority of process P2 is highest among the priority of process P1, P2, P3 and P4, therefore Process P2 will get executed first.
    • Hence Process P2 is executed for 1ms, from 3ms to 4ms. 
    • Remaining Burst time (B.T) for P2 = 1-1 = 0 ms.
Time InstanceProcessArrival TimePriorityExecution TimeInitial Burst TimeFinal Burst Time
3 ms – 4 ms P1   0 ms302 ms2 ms
 P2 1 ms212 ms1 ms
 P3 2 ms4   06 ms6 ms
 P4 3 ms6   04 ms4 ms
  • At time t = 4 ms, 
    • There are 5 processes available in the ready queue: P1, P2, P3, P4 and P5.
    • Since the priority of process P2 is highest among the priority of process P1, P2, P3, P4 and P5, therefore Process P2 will get executed first.
    • Hence Process P2 is executed for 1ms, from 4ms to 5ms. 
    • Remaining Burst time (B.T) for P2 = 1-1 = 0 ms.
    • Since Process P2’s burst time has become 0, therefore it is complete and will be removed from the process queue.
Time InstanceProcessArrival TimePriorityExecution TimeInitial Burst TimeFinal Burst Time
4 ms – 5 ms P1   0 ms302 ms2 ms
 P2 1 ms211 ms0 ms
 P3 2 ms4   06 ms6 ms
 P4 3 ms6   04 ms4 ms
 P5 5 ms10   02 ms2 ms
  • At time t = 5 ms, 
    • There are 4 processes available in the ready queue: P1, P3, P4 and P5.
    • Since the priority of process P1 is highest among the priority of process P1, P3, P4 and P5, therefore Process P1 will get executed first.
    • Hence Process P1 is executed for 2ms, from 5ms to 7ms. 
    • Remaining Burst time (B.T) for P1 = 2-2 = 0 ms.
    • Since Process P1’s burst time has become 0, therefore it is complete and will be removed from the process queue.
Time InstanceProcessArrival TimePriorityExecution TimeInitial Burst TimeFinal Burst Time
5 ms – 7 ms P1   0 ms322 ms0 ms
 P3 2 ms4   06 ms6 ms
 P4 3 ms6   04 ms4 ms
 P5 5 ms10   02 ms2 ms
  • At time t = 7 ms, 
    • There are 3 processes available in the ready queue: P3, P4 and P5.
    • Since the priority of process P3 is highest among the priority of process P3, P4 and P5, therefore Process P3 will get executed first.
    • Hence Process P3 is executed for 6ms, from 7ms to 13ms. 
    • Remaining Burst time (B.T) for P3 = 6-6 = 0 ms.
    • Since Process P3’s burst time has become 0, therefore it is complete and will be removed from the process queue.
Time InstanceProcessArrival TimePriorityExecution TimeInitial Burst TimeFinal Burst Time
7 ms – 13 ms P3 2 ms4   66 ms0 ms
 P4 3 ms6   04 ms4 ms
 P5 5 ms10   02 ms2 ms
  • At time t = 13 ms, 
    • There are 2 processes available in the ready queue: P4 and P5.
    • Since the priority of process P4 is highest among the priority of process P4 and P5, therefore Process P4 will get executed first.
    • Hence Process P4 is executed for 4ms, from 13ms to 17ms. 
    • Remaining Burst time (B.T) for P4 = 4-4 = 0 ms.
    • Since Process P4’s burst time has become 0, therefore it is complete and will be removed from the process queue.
Time InstanceProcessArrival TimePriorityExecution TimeInitial Burst TimeFinal Burst Time
13 ms – 17 ms P4 3 ms6   44 ms0 ms
 P5 5 ms10   02 ms2 ms
  • At time t = 17 ms, 
    • There is only 1 process available in the ready queue: P5.
    • Hence Process P5 is executed for 2ms, from 17ms to 19ms. 
    • Remaining Burst time (B.T) for P5 = 2-2 = 0 ms.
    • Since Process P5’s burst time has become 0, therefore it is complete and will be removed from the process queue.
Time InstanceProcessArrival TimePriorityExecution TimeInitial Burst TimeFinal Burst Time
17 ms – 19 ms P5 5 ms10   22 ms0 ms
  • At time t = 19 ms, 
    • There is no more process available in the ready queue. Hence the processing will now stop.
    • The overall execution of the processes will be as shown below:
Time InstanceProcessArrival TimePriorityExecution TimeInitial Burst TimeFinal Burst Time
0 ms – 1 ms P1   0 ms313 ms2 ms
1 ms – 2 ms P1   0 ms302 ms2 ms
 P2 1 ms214 ms3 ms
2 ms – 3 ms P1   0 ms302 ms2 ms
 P2 1 ms213 ms2 ms
 P3 2 ms4   06 ms6 ms
3 ms – 4 ms P1   0 ms302 ms2 ms
 P2 1 ms212 ms1 ms
 P3 2 ms4   06 ms6 ms
 P4 3 ms6   04 ms4 ms
4 ms – 5 ms P1   0 ms302 ms2 ms
 P2 1 ms211 ms0 ms
 P3 2 ms4   06 ms6 ms
 P4 3 ms6   04 ms4 ms
 P5 5 ms10   02 ms2 ms
5 ms – 7 ms P1   0 ms322 ms0 ms
 P3 2 ms4   06 ms6 ms
 P4 3 ms6   04 ms4 ms
 P5 5 ms10   02 ms2 ms
7 ms – 13 ms P3 2 ms4   66 ms0 ms
 P4 3 ms6   04 ms4 ms
 P5 5 ms10   02 ms2 ms
13 ms – 17 ms P4 3 ms6   44 ms0 ms
 P5 5 ms10   02 ms2 ms
17 ms – 19 ms P5 5 ms10   22 ms0 ms

Gantt chart for above execution:

 

Now we need to calculate the completion time (C.T) & First Arrival Time (F.A.T) of each process through Gantt chart, as per below relations:

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 – Highest Priority, and L – Least Priority

Some other important relations for this example include:

  • 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 ms
  • Total Waiting Time = 4 + 0 + 15 + 10 + 12 = 41 ms
  • Average Waiting Time = (Total Waiting Time)/(no. of processes) = 41/5 = 8.20 ms
  • Total Response Time = 0 + 0 + 5 + 10 + 12 = 27 ms
  • Average Response Time = (Total Response 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 P7

ProcessArrival TimePriority  Burst Time
P1      0 ms3 8 ms
P2   1 ms4 2 ms
P3  3 ms4       4 ms
P4  4 ms5 1 ms
P5  5 ms2 6 ms
P6  6 ms6     5 ms
P7       10 ms1 1 ms
  • 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.
       
  • At time t = 1
    • The priority of P1 is greater than P2, so we execute P1 for 2 ms, from 1 ms to 3 ms.
    • Remaining B.T for P1 = 7-2 = 5 ms.
  • 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.
  • 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.
  • 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.
  • 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.
  • At time t = 10
    • 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.
  • At time t = 11,
    • Now we take the process which is having the highest priority. 
    • Here we find P5 is having the highest priority & execute P5 completely 
    • Remaining B.T of P5 = 1-1 = 0 ms.
    • Here Process P5 completes its execution.
  • At time t = 12,
    • Now we take the process which is having the highest priority. 
    • Here we find P1 is having the highest priority & execute P1 completely 
    • Remaining B.T of P1 = 3-3 = 0 ms.
    • Here Process P1 completes its execution.
  • At time t = 15,
    • Now we take the process which is having the highest priority. 
    • Here we find P2 is having the highest priority & execute P2 completely 
    • Remaining B.T of P2 = 2-2 = 0 ms.
    • Here Process P2 completes its execution.
  • At time t = 17,
    • Now we take the process which is having the highest priority. 
    • Here we find P3 is having the highest priority & execute P3 completely 
    • Remaining B.T of P3 = 4-4 = 0 ms.
    • Here Process P3 completes its execution.
  • At time t = 21,
    • Now we take the process which is having the highest priority. 
    • Here we find P4 is having the highest priority & execute P4 completely 
    • Remaining B.T of P4 = 1-1 = 0 ms.
    • Here Process P4 completes its execution.
  • At time t = 22,
    • Now we take the process which is having the highest priority. 
    • Here we find P6 is having the highest priority & execute P6 completely 
    • Remaining B.T of P6 = 5-5 = 0 ms.
    • Here Process P6 completes its execution.

Gantt chart:

 


 

Here, H – Higher Priority, L – Least Priority

Drawbacks of Preemptive Priority Scheduling Algorithm:

One of the most common drawback of Preemptive priority CPU scheduling algorithm is the 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. 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.

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.


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!