ROUND ROBIN

#include<stdio.h>
int main()
{
   int n,at[100],bt[100];
   int i,t,total=0,count=0,x;
   int wt[100]={0},tat[100]={0};
   float awt=0,atat=0;

   puts("Enter number of processes");
   scanf("%d",&n);

   puts("Enter arrival time and burst time rescpectively");
   for(i=0;i<n;i++)
   scanf("%d%d",&at[i],&bt[i]);

   puts("Enter the time quantum");
   scanf("%d",&t);

   for(i=0;i<n;i++)
   total+=bt[i];

   for(i=0;i<n;i++)
   {
      int pos=i,j;
      for(j=i+1;j<n;j++)
      {
         if(at[pos]>at[j])
         pos=j;
      }
      at[pos]=at[pos]+at[i]-(at[i]=at[pos]);
      bt[pos]=bt[pos]+bt[i]-(bt[i]=bt[pos]);
   }

   puts("PROCESSES");
   for(i=0;i<n;i++)
   printf("\t%d\t%d\n",at[i],bt[i]);

   i=0;
   while(count<total)
   {
      if(bt[i]!=0)
      {
         wt[i]=count+wt[i]-tat[i];
         if(bt[i]<t)
         {
            x=bt[i];
            bt[i]=0;
            count+=x;
         }
         else
         {
            bt[i]=bt[i]-t;
            count+=t;
         }
         tat[i]=count;
      }
      if(i==n-1) i=0;
      else i++;
   }

   for(i=0;i<n;i++)
   {
      wt[i]=wt[i]-at[i];
      awt=awt+wt[i];
      atat=atat+tat[i];
   }
   awt=awt/n;
   atat=atat/n;
   printf("Average waiting time = %f\n",awt);
   printf("Average turnaround time = %f\n",atat);
   return 0;
}

Comments