Search Tutorials

Saturday, 1 June 2013

C code for First in First out Algorithm

First in First out (FIFO) Algorithm:
pages are removed in the same order they are entered.
/* C program to implement First in First out Algorithm */

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

void main()
 {
  int page[20],frame[10],i=0,j,k=0,l=0,n,num,a,found,pf=0;
  char c,ch;
  FILE *fp;
  clrscr();
  fp=fopen("fifo.txt","r");
   while(fscanf(fp,"%d%c",&a,&c)!=EOF)
      page[i++]=a;
      n=i;

while(1)
 {
   i=0,k=0,found=0;
   printf("\nEnter the frame size:");
       scanf("%d",&num);
   for(i=0;i<num;i++)
       frame[i]=-1;

   for(i=0;i<n;i++)
     {
       found=0;
      for(j=0;j<num;j++)
        if(page[i]==frame[j])
      found=1;
        if(found==0)
      {
         if(k==num)
           k=0;
         frame[k++]=page[i];
                    pf++;
      }
    for(l=0;l<num;l++)
      {
       if(frame[l]==-1)
         printf("|  ");
      else
         printf("|%d ",frame[l]);
      }
    if(found==0)
       printf("Page Fault\n");
    else
        printf("No page fault\n");
      printf("\n\n");
  }
printf("\Total page faults=%d",pf);
pf=0;
printf("\nPress Y to continue:");
    scanf("%c",&ch);
    
if(ch!='y')
  break;
}
 getch();
}


Input File:

7,7,4,4,2,1,4,1,5,3,2,1,4,7,6,1,9,8,0,1

//Output Of the above program:-

C code for First in First out Algorithm


1 comment:

Back to Top