OneCompiler

priority snp

1645

#include<stdio.h>

struct process{
int at;
int burst_time;
int ft;
int tat;
int priority;
int wt;
int temp_bt;
}P[10];

struct schdeule{
int pid;
int endtime;

}sch[100];

int sch_cnt;
int np,i;
int ct;
int highpriority;

void take_input(){
printf("Enter the no of process: ");
scanf("%d",&np);

for(i=0; i<np; i++)
{
printf("enter the arrival time %d: ",i);
scanf("%d",&P[i].at);

printf("enter the burst time%d: ",i);
scanf("%d",&P[i].burst_time);
P[i].temp_bt = P[i].burst_time;
printf("enter the prioriy of process %d :",i);
scanf("%d",&P[i].priority);
}
printf("enter the 0 for acending order and 1 for decending order priority :");
scanf("%d",&highpriority);

}

int sort(){
int minp=-1;
int minpt=100, minpt2=0;
printf("\n");

for(i=0;i<np;i++)
{
if(P[i].at<=ct && P[i].temp_bt>0 && P[i].priority <minpt)
{
minp=i;
minpt=P[i].priority;
}
else if (P[i].at<=ct && P[i].temp_bt>0 && P[i].priority >minpt2)
minp=i;
minpt=P[i].priority;

}
return minp;

}

void showganttchart()
{
for(i=0;i<sch_cnt5;i++)
printf("=");
printf("\n|");
for(i=0;i<sch_cnt;i++)
printf("P%d |",sch[i].pid);
printf("\n");
for(i=0;i<sch_cnt
5;i++)
printf("-");
printf("\n0");
for(i=0;i<sch_cnt;i++)
printf("%5d",sch[i].endtime);
printf("\n");
}

void avgoftatandwt()
{
float sumtat=0 , sumwt=0;
for(i=0;i<np;i++)
{
sumtat += P[i].tat;
sumwt +=P[i].wt;
}

float tat =sumtat /np;
float wt =sumwt /np;
printf("average turn around time is : %.2f\n average wating time is: %.2f\n ",tat,wt);
}
void display(){
printf("\nprocess\tat\tbt\tft\ttat\twt\n");

for(i=0;i<np;i++)
{
printf("P%d\t%d\t%d\t%d\t%d\t%d\n",i,P[i].at,P[i].burst_time,P[i].ft,P[i].tat,P[i].wt);
}

}

void main()
{
take_input();
// calculating all values

while(1){
int x = sort();
if(x==-1)
break;
sch[sch_cnt].pid =x;
ct += P[x].temp_bt;
P[x].temp_bt =0;
P[x].ft =ct;
sch[sch_cnt].endtime =ct;
sch_cnt++;

P[x].tat =P[x].ft - P[x].at;
P[x].wt = P[x].tat - P[x].burst_time;

}

showganttchart();
avgoftatandwt();
display();
}