All Programs are Written and Compiled in Dev C++. So, it may generate some error in case of other compilers and may need some modifications in program. Download Dev C++

Tuesday 28 October 2014

LRU Page Replacement Program

LRU Page Replacement Program

#include<iostream>
using namespace std;
int main()
{
int p[50],q[20],b[20],c2[20];
int c=0,k=0,c1,d,f,i,j,n,r,t;
cout<<"Enter no of pages:";
cin>>n;
cout<<"Enter the reference string:";
for(i=0;i<n;i++)
cin>>p[i];
cout<<"Enter no of frames:";
cin>>f;
q[k]=p[k];
cout<<"\n\t"<<q[k]<<endl;
c++;
k++;
for(i=1;i<n;i++)
    {
        c1=0;
        for(j=0;j<f;j++)
        {

          if(p[i]!=q[j])
                c1++;
        }
        if(c1==f)
            {
              c++;
              if(k<f)
              {
                q[k]=p[i];
                k++;
                for(j=0;j<k;j++)
                cout<<"\t"<<q[j];
                cout<<endl;
              }
              else
              {
               for(r=0;r<f;r++)
               {
                c2[r]=0;
                for(j=i-1;j<n;j--)
                 {
                  if(q[r]!=p[j])
                  c2[r]++;
                  else
                     break;
                 }
               }
               for(r=0;r<f;r++)
                b[r]=c2[r];
               for(r=0;r<f;r++)
               {
                 for(j=r;j<f;j++)
                   {
                     if(b[r]<b[j])
                     {
                       t=b[r];
                       b[r]=b[j];
                       b[j]=t;
                     }
                   }
               }
               for(r=0;r<f;r++)
            {
                 if(c2[r]==b[0])
                q[r]=p[i];
                 cout<<"\t"<<q[r];
                }
                cout<<endl;
            }
            }
}
cout<<"\nThe no of page faults is "<<c;
return 0;
}

No comments:

Post a Comment