Thursday, May 10, 2018

FIFO PAGE REPLACEMENT ALGORITHM IN C++

(This program is compile and run using Turbo C++ )

#include<iostream.h>
#include<conio.h>
class Fifo
{
 public:
 int ref_st[50],temp[50],size,frame_no,total_fault;
 int count,p,m;                                       
 int percentage;
 int i,j;
  Fifo(int o)
  {
   count=o;
   p=o;
   m=o;
  }
 void input()
 {
  cout<<"Enter the size of reference string: "<<endl;
  cin>>size;
  cout<<"Enter the elements: "<<endl;
  for(i=0;i<size;i++)
   cin>>ref_st[i];
  cout<<"Enter the total number of frame "<<endl;
  cin>>frame_no;
 }
 void calculate()
 {
  for(i=0;i<frame_no;i++)
   temp[i]=ref_st[i];
  for(i=frame_no;i<size;i++)
  {
   for(j=0;j<frame_no;j++)
   {
    if(ref_st[i]==temp[j])
     p++;
   }
   if(p==0)
   {
     if(m>(frame_no-1))
       m=0;
    temp[m]=ref_st[i];
    m++;
    count++;
   }
   else
    p=0;
  }
  total_fault=count+frame_no;
 }
  void show()
  {
   cout<<"Last frame :"<<endl;
   for(i=0;i<frame_no;i++)
    cout<<temp[i]<<endl;
    cout<<"Number of page fault : "<<endl;
    cout<<total_fault<<endl;
    percentage=((total_fault*100)/size);
   cout<<"Page fault rate: "<<endl;
   cout<<percentage<<"%";
  }
};
void main()
{
 Fifo ob(0);
 clrscr();
 ob.input();
 ob.calculate();
 ob.show();
 getch();
}



No comments:

Post a Comment