Welcome to my blog, enjoy reading.

Saturday, May 22, 2010

Optimal Program using C

#include< stdio.h>
#include< conio.h>
void main()
{
 int page[20],n,f,i,j,a[20],pf=0,k=0,match;
 clrscr();
 printf("enter the length of the reference string");
 scanf("%d",&n);
 printf("enter the frame size");
 scanf("%d",&f);

 printf("enter reference string");
 for(i=0;i< n;i++)
 scanf("%d",&page[i]);
 for(j=0;j< f;j++)
 a[j]=-1;

 printf("\n-1\t-1\t-1\n\n");

 k=0;
 for(i=0;i< n;i++)
 {
  match=0;
  for(j=0;j< f;j++)
  if(a[j]==page[i])
  {
   match=1;
  }
  if(match==1)
  {
   goto x;
  }

  for(j=0;j< f;j++)
  if(a[j]==-1)
  {
   a[j]=page[i];
   pf++;
   goto x;
  }

  a[k]=page[i];
  k=(k+1)%f;
  pf++;
  x:;
  for(j=0;j< f;j++)
  printf("%d \t",a[j]);
  printf("\n\n");
 }
 printf("no of page faults are %d",pf);
 getch();
}


0 comments:

Post a Comment