C Program for Priority CPU Scheduling Algorithm

OUTPUT:
enter no of processes: 5

enter process1 name: aaa
enter process time: 4
enter priority:5
enter process2 name: bbb
enter process time: 3
enter priority:4
enter process3 name: ccc
enter process time: 2
enter priority:3
enter process4 name: ddd
enter process time: 5
enter priority:2
enter process5 name: eee
enter process time: 1
enter priority:1

p_name P_time priority w_time
eee 1 1 0
ddd 5 2 1
ccc 2 3 6
bbb 3 4 8
aaa 4 5 11

total waiting time=26
avg waiting time=5.20

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include<stdio.h>
#include<conio.h>
void main()
{
char p[10][5],temp[5];
int i,j,pt[10],wt[10],totwt=0,pr[10],temp1,n;
float avgwt;
clrscr();
printf("enter no of processes:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("enter process%d name:",i+1);
scanf("%s",&p[i]);
printf("enter process time:");
scanf("%d",&pt[i]);
printf("enter priority:");
scanf("%d",&pr[i]);
}
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(pr[i]>pr[j])
{
temp1=pr[i];
pr[i]=pr[j];
pr[j]=temp1;
temp1=pt[i];
pt[i]=pt[j];
pt[j]=temp1;
strcpy(temp,p[i]);
strcpy(p[i],p[j]);
strcpy(p[j],temp);
}
}
}
wt[0]=0;
for(i=1;i<n;i++)
{
wt[i]=wt[i-1]+et[i-1];
totwt=totwt+wt[i];
}
avgwt=(float)totwt/n;
printf("p_name\t p_time\t priority\t w_time\n");
for(i=0;i<n;i++)
{
printf(" %s\t %d\t %d\t %d\n" ,p[i],pt[i],pr[i],wt[i]);
}
printf("total waiting time=%d\n avg waiting time=%f",tot,avg);
getch();
}

OUTPUT:
enter no of processes: 5

enter process1 name: aaa
enter process time: 4
enter priority:5
enter process2 name: bbb
enter process time: 3
enter priority:4
enter process3 name: ccc
enter process time: 2
enter priority:3
enter process4 name: ddd
enter process time: 5
enter priority:2
enter process5 name: eee
enter process time: 1
enter priority:1

p_name P_time priority w_time
eee 1 1 0
ddd 5 2 1
ccc 2 3 6
bbb 3 4 8
aaa 4 5 11

total waiting time=26
avg waiting time=5.20

Editorial Team
Editorial Team

We are a group of young techies trying to provide the best study material for all Electronic and Computer science students. We are publishing Microcontroller projects, Basic Electronics, Digital Electronics, Computer projects and also c/c++, java programs.

42 thoughts on “C Program for Priority CPU Scheduling Algorithm

  1. please give me a simple code for priority preemptive scheduling having different arrival time for each process…

  2. Implemented in Dev c++

    Priority.cpp

    #include
    #include
    #include
    #include “process.h”
    #include “pfunctions.h”

    using namespace std;

    int main(int argc, char *argv[])
    {
    process Queue[20];
    int num;
    cout<>num;
    //process* Queue = new process[num];
    cout<<"Enter the following details"<<endl;
    Get_Details(Queue,num);
    Sort_Processes(Queue,num);
    makeQueue(Queue,num);
    make_Gantt_Chart(Queue,num);
    make_Table(Queue,num);
    system("PAUSE");
    return EXIT_SUCCESS;
    }

    process.h

    #include
    #include
    #include

    using namespace std;

    struct process
    {
    string name;
    int arrival;
    int burst;
    int prior;
    int tat;
    int wait;
    int serve_time;
    };

    pfunctions.h

    #include
    #include
    #include

    void Get_Details(process Q[],int n);
    void Sort_Processes(process Q[],int n);
    void swap(process Q[],int i,int j);
    void makeQueue(process Q[],int n);
    void make_Gantt_Chart(process Q[],int n);
    void make_Table(process Q[],int n);

    using namespace std;

    void Get_Details(process Queue[],int n)
    {
    for(int i=0;i<n;i++)
    {
    cout<>Queue[i].name >> Queue[i].arrival >> Queue[i].burst >> Queue[i].prior;
    //cout<<endl;
    }

    }
    void Sort_Processes(process Q[],int n)
    {
    int current_time=0,i,j;

    //Insertion Sort w.r.t Arrival time
    for(i=0;i<n;i++)
    for(j=i+1;jQ[j].arrival)
    swap(Q,i,j);
    // Isertion sort w.r.t priority+arrival+burst
    for(i=0;i current_time)
    current_time = Q[i].arrival;
    for(j=i+1;j<n;j++)
    {
    if(Q[j].arrivalQ[j].prior)
    swap(Q,i,j);
    else if(Q[i].prior==Q[j].prior && Q[i].arrival>Q[j].arrival)
    swap(Q,i,j);
    else if(Q[i].prior==Q[j].prior && Q[i].arrival==Q[j].arrival && Q[i].burst>Q[j].burst)
    swap(Q,i,j);
    }
    }
    Q[i].serve_time = current_time;
    Q[i].tat = (current_time+Q[i].burst)-Q[i].arrival;
    Q[i].wait = current_time – Q[i].arrival;
    current_time = current_time + Q[i].burst;
    }
    }
    void swap(process Q[],int i,int j)
    {
    process p;
    p.arrival=Q[i].arrival;
    Q[i].arrival=Q[j].arrival;
    Q[j].arrival=p.arrival;

    p.name=Q[i].name;
    Q[i].name=Q[j].name;
    Q[j].name=p.name;

    p.burst=Q[i].burst;
    Q[i].burst=Q[j].burst;
    Q[j].burst=p.burst;

    p.prior=Q[i].prior;
    Q[i].prior=Q[j].prior;
    Q[j].prior=p.prior;
    }
    void makeQueue(process Q[],int n)
    {
    int i;
    cout<<"\n Queue:-\n";
    cout<<"—————————————————————————–\n";
    for(i=0;i<n;i++)
    cout<<" "<<Q[i].name<<" |";
    cout<<"\n—————————————————————————–\n";
    }
    void make_Gantt_Chart(process Q[],int n)
    {
    int i;
    cout<<"\nGantt chart:-\n";
    cout<<"—————————————————————————–\n";
    for(i=0;i<n;i++)
    cout<<" "<<Q[i].name<<" |";
    cout<<"\n—————————————————————————–\n";
    cout<<Q[0].arrival<<" ";
    for(i=0;i<n;i++)
    cout<<" "<<Q[i].serve_time+Q[i].burst<<" ";
    }
    void make_Table(process Q[],int n)
    {
    int i;
    float wait=0,tat=0;
    cout<<"\n\nCalculation:-\n\nName\tBurst\tArrival\tPriority\tWait\tTAT\n";
    for(i=0;i<n;i++)
    {
    cout<<"\n"<<Q[i].name<<"\t"<<Q[i].burst<<"\t";
    cout<<Q[i].arrival<<" \t "<<Q[i].prior<<"\t\t"<<Q[i].wait<<"\t"<<Q[i].tat;
    wait = wait+Q[i].wait ;
    tat = tat + Q[i].tat;
    }
    cout<<"\n——————————————————";
    cout<<"\n\nSUM=\t-\t-\t -\t\t"<<wait<<"\t"<<tat<<endl;
    cout<<"AVERAGE=-\t-\t -\t\t"<<wait/n<<"\t"<<tat/n<<"\n\n";
    }

  3. pls……… i need your help…… can u write out the algorithm of the program………i would really appreciate it.thanks

  4. i want cpu scheduling algorithms roundrobin&priority with arrivaltime needed avgwt,wt,avgtat,tat

  5. pls………i want cpu scheduling algorithms roundrobin&priority with arrivaltime needed avgwt,wt,avgtat,tat .pls….reply me soon

  6. pls………i want cpu scheduling algorithms roundrobin&priority with arrivaltime needed avgwt,wt,avgtat,tat .pls….reply me soon

  7. pls………i want cpu scheduling algorithms roundrobin&priority with arrivaltime needed avgwt,wt,avgtat,tat .pls….reply me soon

  8. I had 1 doubt in 32 line, in function strcpy hw the prototype will be writtern

  9. i want cpu scheduling , process scheduling guaranteed algorithm…
    so can u plz..send me that one !!!!

  10. sir i want this program implementation in ucos RTOS environment..by assigning three tasks execute based on priority plz help me…….,

Leave a Reply

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

Get the latest updates on your inbox

Be the first to receive the latest updates from Codesdoc by signing up to our email subscription.

    StudentProjects.in