/*--------------------------------------------------------------------------------
Title: Write a program to implement Priority (Non-Preemptive) [With arrival time] scheduling algorithm.
---------------------------------------------------------------------------------*/
#include<stdio.h>
int n;
struct proc
{
int atime,btime,flag,pid,stime,ftime,wt,tt,prio;
char name[20];
}p[50];
void main()
{
struct proc temp;
int i,j,time=0,t,tt,var,sel,value,temp1=0,temp2=0,high,sel1;
float avgt=0,avgw=0;
int arr[50];
printf("\\n\\t\\t No of Process: ");
scanf("%d",&n);
tt=0;
for(i=0;i<n;i++)
{
p[i].pid=i+1;
printf("\\n\\t\\t\\t enter process name :");
scanf("%s",&p[i].name);
printf("\\n\\t\\t\\t enter process arrival Time :");
scanf("%d",&p[i].atime);
printf("\\n\\t\\t\\t enter process Burst Time :");
scanf("%d",&p[i].btime);
printf("\\n\\t\\t\\t enter process priority :");
scanf("%d",&p[i].prio);
p[i].flag=0;
tt=tt+p[i].btime;
}
for(i=1;i<n;i++)
{
for(j=0;j<n;j++)
{
if(p[j].atime>p[i].atime)
{
temp=p[i];
p[i]=p[j];
p[j]=temp;
}
}
}
var=p[0].atime;
tt=tt+var;
time=var;
value=var;
while(time<tt)
{
for(i=0;i<n;i++)
{
if(p[i].btime==0)
{
p[i].flag=-1;
}
}
for(i=0;i<n;i++)
{
if(p[i].atime<=time&&p[i].flag!=-1)
{
p[i].flag=1;
}
}
t=tt+1;
high=-1;
for(i=0;i<n;i++)
{
if(p[i].flag==1)
{
if(p[i].prio>high)
{
high=p[i].prio;
sel1=i;
}
else if(p[i].prio==high)
{
if(p[i].btime<p[sel1].btime)
{
sel1=i;
}
}
}
}
printf("--%d--%s",value,p[sel1].name);
p[sel1].stime=value;
value=value+p[sel1].btime;
p[sel1].ftime=value;
p[sel1].btime=0;
time=value;
}
printf("--%d",value);
// printf("\\n\\t p id \\t stime \\t ftime");
for(i=0;i<n;i++)
{
p[i].wt=p[i].stime-p[i].atime;
temp1=temp1+p[i].wt;
p[i].tt=p[i].ftime-p[i].atime;
temp2=temp2+p[i].tt;
}
printf("\\n proc\\tstart\\tfinish\\twait\\t tt");
for(i=0;i<n;i++)
{
printf("\\n\\t p%s\\t%d\\t%d\\t%d\\t%d\\n",p[i].name,p[i].stime,p[i].ftime,p[i].wt,p[i].tt);
}
avgw=(float)temp1/n;
avgt=(float)temp2/n;
printf("\\n\\t average wating time is=%f\\n\\t average turn around time=%f\\n",avgw,avgt );
printf("\\n\\t Throughput =%f",(n/(float)time));
}
// END OF THE PROGRAM
/*
//------------------
// OUTPUT
//------------------
gescoe@gescoe-Vostro-230:~/Desktop/TE$ gcc a6_PRIO_NonP_W_AT.c
gescoe@gescoe-Vostro-230:~/Desktop/TE$ ./a.out
No of Process: 4
enter process name :P1
enter process arrival Time :0
enter process Burst Time :2
enter process priority :1
enter process name :P2
enter process arrival Time :0
enter process Burst Time :1
enter process priority :3
enter process name :P3
enter process arrival Time :0
enter process Burst Time :4
enter process priority :1
enter process name :P4
enter process arrival Time :0
enter process Burst Time :3
enter process priority :2
GANTT CHART
-------------------------------------------------------
--0--P2--1--P4--4--P1--6--P3--10
----------------------------------------------------------------
proc start finish wait tt
-----------------------------------------------------------------
pP1 4 6 4 6
pP2 0 1 0 1
pP3 6 10 6 10
pP4 1 4 1 4
-----------------------------------------------------------------
Average Wating time is=2.750000
average Turn around time=5.250000
Throughput =0.4
// EXIT
*/