Open In App
Related Articles

Preemptive Priority CPU Scheduling Algorithm

Improve Article
Improve
Save Article
Save
Like Article
Like

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 is more than one process with equal priorities, then the pre-emptive priority CPU scheduling algorithm works on the basis of FCFS (First Come First Serve) algorithm.

How does Preemptive Priority CPU Scheduling Algorithm decide the 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 the highest priority, the process with rank 2 will have comparatively lesser priority, and process with rank 10 will have least priority.  

How does Preemptive Priority CPU Scheduling Algorithm work?

  • Step-1: Select the first process whose arrival time will be 0, we need to select that process because that process is only executing at time t=0.
  • Step-2: Check the priority of the next available process. Here we need to check for 3 conditions.
    • if priority(current_process) > priority(prior_process) :- then execute the current process.
    • if priority(current_process) < priority(prior_process) :- then execute the prior process.
    • 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 complete their execution.

Program for Preemptive Priority CPU Scheduling

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

Examples to show the 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

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

The Preemptive Priority CPU Scheduling Algorithm will work on the basis of the steps 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 Instance Process Arrival Time Priority Execution Time Initial Burst Time Final Burst Time
0 ms – 1 ms  P1    0 ms 3 1ms 3 ms 2 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 Instance Process Arrival Time Priority Execution Time Initial Burst Time Final Burst Time
1 ms – 2 ms  P1    0 ms 3 0 2 ms 2 ms
 P2  1 ms 2 1 4 ms 3 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 Instance Process Arrival Time Priority Execution Time Initial Burst Time Final Burst Time
2 ms – 3 ms  P1    0 ms 3 0 2 ms 2 ms
 P2  1 ms 2 1 3 ms 2 ms
 P3  2 ms 4    0 6 ms 6 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 processes 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 Instance Process Arrival Time Priority Execution Time Initial Burst Time Final Burst Time
3 ms – 4 ms  P1    0 ms 3 0 2 ms 2 ms
 P2  1 ms 2 1 2 ms 1 ms
 P3  2 ms 4    0 6 ms 6 ms
 P4  3 ms 6    0 4 ms 4 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 processes 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 Instance Process Arrival Time Priority Execution Time Initial Burst Time Final Burst Time
4 ms – 5 ms  P1    0 ms 3 0 2 ms 2 ms
 P2  1 ms 2 1 1 ms 0 ms
 P3  2 ms 4    0 6 ms 6 ms
 P4  3 ms 6    0 4 ms 4 ms
 P5  5 ms 10    0 2 ms 2 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 the highest among the priority of processes 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 Instance Process Arrival Time Priority Execution Time Initial Burst Time Final Burst Time
5 ms – 7 ms  P1    0 ms 3 2 2 ms 0 ms
 P3  2 ms 4    0 6 ms 6 ms
 P4  3 ms 6    0 4 ms 4 ms
 P5  5 ms 10    0 2 ms 2 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 the highest among the priority of processes 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 Instance Process Arrival Time Priority Execution Time Initial Burst Time Final Burst Time
7 ms – 13 ms  P3  2 ms 4    6 6 ms 0 ms
 P4  3 ms 6    0 4 ms 4 ms
 P5  5 ms 10    0 2 ms 2 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 Instance Process Arrival Time Priority Execution Time Initial Burst Time Final Burst Time
13 ms – 17 ms  P4  3 ms 6    4 4 ms 0 ms
 P5  5 ms 10    0 2 ms 2 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 Instance Process Arrival Time Priority Execution Time Initial Burst Time Final Burst Time
17 ms – 19 ms  P5  5 ms 10    2 2 ms 0 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 Instance Process Arrival Time Priority Execution Time Initial Burst Time Final Burst Time
0 ms – 1 ms  P1    0 ms 3 1 3 ms 2 ms
1 ms – 2 ms  P1    0 ms 3 0 2 ms 2 ms
 P2  1 ms 2 1 4 ms 3 ms
2 ms – 3 ms  P1    0 ms 3 0 2 ms 2 ms
 P2  1 ms 2 1 3 ms 2 ms
 P3  2 ms 4    0 6 ms 6 ms
3 ms – 4 ms  P1    0 ms 3 0 2 ms 2 ms
 P2  1 ms 2 1 2 ms 1 ms
 P3  2 ms 4    0 6 ms 6 ms
 P4  3 ms 6    0 4 ms 4 ms
4 ms – 5 ms  P1    0 ms 3 0 2 ms 2 ms
 P2  1 ms 2 1 1 ms 0 ms
 P3  2 ms 4    0 6 ms 6 ms
 P4  3 ms 6    0 4 ms 4 ms
 P5  5 ms 10    0 2 ms 2 ms
5 ms – 7 ms  P1    0 ms 3 2 2 ms 0 ms
 P3  2 ms 4    0 6 ms 6 ms
 P4  3 ms 6    0 4 ms 4 ms
 P5  5 ms 10    0 2 ms 2 ms
7 ms – 13 ms  P3  2 ms 4    6 6 ms 0 ms
 P4  3 ms 6    0 4 ms 4 ms
 P5  5 ms 10    0 2 ms 2 ms
13 ms – 17 ms  P4  3 ms 6    4 4 ms 0 ms
 P5  5 ms 10    0 2 ms 2 ms
17 ms – 19 ms  P5  5 ms 10    2 2 ms 0 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 the 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

The final table

The final table

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 + 5 + 10 + 12 = 31 ms
  • Average Waiting Time = (Total Waiting Time)/(no. of processes) = 31/5 = 6.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

Process 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
  • 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

C




#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <stdbool.h>
 
void srtn(int pid[], int at[], int bt[], int wt[], int tat[], int size)
{
    printf("\n-------------------------------------------------------------------------------");
    printf("\nInside SRTN function...");
    printf("\n-------------------------------------------------------------------------------");
    printf("\nPrinting data...");
    printf("\nProcess ID\tArrival Time\tBurst Time\tWaiting Time\tTurnaround Time");
    for(int i = 0; i < size; i++){
        printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d", pid[i], at[i], bt[i], wt[i], tat[i]);
    }
    printf("\n-------------------------------------------------------------------------------");
    printf("\nForming table...");
    int remaining_time[size];
    bool completed[size];
    for (int i = 0; i < size; i++) {
        remaining_time[i] = bt[i];
        completed[i] = false;
    }
    int current_time = 0;
    int completed_processes = 0;
    printf("\nJob\tArrival Time\tBurst Time\tFinish Time\tTurnaround Time\tWaiting Time\n");
    while (completed_processes < size) {
        int shortest_job = -1;
        int shortest_time = 9999; // A large number as initial value
        for (int i = 0; i < size; i++) {
            if (at[i] <= current_time && !completed[i] && remaining_time[i] < shortest_time) {
                shortest_job = i;
                shortest_time = remaining_time[i];
            }
        }
        if (shortest_job == -1) {
            current_time++;
        } else {
            remaining_time[shortest_job]--;
            if (remaining_time[shortest_job] == 0) {
                completed_processes++;
                int finish_time = current_time + 1;
                tat[shortest_job] = finish_time - at[shortest_job];
                wt[shortest_job] = tat[shortest_job] - bt[shortest_job];
                completed[shortest_job] = true;
 
                printf("%c\t%d\t\t%d\t\t%d\t\t%d\t\t%d\n", 'A' + pid[shortest_job], at[shortest_job], bt[shortest_job], finish_time, tat[shortest_job], wt[shortest_job]);
            }
            current_time++;
        }
    }
    printf("\n-------------------------------------------------------------------------------");
    printf("\n-------------------------------------------------------------------------------");
    printf("\n-------------------------------------------------------------------------------");  
}
 
void rr(int pid[], int at[], int bt[], int size, int quantum)
{
    //pid is analogous to Array1; bt is analogous to Array2; wt is analogous to Array3
    printf("\n-------------------------------------------------------------------------------");
    printf("\nInside RR function...");
    printf("\n-------------------------------------------------------------------------------");
    printf("\nPrinting data...");
    printf("\nProcess ID\tArrival Time\tBurst Time");
    for(int i = 0; i < size; i++){
        printf("\n%d\t\t%d\t\t%d", pid[i], at[i], bt[i]);
    }
    printf("\n-------------------------------------------------------------------------------\n");
    int remaining_time[size];
    int finish_time[size];
    int tat[size];
    int wt[size];
    int time = 0;
    int total_tat = 0;
    int total_wt = 0;
    for (int i = 0; i < size; i++) {
        remaining_time[i] = bt[i];
        finish_time[i] = 0;
        tat[i] = 0;
        wt[i] = 0;
    }
    int done = 0;
    while (!done) {
        done = 1;
        for (int i = 0; i < size; i++) {
            if (remaining_time[i] > 0) {
                done = 0;
 
                if (remaining_time[i] > quantum) {
                    time += quantum;
                    remaining_time[i] -= quantum;
                } else {
                    time += remaining_time[i];
                    finish_time[i] = time;
                    tat[i] = finish_time[i] - at[i];
                    wt[i] = tat[i] - bt[i];
                    total_tat += tat[i];
                    total_wt += wt[i];
                    remaining_time[i] = 0;
                }
            }
        }
    }
    // Print the table
    printf("Job\tArrival Time\tBurst Time\tFinish Time\tTurnaround Time\tWaiting Time\n");
    for (int i = 0; i < size; i++) {
        printf("%d\t\t%d\t\t%d\t\t%d\t\t%d\t\t%d\n", pid[i], at[i], bt[i], finish_time[i], tat[i], wt[i]);
    }
    // Calculate and print average turnaround time and waiting time
    float avg_tat = (float)total_tat / size;
    float avg_wt = (float)total_wt / size;
    printf("Average\t%.1f\t%.1f\n", avg_tat, avg_wt);
    printf("\n-------------------------------------------------------------------------------");
    printf("\n-------------------------------------------------------------------------------");
    printf("\n-------------------------------------------------------------------------------");
}
 
void priority(int pid[], int at[], int bt[], int wt[], int tat[], int p[], int size)
{
    printf("\n-------------------------------------------------------------------------------");
    printf("\nInside Priority function...");
    printf("\n-------------------------------------------------------------------------------");
    printf("\nPrinting data...");
    printf("\nProcess ID\tArrival Time\tBurst Time\tWaiting Time\tTurnaround Time");
    for(int i = 0; i < size; i++){
        printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d", pid[i], at[i], bt[i], wt[i], tat[i]);
    }
    printf("\n-------------------------------------------------------------------------------\n");\
    int remaining_time[size];
    int current_time = 0;
    int completed = 0;
    // Initialize the remaining time for each process
    for (int i = 0; i < size; i++) {
        remaining_time[i] = bt[i];
    }
    printf("Process\tArrival Time\tBurst Time\tPriority\tFinish Time\tTurnaround Time\tWaiting Time\n");
    while (completed < size) {
        int selected = -1;
        int highest_priority = 9999; // A large initial value
        for (int i = 0; i < size; i++) {
            if (at[i] <= current_time && remaining_time[i] > 0 && p[i] < highest_priority) {
                selected = i;
                highest_priority = p[i];
            }
        }
        if (selected == -1) {
            current_time++;
        } else {
            remaining_time[selected]--;
            current_time++;
            if (remaining_time[selected] == 0) {
                completed++;
                int finish_time = current_time;
                int turnaround_time = finish_time - at[selected];
                int waiting_time = turnaround_time - bt[selected];
                printf("P%d\t%d\t\t%d\t\t%d\t\t%d\t\t%d\t\t%d\n", pid[selected], at[selected], bt[selected], p[selected], finish_time, turnaround_time, waiting_time);
                wt[selected] = waiting_time;
                tat[selected] = turnaround_time;
            }
        }
    }
    printf("\n-------------------------------------------------------------------------------");
    printf("\n-------------------------------------------------------------------------------");
    printf("\n-------------------------------------------------------------------------------");
}
 
int main()
{
    printf("\n-------------------------------------------------------------------------------");
    printf("\n--------------------Preemptive Scheduling Algorithm Program--------------------");
    printf("\n-------------------------------------------------------------------------------");
    int ch_run;
    while(0 < 1){
        fflush(stdin);
        printf("\nEnter 1 for Shortest Remaining Time Next\nEnter 2 for Round Robin\nEnter 3 for Priority\nEnter choice: ");
        int ch_algo, n;
        scanf("%d", &ch_algo);
        printf("\n-----------------------------------------------------------------------------------");
        printf("\nEnter 1 for default values and 2 for custom values\nEnter choice: ");
        int ch_val;
        scanf("%d", &ch_val);
        if(ch_val == 1){
            n = 5;
            int pid[n], wt[n], tat[n];
            printf("\n-----------------------------------------------------------------------------------");
            printf("\nEntered data: Choice = %d, Number of processes = %d", ch_algo, n);
            printf("\n-----------------------------------------------------------------------------------");
            //Initializing Process ID Array
            for(int i = 0; i < n; i++){
                pid[i] = i;
                printf("\nProcess %d has got Process ID %d.", i+1, pid[i]);
            }
            printf("\nProcess ID Array initialized successfully.");
            printf("\n-----------------------------------------------------------------------------------");
            int at[] = {2, 0, 1, 4, 3};
            for(int i = 0; i < n; i++)
                printf("\nProcess %d has got Arrival Time %d.", i+1, at[i]);
            printf("\n-----------------------------------------------------------------------------------");
            int bt[] = {6, 4, 8, 3, 2};
            for(int i = 0; i < n; i++)
                printf("\nProcess %d has got Burst Time %d.", i+1, bt[i]);
            int quantum;
            switch(ch_algo)
            {
                case 1:
                    printf("\nYou selected SRTN!");
                    srtn(pid, at, bt, wt, tat, n);
                    break;
                case 2:
                    printf("\nYou selected RR!");
                    printf("\nEnter time quantum: ");
                    scanf("%d", &quantum);
                    rr(pid, at, bt, n, quantum);
                    break;
                case 3:
                    printf("\nYou selected Priority!");
                    printf("\n-----------------------------------------------------------------------------------");
                    int p[] = {0, 2, 4, 1, 3};
                    for(int i = 0; i < n; i++)
                        printf("\nProcess %d has got Priority %d.", i+1, p[i]);
                    priority(pid, at, bt, wt, tat, p, n);
                    break;
                default:
                    printf("\nInvalid choice!");
            }
        } else {
            printf("\n-----------------------------------------------------------------------------------");
            printf("\nEnter the number of processes in ready queue (arrival time of all processes is assumed to be 0 ms): ");
            scanf("%d", &n);
            printf("\n-----------------------------------------------------------------------------------");
            printf("\nEntered data: Choice = %d, Number of processes = %d", ch_algo, n);
            printf("\n-----------------------------------------------------------------------------------");
            int pid[n], at[n], bt[n], wt[n], tat[n], p[n];
            //Initializing Process ID Array
            for(int i = 0; i < n; i++){
                pid[i] = i;
                printf("\nProcess %d has got Process ID %d.", i+1, pid[i]);
            }
            printf("\nProcess ID Array initialized successfully.");
            printf("\n-----------------------------------------------------------------------------------");
            printf("\nEnter arrival time for each process.");
            //Array to store the arrival times of all processes
            for(int i = 0; i < n; i++){
                printf("\nArrival time for Process ID %d: ", pid[i]);
                scanf("%d", &at[i]);
            }
            printf("\n-----------------------------------------------------------------------------------");
            printf("\nEnter burst time (execution time) for each process.");
            //Array to store the burst times of all processes
            for(int i = 0; i < n; i++){
                printf("\nBurst time for Process ID %d: ", pid[i]);
                scanf("%d", &bt[i]);
            }
            printf("\n-----------------------------------------------------------------------------------");
            //Array to store the waiting times of all processes
            //n is the process id.
            //If wt[1] = 5, then it implies that the process with process id 1 has a waiting time of 5 ms.
            wt[0] = 0;
            //The process with process id 0 has a wait time of 0 ms, in a system where all processes arrive at 0 ms.
            //Array to store the turnaround time of all processes
            int quantum;
            switch(ch_algo)
            {
                case 1:
                    printf("\nYou selected SRTN!");
                    srtn(pid, at, bt, wt, tat, n);
                    break;
                case 2:
                    printf("\nYou selected RR!");
                    printf("\nEnter time quantum: ");
                    scanf("%d", &quantum);
                    rr(pid, at, bt, n, quantum);
                    break;
                case 3:
                    printf("\nYou selected Priority!");
                    printf("\n-----------------------------------------------------------------------------------");
                    printf("\nEnter burst time (execution time) for each process.");
                    //Array to store the burst times of all processes
                    for(int i = 0; i < n; i++){
                        printf("\nBurst time for Process ID %d: ", pid[i]);
                        scanf("%d", &p[i]);
                    }
                    for(int i = 0; i < n; i++)
                        printf("\nProcess %d has got Priority %d.", i+1, p[i]);
                    priority(pid, at, bt, wt, tat, p, n);
                    break;
                default:
                    printf("\nInvalid choice!");
            }
        }
        printf("\nDo you want to run the program again? Enter '1' else any integer.\nEnter your choice: ");
        scanf("%d", &ch_run);
        if(ch_run == 1){
            continue;
        } else {
            break;
        }
    }
    return 0;
}


Drawbacks of Preemptive Priority Scheduling Algorithm:

One of the most common drawbacks of the 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 a starvation problem.

Solution: The solution to 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 for the 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.


Unlock the Power of Placement Preparation!
Feeling lost in OS, DBMS, CN, SQL, and DSA chaos? Our Complete Interview Preparation Course is the ultimate guide to conquer placements. Trusted by over 100,000+ geeks, this course is your roadmap to interview triumph.
Ready to dive in? Explore our Free Demo Content and join our Complete Interview Preparation course.

Last Updated : 31 Oct, 2023
Like Article
Save Article
Similar Reads
Related Tutorials