Вы находитесь на странице: 1из 4

/* SHARANABASU G ANGADI */

/* Information Science & Engineering */


/* BEC Bagalkot , (Karnataka) INDIA. */
/* angadi.sharan@yahoo.in */
Write a C/C++ program to compute average waiting time and average turnaround
time for Round robin scheduling algorithm. The program should accept arrival
time and burst time as input. Assume suitable time quantum as input.

$vim 5.c
#include<stdio.h>
typedef struct proc
{ int at,bt,status,t_bt;
int proc,est;
}P;
typedef struct et
{ int st,et,p;
}ET;

P p[15];
ET ti[40];
int z;//to maintain time count

int check(int n)
{ int i;
for(i=0;i<n;i++)
{ if(p[i].status==0)
return 1;
}
return 0;
}

void cal_proc(int n,int qtm) //qtm is Quantum Time


{ int ct=0,i,j,pros;
while(check(n))
{
for(i=0;i<n;i++)
{
if(p[i].status==0 && p[i].at<=ct)
{ if(p[i].bt>qtm)
{ p[i].bt-=qtm;
ti[z].st=ct;
ti[z].et=ct+qtm;
ti[z].p=i;
z++;
ct+=qtm;
}
else
{
p[i].status=1;
ti[z].st=ct;
ti[z].et=ct+p[i].bt;
ct+=p[i].bt;
ti[z].p=i;
z++;
}//ELSE
}//if
}//for
}//while
}//FUNCTION

void print_awt_atat(int n)
{ int i,j,flag,tat[15],wt[15];
float temp=0,atat=0,awt=0;
for(j=0;j<n;j++)
{ flag=1;
for(i=0;i<z;i++)
{
if(ti[i].p==j)
{ if(flag)
wt[j]=ti[i].st,flag=0;
temp=0;
while(ti[i+1].p!=j && i+1<z)
{ temp+=(ti[i+1].et-ti[i+1].st);
i++;
}
if(i+1<z)
wt[j]+=temp;
}
}
wt[j]-=p[j].at;
tat[j]=wt[j]+p[j].t_bt;
}
for(i=0;i<n;i++)
{
awt+=wt[i];
atat+=tat[i];
}
awt/=n*1.0;
atat/=n*1.0;

printf("\n\tProc A_T B_T W_T TA_T\t");


printf("\n\t______________________________________\n\n");
for(i=0;i<n;i++)
printf("\t%d\t%d\t%d\t%d\t%d\n",p[i].proc,p[i].at,p[i].t_bt,wt[i],tat[i]);
printf("\n\t_______________________________________\n");
printf("\n\n\t\tAverage Waiting time = %f\n",awt);
printf("\n\n\t\tAverage Turn around time = %f\n",atat);
}

int main()
{ int n,i,qntm;
printf("\n\n\t\tEnter the no of Process (max 15) : ");
scanf("%d",&n);
printf("\n\n\t\tEnter the arrivel time of process \n");
for(i=0;i<n;i++)
{ printf("\t\tprocess P%d : ",i);
scanf("%d",&p[i].at);
}
printf("\n\n\t\tEnter the Burst time of process \n");
for(i=0;i<n;i++)
{ printf("\t\tprocess P%d : ",i);
scanf("%d",&p[i].bt);
p[i].status=0;
p[i].proc=i;
p[i].t_bt=p[i].bt;
}
printf("\n\n\t\tEnter the Quantum Time : ");
scanf("%d",&qntm);
cal_proc(n,qntm);
print_awt_atat(n);

return 0;

/*===========================Out_put=================================*/
$cc 5.c
$./a.out
Enter the no of Process (max 15) : 4
Enter the arrivel time of process
process P0 : 5
process P1 : 0
process P2 : 4
process P3 : 3

Enter the Burst time of process


process P0 : 5
process P1 : 6
process P2 : 4
process P3 : 2

Enter the Quantum Time : 2

Proc A_T B_T W_T TA_T


______________________________________
0 5 5 7 12
1 0 6 6 12
2 4 4 6 10
3 3 2 3 5
_______________________________________

Average Waiting time = 5.500000

Average Turn around time = 9.750000

/*==================================================================*/

Вам также может понравиться