Priority Scheduling Program in C and C++

Here you will get the implementation of the priority scheduling algorithm in C and C++.

What is Priority Scheduling Algorithm?

In the priority scheduling algorithm, each process has a priority associated with it and as each process hits the queue, it is stored based on its priority so that processes with higher priority is dealt with first. It should be noted that equal priority processes are scheduled in FCFS order.

Also Read: FCFS Scheduling Program in C and C++

To prevent high priority processes from running indefinitely the scheduler may decrease the priority of the currently running process at each clock tick (i.e., at each clock interrupt). If this action causes its priority to drop below that of the next highest process, a process switch occurs. Alternatively, each process may be assigned a maximum time quantum that it is allowed to run. When this quantum is used up, the next highest priority process is given a chance to run.

Example:

Here is an example of a Priority Scheduling algorithm.

ProcessArrival TimeBurst TimePriority
P1052
P2131
P3264
P4443
P5625

In this example, there are 5 processes with their arrival time, burst time, and priority. The execution order, waiting time, and turnaround time for each process will be as given below.

ProcessBurst TimeWaiting TimeTurnaround Time
P2303
P1538
P44812
P361218
P521820
  • Average Waiting Time = (0 + 3 + 8 + 12 + 18) / 5 = 8.2
  • Average Turnaround Time = (3 + 8 + 12 + 18 + 20) / 5 = 12.2

Limitations

The problem occurs when the operating system gives a particular task a very low priority, so it sits in the queue for a larger amount of time, not being dealt with by the CPU. If this process is something the user needs, there could be a very long wait, this process is known as “Starvation” or “Infinite Blocking”.

Solution

Many operating systems use a technique called “aging”, in which a low priority process slowly gains priority over time as it sits in the queue. Even if, the priority of the process is low, there is a surety of its execution.

C Program for Priority Scheduling

#include<stdio.h>
int main()
{
    int bt[20],p[20],wt[20],tat[20],pr[20],i,j,n,total=0,pos,temp,avg_wt,avg_tat;
    printf("Enter Total Number of Process:");
    scanf("%d",&n);
    printf("\nEnter Burst Time and Priority\n");
    for(i=0;i<n;i++)
    {
        printf("\nP[%d]\n",i+1);
        printf("Burst Time:");
        scanf("%d",&bt[i]);
        printf("Priority:");
        scanf("%d",&pr[i]);
        p[i]=i+1;           //contains process number
    }
    //sorting burst time, priority and process number in ascending order using selection sort
    for(i=0;i<n;i++)
    {
        pos=i;
        for(j=i+1;j<n;j++)
        {
            if(pr[j]<pr[pos])
                pos=j;
        }
        temp=pr[i];
        pr[i]=pr[pos];
        pr[pos]=temp;
        temp=bt[i];
        bt[i]=bt[pos];
        bt[pos]=temp;
        temp=p[i];
        p[i]=p[pos];
        p[pos]=temp;
    }
    wt[0]=0;	//waiting time for first process is zero
    //calculate waiting time
    for(i=1;i<n;i++)
    {
        wt[i]=0;
        for(j=0;j<i;j++)
            wt[i]+=bt[j];
        total+=wt[i];
    }
    avg_wt=total/n;      //average waiting time
    total=0;
    printf("\nProcess\t    Burst Time    \tWaiting Time\tTurnaround Time");
    for(i=0;i<n;i++)
    {
        tat[i]=bt[i]+wt[i];     //calculate turnaround time
        total+=tat[i];
        printf("\nP[%d]\t\t  %d\t\t    %d\t\t\t%d",p[i],bt[i],wt[i],tat[i]);
    }
    avg_tat=total/n;     //average turnaround time
    printf("\n\nAverage Waiting Time=%d",avg_wt);
    printf("\nAverage Turnaround Time=%d\n",avg_tat);
	return 0;
}

C++ Program for Priority Scheduling

#include<iostream>
using namespace std;
int main()
{
    int bt[20],p[20],wt[20],tat[20],pr[20],i,j,n,total=0,pos,temp,avg_wt,avg_tat;
    cout<<"Enter Total Number of Process:";
    cin>>n;
    cout<<"\nEnter Burst Time and Priority\n";
    for(i=0;i<n;i++)
    {
        cout<<"\nP["<<i+1<<"]\n";
        cout<<"Burst Time:";
        cin>>bt[i];
        cout<<"Priority:";
        cin>>pr[i];
        p[i]=i+1;           //contains process number
    }
    //sorting burst time, priority and process number in ascending order using selection sort
    for(i=0;i<n;i++)
    {
        pos=i;
        for(j=i+1;j<n;j++)
        {
            if(pr[j]<pr[pos])
                pos=j;
        }
        temp=pr[i];
        pr[i]=pr[pos];
        pr[pos]=temp;
        temp=bt[i];
        bt[i]=bt[pos];
        bt[pos]=temp;
        temp=p[i];
        p[i]=p[pos];
        p[pos]=temp;
    }
    wt[0]=0;            //waiting time for first process is zero
    //calculate waiting time
    for(i=1;i<n;i++)
    {
        wt[i]=0;
        for(j=0;j<i;j++)
            wt[i]+=bt[j];
        total+=wt[i];
    }
    avg_wt=total/n;      //average waiting time
    total=0;
    cout<<"\nProcess\t    Burst Time    \tWaiting Time\tTurnaround Time";
    for(i=0;i<n;i++)
    {
        tat[i]=bt[i]+wt[i];     //calculate turnaround time
        total+=tat[i];
        cout<<"\nP["<<p[i]<<"]\t\t  "<<bt[i]<<"\t\t    "<<wt[i]<<"\t\t\t"<<tat[i];
    }
    avg_tat=total/n;     //average turnaround time
    cout<<"\n\nAverage Waiting Time="<<avg_wt;
    cout<<"\nAverage Turnaround Time="<<avg_tat;
    return 0;
}

Output:

C/C++ Program for Priority Scheduling Algorithm

I hope you got the idea of how to implement a priority scheduling algorithm. Comment below for any queries related to priority scheduling.

34 thoughts on “Priority Scheduling Program in C and C++”

  1. Error 2 error C1010: unexpected end of file while looking for precompiled header. Did you forget to add ‘#include “StdAfx.h”‘ to your source?
    how can I repair it ???

  2. #include
    struct process
    {
    int pid;
    int btime;
    int wtime;
    int ttime;
    int prtime;
    } p[10];
    main()
    {
    int i,j,k,n,ttur,twat,temppro,tempburst,temppir;
    float awat,atur;
    printf(“Enter no. of process :”);
    scanf(“%d”, &n);
    for(i=0; i<n; i++)
    {
    printf("\nBurst time for process P%d (in ms) :",(i+1));
    scanf("\t%d", &p[i].btime);
    printf("\n Enter priority\t");
    scanf("%d",&p[i].prtime);
    p[i].pid = i+1;
    }
    for(i=0;i<n;i++)
    {
    for(j=i+1;jp[j].prtime)
    {
    tempburst=p[i].btime;
    p[i].btime=p[j].btime;
    p[j].btime=tempburst;
    temppro=p[i].pid;
    p[i].pid=p[j].pid;
    p[j].pid=temppro;
    temppir=p[i].prtime;
    p[i].prtime=p[j].prtime;
    p[j].prtime=temppir;

    }

    }

    }

    p[0].wtime = 0;
    for(i=0; i<n; i++)
    {
    p[i+1].wtime = p[i].wtime + p[i].btime;
    p[i].ttime = p[i].wtime + p[i].btime;
    }
    ttur = twat = 0;
    for(i=0; i<n; i++)
    {
    ttur+=p[i].ttime;
    twat+=p[i].wtime;
    }
    awat = (float)twat/n;
    atur = (float)ttur/n;

    printf("\n priority Scheduling ALGORITHM \n\n");
    for(i=0; i<38; i++)
    printf("-");
    printf("\n Process B-Time T-Time W-Time Priority\n");
    for(i=0; i<38; i++)
    printf("-");
    for(i=0; i<n; i++)
    printf("\n P%d\t%4d\t%3d\t%2d\t%d",p[i].pid,p[i].btime,p[i].ttime,p[i].wtime,p[i].prtime);
    printf("\n");
    for(i=0;i<38;i++)
    printf("-");
    printf("\n\nGANTT Chart\n");
    printf("-");
    for(i=0; i<(p[n-1].ttime + 2*n);i++)
    printf("-");
    printf("\n");
    printf("|");
    for(i=0;i<n;i++)
    {
    k = p[i].btime/2;
    for(j=0; j<k;j++)
    printf(" ");
    printf("P%d",p[i].pid);
    for(j=k+1;j<p[i].btime;j++)
    printf(" ");
    printf("|");
    }
    printf("\n");
    printf("-");
    for(i=0;i<(p[n-1].ttime + 2*n);i++)
    printf("-");
    printf("\n");
    printf("0");
    for(i=0;i<n;i++)
    {
    for(j=0;j<p[i].btime;j++)
    printf(" ");
    printf("%2d",p[i].ttime);
    }
    printf("\n\nAverage waiting time: %5.2fms", awat);
    printf("\nAverage turn around time : %5.2fms\n", atur);
    }

    OUTPUT:–
    Enter no. of process :5

    Burst time for process P1 (in ms) :10

    Enter priority 4

    Burst time for process P2 (in ms) :12

    Enter priority 8

    Burst time for process P3 (in ms) :5

    Enter priority 8

    Burst time for process P4 (in ms) :14

    Enter priority 6

    Burst time for process P5 (in ms) :17

    Enter priority 9

    priority Scheduling ALGORITHM

    ————————————–
    Process B-Time T-Time W-Time Priority
    ————————————–
    P1 10 10 0 4
    P4 14 24 10 6
    P3 5 29 24 8
    P2 12 41 29 8
    P5 17 58 41 9
    ————————————–

    GANTT Chart
    ———————————————————————
    | P1 | P4 | P3 | P2 | P5 |

    ———————————————————————
    0 10 24 29 41 58

    Average waiting time: 20.80ms
    Average turn around time : 32.40ms

    1. you did not mention in your program that it is for premtive or non-premtive .
      as in priority both are possible.
      so kindly mention it.

    2. Can u plz tell me on which application I can execute these priority programs because these are not running on my PC….

  3. If i want to take input (process id, Burst time, Priority) in a single text file what i need to to for it?can you please help me in this process??

  4. There is a small mistake in the programs(c & c++) which causes the sorting mistake.The processes should be sorted based on the priority but it doesnt.Here is the correct one
    for(i=0;i<n;i++)
    {
    pos=i;
    for(j=i+1;jpr[pos])
    pos=j;
    }

    temp=pr[i];
    pr[i]=pr[pos];
    pr[pos]=temp;

    temp=bt[i];
    bt[i]=bt[pos];
    bt[pos]=temp;

    temp=p[i];
    p[i]=p[pos];
    p[pos]=temp;
    }

  5. If a teacher is being served and during the period when he is being served, another teacher comes, then that teacher would get the service next. This process might continue leading to increase in waiting time of students. Ensure in your program that the waiting time of students is minimized.

    can you please solve this question

  6. how are you too sure that all processes are arriving at same time… all your CPU scheduling programs has same bullshit that everyone can do. send me the program of primitive SJF (SJRF) with diffrent arrival time

  7. C – programme code for preemptive priority scheduling based on dynamically changing priority
    larger number indicates larger priority ?

  8. Sir Please help me with this question
    If a teacher is being served and during the period when he is being served another teacher comes,
    then that teacher would get the service next.
    This process might continue leading to increase in waiting time of students.
    Write a C program to ensure in your program that the waiting time of students is minimized.
    Assume values of arrival time, burst time by your own.

  9. Ques 23. Design a scheduling program to implements a Queue with two levels:
    Level 1 : Fixed priority preemptive Scheduling
    Level 2 : Round Robin Scheduling
    For a Fixed priority preemptive Scheduling (Queue 1), the Priority 0 is highest priority. If one
    process P1 is scheduled and running, another process P2 with higher priority comes. The
    New process (high priority) process P2 preempts currently running process P1 and process P1
    will go to second level queue. Time for which process will strictly execute must be
    considered in the multiples of 2.
    All the processes in second level queue will complete their execution according to round
    robin scheduling.
    Consider: 1. Queue 2 will be processed after Queue 1 becomes empty.
    2. Priority of Queue 2 has lower priority than in Queue 1.

  10. write a c program to simulate CPU scheduling algorithm to find turnaround time and waiting time for the priority scheduling use only do-while loop………..ye program kr do

  11. the output is not being shown in floats even it im changing the avt wt time and avg tt to float its just showing .00000 but not the answer that needs to be actually be there

  12. Md. Abdur Rahim

    Sample Input:
    Number of processes: 5
    Burst Times: 11 28 2 10 16
    Arrival Times: 0 5 12 2 9
    Priority: 2 0 3 1 4
    Time Quantum: 4

Leave a Comment

Your email address will not be published. Required fields are marked *