SCHEDULING ALGORITHMS
Round Robin
Programs
// 4. Round Robin scheduling
#include<stdio.h>
#include<stdlib.h>
typedef struct process
{
int pid, ft, wt, bt;
}pr;
int n;
pr p[10];
void rrs(int);
int main()
{
int i, tq;
printf(“\nHow many processes : “);
scanf(“%d”,&n);
printf(“\tEnter the values for Burst time\n”);
for(i=0;i<n;i++)
{
printf(“\tEnter for Process %d : “,i);
scanf(“%d”,&p[i].bt);
p[i].pid=i;
}
printf(“\n\tEnter the Time Quantum : “);
scanf(“%d”,&tq);
rrs(tq);
}
void rrs(int TQ)
{
int i,j;
float avgwt, avgtat, sum_wt=0, sum_tat=0;
int AP[25], AT[25];
int fin=0,count=0,time=0;
int fintime=0;
AT[0]=0;
while(fin<n)
{
for(i=0;i<n;i++)
{
if(p[i].bt>0)
{
time = (p[i].bt<=TQ)?p[i].bt:TQ;
if(p[i].bt<=TQ)
p[i].ft = fintime + time;
p[i].bt=p[i].bt-time;
fintime+=time;
AP[count] = p[i].pid;
AT[++count] = AT[count-1]+time;
for(j=0;j<n;j++)
if( (n-fin)!=1 && i!=j && p[j].bt>0 )
p[j].wt+=time;
if(p[i].bt==0)
fin++;
}
}
}
for(i=0;i<n;i++)
{
sum_wt=sum_wt+p[i].wt;
sum_tat=sum_tat+p[i].ft;
}
avgwt=sum_wt/n;
avgtat = sum_tat/n;
printf(“\nAverage Waiting Time : %5.2f ms \n\n”, avgwt);
printf(“\nAverage Turnarround Time : %5.2f ms \n\n”, avgtat);
printf(“\n\nGANTT CHART\n\n\t”);
for(i=0;i<count;i++)
printf(“P%d\t”,AP[i]);
printf(“\n”);
for(i=0;i<=count;i++)
printf(” %d\t”,AT[i]);
printf(“\n”);
}