You are on page 1of 4

#include<stdio.

h>
main()
{
int c,w[5],a[5],b[5],p[5],i,j,temp,t[5],pr[5],addw=0,addt=0,m=4,ic=0,ch=1;
int avgw,avgt,min,ex,rem,time,max,bc[4];
/*c=choice,w=wait,a=arrival,b=burst,p=process no.,t=turnaround,m=max,pr=priority,ic=i
dex check,ch=check,ex=execute,bc=burst copy*/

for(i=0;i<=4;i++)
{
printf("enter burst time and arrival time for process p%d :- ",p[i]);
scanf("%d",&b[i]);
scanf("%d",&a[i]);
}
for(i=0;i<=4;i++)
{
p[i]=i+1;
t[i]=b[i];
w[i]=0;
bc[i]=b[i];
}
/*Arrival time sort*/
for(i=0;i<4;i++)
{
for(j=i+1;j<=4;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
temp=p[i];
p[i]=p[j];
p[j]=temp;
temp=b[i];
b[i]=b[j];
b[j]=temp;
}
}
}
max=a[4];
/*Burst time sort in arrival time sort*/
for(i=0;i<4;i++)
{
for(j=i+1;j<=4;j++)
{
if(a[i]==a[j]&&b[i]>b[j])
{
temp=p[i];
p[i]=p[j];
p[j]=temp;
temp=b[i];
b[i]=b[j];
b[j]=temp;
}
}
}
b[0]=b[0]-1;
time=a[0]+1;
for(i=0;i<=3,i++)
{
if(a[i]!=a[i+1])

{

m=i; /*index upto which smallest arrival time is same , if any*/

break;

}
}
for(i=0;i<m;i++)
{
w[i+1]=1; /*assigning waiting time to arrived processs*/
}
while(ch!=0)
{
if(time<max)
{
for(i=0;i<=4;i++)
{
if(a[i]>time)
{
min=i-1; /*index no. upto which process
es have arrived*/
break;
}
}
for(i=0;i<=min;i++)
{
for(j=i+1;j<=min;j++)
{
if(b[i]>b[j]) /*sorting arrived processes
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
temp=p[i];
p[i]=p[j];
p[j]=temp;
temp=b[i];
b[i]=b[j];
b[j]=temp;
temp=w[i];
w[i]=w[j];
w[j]=temp;
temp=t[i];
t[i]=t[j];
t[j]=temp;
}
}
}
for(ic=0;ic<=4;i++)
{
if(b[ic]!=0)
{
b[ic]=b[ic]-1;
time=time+1;
break;
}
}
for(i=ic+1;i<=min;i++)
{
w[i]=w[i]+1;
}
}
else if(time>=max)
{
/*sorting according to burst time*/
for(i=0;i<4;i++)
{
for(j=i+1;j<=4;j++)
{
if(b[i]>b[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
temp=p[i];
p[i]=p[j];
p[j]=temp;
temp=b[i];
b[i]=b[j];
b[j]=temp;
temp=w[i];
w[i]=w[j];
w[j]=temp;
temp=t[i];
t[i]=t[j];
t[j]=temp;
}
}
}
/*sorting according to arival time after burst sort*/
for(i=0;i<4;i++)
{
for(j=i+1;j<=4;j++)
{
if(b[i]==b[j]&&a[i]>a[j])
{
temp=p[i];
p[i]=p[j];
p[j]=temp;
temp=a[i];
a[i]=a[j];
a[j]=temp;
temp=w[i];
w[i]=w[j];
w[j]=temp;
temp=t[i];
t[i]=t[j];
t[j]=temp;
}
}
}
for(i=0;i<=4;i++)
{
if(b[i]!=0)
{
for(j=i+1;j<=4;j++)
{
w[j]=w[j]+b[i];
}
b[i]=0;
}
}
}
if(b[0]=0&&b[1]=0&&b[2]=0&&b[3]=0&&b[4]=0)
{
printf("\n\nall processes executed!!\n");
ch=0;
}
}
for(i=0;i<=4;i++)
{
t[i]=t[i]+w[i];
addw=addw+w[i];
addt=addt+t[i];
}
avgw=addw/5;
avgt=addt/5;
printf("\n\nSR NO. PROCESS BURST WAITING TURNAROUND ");
for(i=1;i<=5;i++)
{
printf("\n\n %d %d %d %d %d\n",i,p[i-1],bc[i-1],
}
printf("\n\naverage waiting time = %d\n\n",avgw);
printf("avergae turnaround time = %d\n\n",avgt);
}

You might also like