Monday, March 31, 2014

C code for First Come First Serve Scheduling FCFS

,
First-Come-First-Served:
The simplest scheduling policy is first-come-first-served (FCFS), also known as first-in-first-out (FIFO) or a strict queuing scheme. As each process becomes ready, it joins the ready queue. When the currently running process ceases to execute, the process that has been in the ready queue the longest is selected for running. FCFS performs much better for long processes than short ones.

C program:

#include<stdio.h>
#include<conio.h>
int initialize();
int p[10],a[10],b[10],w[10],ta[10],t1;
void main()
 {
  int i,j,n,t,sum=0;
  float aw,at,sum1;
  clrscr();
  printf("

P  A  B  W  TAT

");
  n=initialize();
   for(i=0;i<n;i++)
    {
     for(j=i+1;j<n;j++)
      {
       if(a[i]>a[j])
    {
     t=a[i];
     a[i]=a[j];
     a[j]=t;

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

     t=p[i];
     p[i]=p[j];
     p[j]=t;
    }
      }
    }
t1=a[0];
 for(i=0;i<n;i++)
  {
  if (t1-a[i]<0)
    t1+=a[i]-t1;
   if(i==0)
    w[i]=0;
   else
    w[i]=t1-a[i];
   t1+=b[i];
  }
 for(i=0;i<n;i++)
  {
   ta[i]=b[i]+w[i];
  }
 for(i=0;i<n;i++)
 {
    printf("%d  %d  %d  %d  %d

",p[i],a[i],b[i],w[i],ta[i]);
 }
printf("

Average waiting time is:");
 sum1=0.0;
  for(i=0;i<n;i++)
   {
    sum1+=w[i];
   }
 aw=sum1/n;
 printf("%f",aw);

 printf("

Average turn around time is:");
 sum1=0.0;
  for(i=0;i<n;i++)
   {
    sum1+=ta[i];
   }
 at=sum1/n;
 printf("%f",at);
printf("

The Grantt chart is:

");
 for(i=0;i<n;i++)
  {
   printf("|%d",p[i]);
    if(i==n-1)
     printf("|");
  }

 getch();
}

int initialize()
{
  int n,n1,n2,n3,i=0;
  char ch=a;
  FILE *fp;
  fp=fopen("sjf.txt","r");
  while(fscanf(fp,"%d%d%d",&n1,&n2,&n3)!=EOF)
    {
     
       p[i]=n1;
     
       a[i]=n2;
     
       b[i]=n3;
        i++;
    }
  return i;
}


Input File:

1 0 3
2 1 3
3 2 2

//Output Of the above program:-

C code for First Come First Serve Scheduling(FCFS)


0 comments to “C code for First Come First Serve Scheduling FCFS”

Post a Comment

 

Download Android Game Copyright © 2016 -- Powered by Blogger