Google Search

Thursday, October 6, 2011

write a C program to to implement the FCFS algorithm


/* A program to implement the FCFS algorithm*/
#include<conio.h>
main()
{
          int n,i,j,sum=0;
          int arrival[10],service[10],start[10];
          int finish[10],wait[10],turn[10];
          float avgturn=0.0,avgwait=0.0;
          start[0]=0;
          clrscr();
          printf("Enter the number of processes:");
          scanf("%d",&n);
          for(i=0;i<n;i++)
          {
                   printf("Enter the arrival and service time of %d process:",i+1);
                   scanf("%d%d",&arrival[i],&service[i]);
          }
          for(i=0;i<n;i++)
          {
                   sum=0;
                   for(j=0;j<i;j++)
                   sum=sum+service[j];
                   start[i]=sum;
          }
          for(i=0;i<n;i++)
          {
                   finish[i]=service[i]+start[i];
                   wait[i]=start[i];
                   turn[i]=service[i]+wait[i];
          }
          for(i=0;i<n;i++)
          {
                   avgwait+=wait[i];
                   avgturn+=turn[i];
          }
          avgwait/=n;
          avgturn/=n;
          printf("\nArrival Service Start Finish Wait Turn\n");
          for(i=0;i<n;i++)
          printf("%d\t%d\t%d\t%d\t%d\t%d\n",arrival[i],service[i],start[i],
finish[i],wait[i],turn[i]);
          printf("\nAverage waiting time=%f",avgwait);
          printf("\nAverage turn around time=%f",avgturn);
          getch();
}

0 comments:

Post a Comment