/*CSEMATTERBLOG
PROGARM TO IMPLEMENT ROUND ROBIN ALGO*/
#include<stdio.h>
#include<conio.h>
int main()
{
int bt[20],gc[20],wt[20],tat[20],bt1[20],st[20],ts,n,i,j,k,count=0,count1,sum_bt=0,tq;
int swt=0,stat=0,temp,sq=0,c,p=0;
float awt=0.0,atat=0.0;
printf("Enter the nuo. of processs");
scanf("%d",&n);
printf("Enter the burst time");
for(i=0; i<n; i++)
{scanf("%d",&bt[i]);
bt1[i]=bt[i];
st[i]=bt[i];}
printf("Enter the time slice");
scanf("%d",&ts);
tq=ts;
for(i=0; i<n; i++)
sum_bt=sum_bt+bt[i];
for(k=0; k<n; k++)
{
do
{
for(i=0; i<n; i++)
{
if(bt[i]>=ts)
{
for(j=count; j<(count+ts); j++)
gc[j]=i+1;
count+=ts;
bt[i]=bt[i]-ts;
}
else
{
for(j=count; j<=(count+bt[i]); j++)
gc[j]=i+1;
count+=bt[i];
bt[i]=0;
}
}
}while(bt[k]!=0);
}
while(1)
{
for(i=0,count=0;i<n;i++)
{
temp=tq;
if(st[i]==0)
{
count++;
continue;
}
if(st[i]>tq)
st[i]=st[i]-tq;
else
if(st[i]>=0)
{
temp=st[i];
st[i]=0;
}
sq=sq+temp;
tat[i]=sq;
}
if(n==count)
break;
}
for(i=0;i<n;i++)
{
wt[i]=tat[i]-bt1[i];
swt=swt+wt[i];
stat=stat+tat[i];
}
awt=(float)swt/n;
atat=(float)stat/n;
printf("Process_no Burst time Wait time Turn around time\n");
for(i=0;i<n;i++)
printf("%d %d %d %d\n",i+1,bt1[i],wt[i],tat[i]);
printf("Avg wait time is %f Avg turn around time is %f",awt,atat);
printf("\n \n");
printf("GANTT CHART: \n");
c=gc[0];
printf(" ||P%d\t",gc[0]);
for(i=1;i<sum_bt;i++)
{
if(gc[i]!=c)
{printf(" ||P%d|| ",gc[i]);
c=gc[i];}
}
printf("\n0\t");
c=gc[0];
for(i=1;i<=sum_bt;i++)
{ p=p+1;
if(gc[i]!=c)
{printf("%d \t",p);
c=gc[i];
}}
getch();
}
/*OUTPUT:
Enter the no. of proceses 5
Enter the burst time of processes
5 1 2 2 3
Enter the time slice 2
Process Burst Time Waiting Time Turn AroundTime
1 5 8 13
2 1 2 3
3 2 3 5
4 2 5 7
5 3 9 12
GANTT CHART:
||P1|| ||P2|| ||P3|| ||P4|| ||P5|| ||P1|| ||P5|| ||P1||
0 2 3 5 7 9 11 12 13*/
PROGARM TO IMPLEMENT ROUND ROBIN ALGO*/
#include<stdio.h>
#include<conio.h>
int main()
{
int bt[20],gc[20],wt[20],tat[20],bt1[20],st[20],ts,n,i,j,k,count=0,count1,sum_bt=0,tq;
int swt=0,stat=0,temp,sq=0,c,p=0;
float awt=0.0,atat=0.0;
printf("Enter the nuo. of processs");
scanf("%d",&n);
printf("Enter the burst time");
for(i=0; i<n; i++)
{scanf("%d",&bt[i]);
bt1[i]=bt[i];
st[i]=bt[i];}
printf("Enter the time slice");
scanf("%d",&ts);
tq=ts;
for(i=0; i<n; i++)
sum_bt=sum_bt+bt[i];
for(k=0; k<n; k++)
{
do
{
for(i=0; i<n; i++)
{
if(bt[i]>=ts)
{
for(j=count; j<(count+ts); j++)
gc[j]=i+1;
count+=ts;
bt[i]=bt[i]-ts;
}
else
{
for(j=count; j<=(count+bt[i]); j++)
gc[j]=i+1;
count+=bt[i];
bt[i]=0;
}
}
}while(bt[k]!=0);
}
while(1)
{
for(i=0,count=0;i<n;i++)
{
temp=tq;
if(st[i]==0)
{
count++;
continue;
}
if(st[i]>tq)
st[i]=st[i]-tq;
else
if(st[i]>=0)
{
temp=st[i];
st[i]=0;
}
sq=sq+temp;
tat[i]=sq;
}
if(n==count)
break;
}
for(i=0;i<n;i++)
{
wt[i]=tat[i]-bt1[i];
swt=swt+wt[i];
stat=stat+tat[i];
}
awt=(float)swt/n;
atat=(float)stat/n;
printf("Process_no Burst time Wait time Turn around time\n");
for(i=0;i<n;i++)
printf("%d %d %d %d\n",i+1,bt1[i],wt[i],tat[i]);
printf("Avg wait time is %f Avg turn around time is %f",awt,atat);
printf("\n \n");
printf("GANTT CHART: \n");
c=gc[0];
printf(" ||P%d\t",gc[0]);
for(i=1;i<sum_bt;i++)
{
if(gc[i]!=c)
{printf(" ||P%d|| ",gc[i]);
c=gc[i];}
}
printf("\n0\t");
c=gc[0];
for(i=1;i<=sum_bt;i++)
{ p=p+1;
if(gc[i]!=c)
{printf("%d \t",p);
c=gc[i];
}}
getch();
}
/*OUTPUT:
Enter the no. of proceses 5
Enter the burst time of processes
5 1 2 2 3
Enter the time slice 2
Process Burst Time Waiting Time Turn AroundTime
1 5 8 13
2 1 2 3
3 2 3 5
4 2 5 7
5 3 9 12
GANTT CHART:
||P1|| ||P2|| ||P3|| ||P4|| ||P5|| ||P1|| ||P5|| ||P1||
0 2 3 5 7 9 11 12 13*/
can you please give the code with arrival time?
ReplyDelete