Optimal page replacement algorithm in c++
#include<bits/stdc++.h>
using namespace std;
int main()
{
int f,p,j=0,x,pf=0,h=0,m,i,index,u,e=0,w,g;
int arr[1000]={0};
vector<int>o;
std::vector<int>::iterator it;
cout<<"Enter no of pages"<<endl;
cin>>p;
cout<<"Enter the reference string"<<endl;
for(i=0;i<p;i++)
{
cin>>x;
o.push_back(x);
}
cout<<"Enter no of frames"<<endl;
cin>>f;
vector<int>a(f,-1);
while(j<p)
{
m=0,e=0,g=0;
if(j<=f-1)
{
x=o.at(j);
a.at(j)=x;
++pf;
}
else
{
if(find(a.begin(), a.end(), o.at(j))!=a.end())
{
++h;
}
else
{
for(i=0;i<f;i++)
{
if(find(o.begin()+j+1,o.end(),a.at(i))!=o.end())
{
it=find(o.begin()+j+1,o.end(),a.at(i));
if(it-o.begin()>m && e==0)
{
m=(it-o.begin());index=i;
}
}
else
{
index=i;
++e;
if(e>=2)
{
for(w=0;w<f;w++)
{
if(arr[a[w]]>=g){g=arr[a[w]];index=w;}
}
break;
}
}
}
x=o.at(j);
arr[a[index]]=0;
a.at(index)=x;
++pf;
}
}
for(i=0;i<f;i++)
{
++arr[a[i]];
cout<<a[i]<<" ";
}
cout<<endl;
++j;
}
cout<<"No of page faults happen "<<pf<<endl;
cout<<"No of page hit happen "<<h;
}
/*output:
Enter no of pages
20
Enter the reference string
1 2 3 4 2 5 3 4 2 6 7 8 7 9 7 8 2 5 4 9
Enter no of frames
3
1 -1 -1
1 2 -1
1 2 3
4 2 3
4 2 3
4 5 3
4 5 3
4 5 3
4 5 2
6 5 2
7 5 2
7 8 2
7 8 2
7 8 9
7 8 9
7 8 9
2 8 9
2 5 9
2 5 4
9 5 4
No of page faults happen 14
No of page hit happen 6
20
Enter the reference string
1 2 3 4 2 5 3 4 2 6 7 8 7 9 7 8 2 5 4 9
Enter no of frames
4
1 -1 -1 -1
1 2 -1 -1
1 2 3 -1
1 2 3 4
1 2 3 4
5 2 3 4
5 2 3 4
5 2 3 4
5 2 3 4
5 2 6 4
5 2 7 4
5 2 7 8
5 2 7 8
9 2 7 8
9 2 7 8
9 2 7 8
9 2 7 8
9 5 7 8
9 5 4 8
9 5 4 8
No of page faults happen 11
No of page hit happen 9*/
using namespace std;
int main()
{
int f,p,j=0,x,pf=0,h=0,m,i,index,u,e=0,w,g;
int arr[1000]={0};
vector<int>o;
std::vector<int>::iterator it;
cout<<"Enter no of pages"<<endl;
cin>>p;
cout<<"Enter the reference string"<<endl;
for(i=0;i<p;i++)
{
cin>>x;
o.push_back(x);
}
cout<<"Enter no of frames"<<endl;
cin>>f;
vector<int>a(f,-1);
while(j<p)
{
m=0,e=0,g=0;
if(j<=f-1)
{
x=o.at(j);
a.at(j)=x;
++pf;
}
else
{
if(find(a.begin(), a.end(), o.at(j))!=a.end())
{
++h;
}
else
{
for(i=0;i<f;i++)
{
if(find(o.begin()+j+1,o.end(),a.at(i))!=o.end())
{
it=find(o.begin()+j+1,o.end(),a.at(i));
if(it-o.begin()>m && e==0)
{
m=(it-o.begin());index=i;
}
}
else
{
index=i;
++e;
if(e>=2)
{
for(w=0;w<f;w++)
{
if(arr[a[w]]>=g){g=arr[a[w]];index=w;}
}
break;
}
}
}
x=o.at(j);
arr[a[index]]=0;
a.at(index)=x;
++pf;
}
}
for(i=0;i<f;i++)
{
++arr[a[i]];
cout<<a[i]<<" ";
}
cout<<endl;
++j;
}
cout<<"No of page faults happen "<<pf<<endl;
cout<<"No of page hit happen "<<h;
}
/*output:
Enter no of pages
20
Enter the reference string
1 2 3 4 2 5 3 4 2 6 7 8 7 9 7 8 2 5 4 9
Enter no of frames
3
1 -1 -1
1 2 -1
1 2 3
4 2 3
4 2 3
4 5 3
4 5 3
4 5 3
4 5 2
6 5 2
7 5 2
7 8 2
7 8 2
7 8 9
7 8 9
7 8 9
2 8 9
2 5 9
2 5 4
9 5 4
No of page faults happen 14
No of page hit happen 6
20
Enter the reference string
1 2 3 4 2 5 3 4 2 6 7 8 7 9 7 8 2 5 4 9
Enter no of frames
4
1 -1 -1 -1
1 2 -1 -1
1 2 3 -1
1 2 3 4
1 2 3 4
5 2 3 4
5 2 3 4
5 2 3 4
5 2 3 4
5 2 6 4
5 2 7 4
5 2 7 8
5 2 7 8
9 2 7 8
9 2 7 8
9 2 7 8
9 2 7 8
9 5 7 8
9 5 4 8
9 5 4 8
No of page faults happen 11
No of page hit happen 9*/
Comments
Post a Comment