Search Tutorials

Saturday, 1 June 2013

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("\n\nP  A  B  W  TAT\n\n");
  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\n\n",p[i],a[i],b[i],w[i],ta[i]);
 }
printf("\n\nAverage waiting time is:");
 sum1=0.0;
  for(i=0;i<n;i++)
   {
    sum1+=w[i];
   }
 aw=sum1/n;
 printf("%f",aw);

 printf("\n\nAverage turn around time is:");
 sum1=0.0;
  for(i=0;i<n;i++)
   {
    sum1+=ta[i];
   }
 at=sum1/n;
 printf("%f",at);
printf("\n\nThe Grantt chart is:\n\n");
 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)


2 comments:



  1. FCFS scheduling algorithm describe very easily, to know details visit this link.

    http://www.secufoon.com/describe-first-come-first-serve-scheduling-algorithmfcfs-in-c-language-concept/

    ReplyDelete
  2. Sir what is the mean of if(t1-a[i]<0)

    ReplyDelete

Back to Top