Search Tutorials

Saturday 1 June 2013

C code for Round Robin Scheduling

Round Robin: 
A straightforward way to reduce the penalty that short jobs suffer with FCFS is to use preemption based on a clock. The simplest such policy is round robin. A clock interrupt is generated at periodic intervals. When the interrupt occurs, the currently running process is placed in the ready queue, and the next ready job is selected on a FCFS basis. This technique is also known as time slicing, because each process is given a slice of time before being preempted.

/* C program to implement Round Robin Scheduling */

#include <stdio.h>
#include <conio.h>

int initialise();
void sort();
void print();
void GC();
void WT(int,int,char);
int RR();
float averagewaitingtime();
float averageturnaroundtime();

int p[15],a[15],b[15],br[15],gc[30],n,time,k;
static int wt[15];
void main()
  {
    int k;
    clrscr();
    printf("Enter the time slice:");
     fflush(stdin);
    scanf("%d",&time);
     n=initialise();
     sort();
     k=RR();
     print();
     printf("\n\nAverage Wating time = %.2fmsec. & Average Turnaround time= %.2fmsec.\n\n",averagewaitingtime()/n,averageturnaroundtime()/n);
     GC(k);
     getch();
  }

int RR()
  {
   int i,flag,left,k=0;
    do
      {
    flag=1;
    for(i=0;i<n;i++)
     {
       if ( br[i]==0  || (a[i] > (k*time)))
         continue;
       if(br[i] <= time)
        {
          left=br[i];
          gc[k++]=p[i];
          br[i]=0;
          WT(i,left,'l');
        }
       else
        {
          gc[k++]=p[i];
          br[i]-=time;
          WT(i,time,'g');
        }
     }
      for(i=0;i<n;i++)
    if(br[i]!=0)
      flag=0;
      }while(!flag);
   return k;
  }


 void WT(int i,int add,char ch)
  {
    int j;
    if ( ch == 'l' )
     {
       for(j=0;j<n;j++)
       if( j!=i && br[j]!=0 )
          wt[j]+=add;
     }
    if ( ch == 'g' )
     {
       for(j=0;j<n;j++)
    if ( j!=i && br[j]!=0 )
     wt[j]+=add;
     }
  }



 void print()
    {
      int i;
       printf("\n\n    | Process | Arrival time | Burst_Time | Waiting_Time | T_arnd_Time |\n");
       printf("    -------------------------------------------------------------------\n");
    for(i=0;i<n;i++)
     printf("    |%4d     |%8d      |%6d      |%8d      |%6d       |\n\n",p[i],a[i],b[i],wt[i],b[i]+wt[i]);
       printf("    --------------------------------------------------------------------\n");
    }

  void GC(int g)
   {
     int i;
     printf("\n\n\nAND THE G.C IS :::\n\n");
      for(i=0;i<g;i++)
       printf("------");
      printf("\n");
       for(i=0;i<g;i++)
          {
    printf("|  %d  ",gc[i]);
     if(i==g-1)
      printf("|");
    }
     printf("\n");
     for(i=0;i<g;i++)
      printf("------");
      printf("\n");
   }

void sort()
 {
   int i,n,t,j;
   for(i=0;i<n;i++)
    {
    for(j=i+1;j<n;j++)
     {
      if(a[i] > a[j])
       {
     t=p[i];
     p[i]=p[j];
     p[j]=t;

     t=a[i];
     a[i]=a[j];
     a[j]=t;

     t=b[i];
     b[i]=b[j];
     b[j]=t;

     t=br[i];
     br[i]=br[j];
     br[j]=t;
       }
    }
  }
}

 int initialise()
  {
    FILE *fp;
    int i=0,n1,n2,n3;
    fp=fopen("sjf.txt","r");
    while(fscanf(fp,"%d%d%d",&n1,&n2,&n3)!=EOF)
      {
     p[i]=n1;
     a[i]=n2;
     br[i]=b[i++]=n3;
      }
    fclose(fp);
    return i;
  }
 float averagewaitingtime()
  {
    float sum=0;
    int i;
     for(i=0;i<n;i++)
    sum+=wt[i];
      return sum;
  }

  float averageturnaroundtime()
  {
    float sum=0;
    int i;
     for(i=0;i<n;i++)
    sum+=(wt[i]+b[i]);
      return sum;
  }


Input File:

1 0 3
2 1 3
3 2 2

//Output Of the above program:-

C code for Round Robin Scheduling

C code for Round Robin Scheduling


2 comments:

Back to Top