Site icon DataFlair

Priority Scheduling Algorithm in Operating System

OS Priority Scheduling algorithm

FREE Online Courses: Click, Learn, Succeed, Start Now!

Priority Scheduling is a process scheduling algorithm based on priority where the scheduler selects tasks according to priority. Thus, processes with higher priority execute first followed by processes with lower priorities.

If two jobs have the same priorities then the process that should execute first is chosen on the basis of round-robin or FCFS. Which process should have what priority depends on a process’ memory requirements, time requirements, the ratio of I/O burst to CPU burst, etc.

Types of Priority Scheduling

Following are the two main types of priority scheduling:

1. Preemptive Scheduling: Tasks execute according to their priorities. In case a lower priority task is running and a higher priority task arrives in the waiting state then the lower priority task is put on hold. The higher priority task replaces it and once done executing the lower priority task resumes execution from when it was paused. This process requires special hardware like a timer.

2. Non-Preemptive Scheduling: The OS allocates the CPU to a specific process that releases the CPU either through context switching or after termination. We can use this method on various hardware platforms because unlike preemptive scheduling it doesn’t require any special hardware.

Characteristics of Priority Scheduling

Example of Priority Scheduling

Following five processes have a unique priority, burst time, and arrival time.

Process Priority Burst Time Arrival Time
P1 1 4 0
P2 2 3 0
P3 1 7 6
P4 3 4 11
P5 2 2 12

Step 0: At time = 0, two processes P1 and P2 arrive. Since P1(with a burst time 4) has higher priority it executes before P2.

Step 1: At time = 1, no new process arrives and execution of P1 continues.

Technology is evolving rapidly!
Stay updated with DataFlair on WhatsApp!!

Step 2: At time = 2, no new process arrives, P2 is in the waiting queue, and execution of P1 continues.

Step 3: At time = 3, no new process arrives, P2 is in the waiting queue, and execution of P1 continues.

Step 4: At time = 4, P1 finishes execution, and P2 starts execution.

Step 5: At time = 5, P2 continues execution as no new process arrives.

Step 6: At time = 6, P3 arrives and since it has a higher priority of 1 the OS preempts P2. P3 starts executing.

Process Priority Burst Time Arrival Time
P1 1 4 0
P2 2 1 of 3 pending 0
P3 1 7 6
P4 3 4 11
P5 2 2 12

Step 7: At time = 7, P3 continues executing as no new process arrives and P2 is on hold in the waiting queue.

Step 8: At time = 8, we continue with P3 as no new process arrives.

Step 9: At time = 9, we continue with P3 as no new process arrives.

Step 10: At time = 10, we continue with P3 as no new process arrives.

Step 11: At time = 11, P4 arrives but P3 continues executing as it has a higher priority than P4.

Process Priority Burst Time Arrival Time
P1 1 4 0
P2 2 1 of 3 pending 0
P3 1 7 6
P4 3 4 11
P5 2 2 12

Step 12: At time = 12, P5 arrives but P3 continues executing as it has a higher priority.

Step 13: At time = 13, P3 is done executing. Now there are three processes P2, P4, and P5 in the ready queue. Among these two processes have higher and similar priorities. These are P2 and P5. Since the arrival time of P2 is less than that of P5, P2 starts execution.

Process Priority Burst Time Arrival Time
P1 1 4 0
P2 2 1 of 3 pending 0
P3 1 7 6
P4 3 4 11
P5 2 2 12

Step 14: At time = 14, P2 completes execution. In the waiting state between P4 and P5, P5 has a higher priority so it starts executing.

Step 15: At time = 15, P5 continues executing.

Step 16: At time = 16, P5 completes execution and since P4 is the only process present in the waiting state it starts executing.

Step 17: At time = 20, no process is left for execution as P5 completes execution.

Step 18: Following is the average waiting time: Waiting Time (Wt) = Start Time (St) – Arrival Time (At) + Waiting Time for next burst

P1 = 0 – 0 = 0

P2 = 4 – 0 + 7 = 11

P3 = 6 – 6 = 0

P4 = 16 – 11 = 5

Thus, the average waiting time is: (0+11+0+5+2)/5 = 18/5 = 3.6

Advantages of priority scheduling in OS

Following are the benefits of priority scheduling method:

Disadvantages of priority scheduling in OS

Following are the disadvantages of priority scheduling:

Aging Technique

Aging technique can help prevent starvation of a process. In this, we can increase the priority of the low-priority processes based on their waiting time. This ensures that no process waits for an indefinite time to get CPU time.

Implementing priority scheduling algorithm in C++

Following is the code to implement priority scheduling algorithm in C++:

#include<bits/stdc++.h> 
using namespace std; 
  
struct Process 
{ 
    int pid;  //process ID
    int bt;   //CPU burst time 
    int priority; //priority of the process
}; 
  
bool sortProcesses(Process a, Process b) 
{ 
    return (a.priority > b.priority); 
} 

void findWaitingTime(Process proc[], int n, 
                     int wt[]) 
{ 
    wt[0] = 0; 
  
    for (int  i = 1; i < n ; i++ ) 
        wt[i] =  proc[i-1].bt + wt[i-1] ; 
} 
  
void findTurnAroundTime( Process proc[], int n, 
                         int wt[], int tat[]) 
{
    for (int  i = 0; i < n ; i++) 
        tat[i] = proc[i].bt + wt[i]; 
} 
 
void findavgTime(Process proc[], int n) 
{ 
    int wt[n], tat[n], total_wt = 0, total_tat = 0; 
    findWaitingTime(proc, n, wt); 
  
    findTurnAroundTime(proc, n, wt, tat); 
  
    cout << "\nProcesses  "<< " Burst time  "
         << " Waiting time  " << " Turnaround time\n"; //process details
  
    for (int  i=0; i<n; i++) 
    { 
        total_wt = total_wt + wt[i]; 
        total_tat = total_tat + tat[i]; 
        cout << "   " << proc[i].pid << "\t\t"
             << proc[i].bt << "\t    " << wt[i] 
             << "\t\t  " << tat[i] <<endl; 
    } 
  
    cout << "\nAverage waiting time = "
         << (float)total_wt / (float)n; 
    cout << "\nAverage turnaround time = "
         << (float)total_tat / (float)n; 
} 
  
void priorityScheduling(Process proc[], int n) 
{ 
    sort(proc, proc + n, sortProcesses); 
  
    cout<< "Order in which processes gets executed \n"; 
    for (int  i = 0 ; i <  n; i++) 
        cout << proc[i].pid <<" " ; 
  
    findavgTime(proc, n); 
} 
  
// Driver code 
int main() 
{ 
    Process proc[] = {{1, 10, 2}, {2, 5, 0}, {3, 8, 1}}; 
    int n = sizeof proc / sizeof proc[0]; 
    priorityScheduling(proc, n); 
    return 0; 
}

Summary

Priority Scheduling is a process scheduling algorithm based on priority where the scheduler selects tasks according to priority. Processes with higher priority execute first followed by the ones with lower priority. There are two types of priority scheduling: preemptive and non-preemptive. Aging techniques can help prevent starvation.

Exit mobile version