C/C++ Program for Priority Scheduling Algorithm

Here you will get C and C++ program for priority scheduling algorithm.

What is Priority Scheduling Algorithm?

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

Also Read: C Program for Shortest Job First (SJF) Scheduling Algorithm

 

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.
 

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

 

C++ Program

 

Output

C/C++ Program for Priority Scheduling Algorithm

8 thoughts on “C/C++ Program for Priority Scheduling Algorithm

  1. amr tarek

    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 ???

    Reply
  2. Pawan Satpute

    #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

    Reply
    1. abhay

      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.

      Reply
  3. Mayur Agarwal

    Can you please provide the code for ASAP and ALAP scheduling algorithm ?

    Reply

Leave a Reply

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