//
#include<stdio.h>
struct process
{
int at,bt,wt,tat,st,ft,flag,id,tbt;
}p[10],temp;
int n,t,save_et[10],save_id[10],turn,btsum;
float awt,atat;
void read();
void print();
void rndrbn();
void fifoq();
main()
{
int ch;
do
{
printf("\n
Enter choice:1.RR 3.exit.ENTER UR CHOICE\n");
scanf("%d",&ch);
switch(ch)
{
case 1:read();
fifoq();
rndrbn();
print();
break;
case 3:
return;
default:
printf("
invalid choice
");
}
}while(1);
}
void read()
{
int i;
printf("
Enter no of processes:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("
Enter arriving time ,burst time of process p%d : ",(i+1));
scanf("%d%d",&p[i].at,&p[i].bt);
p[i].id=i+1;
p[i].wt=p[i].flag=0;
p[i].tbt=p[i].bt;
btsum+=p[i].bt;
}
printf("
Enter time quantum : ");
scanf("%d",&t);
}
void fifoq()
{
int i,j;
for(i=0;i<n;i++)
{
for(j=0;j<n-i-1;j++)
{
if(p[j].at>p[j+1].at)
{
temp=p[j];
p[j]=p[j+1];
p[j+1]=temp;
}
}
}
}
void rndrbn()
{
//int tmp;
int cnt=n;
int i=0;
int et=0;
int sum=0;
float twt=0;
float ttat=0;
while(cnt!=0)
{
if((p[i].bt)>t)
{
et=t;
p[i].bt-=t;
}
else
{
et=p[i].bt;
p[i].bt=0;
}
p[i].st=sum;
if((p[i].flag)==0)
{
p[i].wt=p[i].st-p[i].at;
p[i].flag++;
}
else
p[i].wt=p[i].wt+(p[i].st-p[i].ft);
sum=sum+et;
p[i].ft=sum;
save_et[turn]=et;
save_id[turn++]=p[i].id;
if((p[i].bt)==0)
{
cnt--;
}
do
{
i=(i+1)%n;
}while((p[i].bt)==0 && cnt!=0);
}
for(i=0;i<n;i++)
{
p[i].tat=p[i].wt+p[i].tbt;
twt+=p[i].wt;
ttat+=p[i].tat;
}
awt=twt/n;
atat=ttat/n;
}
void print()
{
int i,sum=0;
for(i=0;i<=btsum;i++)
printf("-");
printf("
|");
for(i=0;i<turn;i++)
printf("%*d|",-(save_et[i]-1),save_id[i]);
printf("
");
for(i=0;i<=btsum;i++)
printf("-");
printf("
");
for(i=0;i<turn;i++)
{
printf("%*d",-(save_et[i]),sum);
sum+=save_et[i];
}
printf("%d",sum);
for(i=0;i<n;i++)
{
printf("
%d %d %d",p[i].id,p[i].wt,p[i].tat);
}
printf("
%f %.2f",awt,atat);
btsum=0;
}