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++

Friday, 12 December 2014

Least Recently Used Page Replacement Program

AIM: To Implement LRU Page Replacement Program

#include<iostream>
using namespace std;
int main()
{
int p[50],fr[10],used[10];
int i,j,k,n,fs,flag=0,pf=0,index;
cout<<"Enter no of Refrence Strings: ";
cin>>n;
cout<<"Enter Refrence Strings: ";
for(i=0;i<n;i++)
cin>>p[i];
cout<<"Enter Frame Size: ";
cin>>fs;

for(i=0;i<fs;i++)
fr[i]=-1;
for(i=0;i<n;i++)
{
cout<<p[i]<<"\t";
flag=0;
for(j=0;j<fs;j++)
{
if(p[i]==fr[j])
{
cout<<"\t ";
flag=1;
break;
}
}
if(flag==0)
{
for(j=0;j<fs;j++)
{
used[j]=0;
}
for(j=0,k=i-1;j<fs-1;j++,k--)
{
for(int l=0;l<fs;l++)
{
if(fr[l]==p[k])
used[l]=1;
}
}
for(j=0;j<fs;j++)
{
if(used[j]==0)
{
index=j;
}
}
fr[index]=p[i];
pf++;
cout<<"Fault\t:";
for(j=0;j<fs;j++)
cout<<fr[j]<<"\t";
cout<<endl;
}
else
{
cout<<endl;
}

}
cout<<"\nTotal No of Page Fault="<<pf;
return 0;
}

No comments:

Post a Comment