先进先出页面置换算法
【问题描述】编程模拟先进先出页面置换算法。
【输入形式】
【输出形式】
【样例输入】输入用文件形式,文件名为test.txt
3(事先分配的物理块数)
7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0 1 7 0 1(页面引用串)
【样例输出】
missing 7,no page replacement is required, the physicalblock is 7
missing 0,no page replacement is required, the physicalblock is 7 0
missing 1,no page replacement is required, the physicalblock is 7 0 1
missing 2, replacement page 7, the physicalblock is 2 0 1
no missing 0, in physicalblock: 2 0 1
missing 3, replacement page 0, the physicalblock is 2 3 1
missing 0, replacement page 1, the physicalblock is 2 3 0
missing 4, replacement page 2, the physicalblock is 4 3 0
missing 2, replacement page 3, the physicalblock is 4 2 0
missing 3, replacement page 0, the physicalblock is 4 2 3
missing 0, replacement page 4, the physicalblock is 0 2 3
no missing 3, in physicalblock: 0 2 3
no missing 2, in physicalblock: 0 2 3
missing 1, replacement page 2, the physicalblock is 0 1 3
missing 2, replacement page 3, the physicalblock is 0 1 2
no missing 0, in physicalblock: 0 1 2
no missing 1, in physicalblock: 0 1 2
missing 7, replacement page 0, the physicalblock is 7 1 2
missing 0, replacement page 1, the physicalblock is 7 0 2
missing 1, replacement page 2, the physicalblock is 7 0 1
number of missing pages is 15
page miss rate is 75.00%
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
int n;//3
int m;//12
int s[20];
int a[20];
int temp[20];
int sum=0;
bool hit(int q)
{
for(int j=0;j<n;j++)
{
if(s[q]==a[j])
{
return true;
}
}
return false;
}
void nohit(int q)
{
int max=0;
for(int k=1;k<n;k++)
{
if(temp[k]>temp[max])
{
max=k;
}
}
cout<<"missing "<<s[q]<<", replacement page "<<a[max]<<", the physicalblock is ";
a[max]=s[q];
temp[max]=0;
for(int j=0;j<n;j++)
{
cout<<a[j]<<" ";
temp[j]++;
}
cout<<endl;
sum++;
}
void search1(int q)
{
if(hit(q))
{
cout<<"no missing "<<s[q]<<", in physicalblock: ";
for(int j=0;j<n;j++)
{
cout<<a[j]<<" ";
}
cout<<endl;
}
else
{
nohit(q);
}
}
int main()
{
freopen("test.txt", "rt", stdin);
cin>>n;
int i=0;
while(cin>>s[i])
{
if(i<n)
{
a[i]=s[i];
cout<<"missing "<<s[i]<<",no page replacement is required, the physicalblock is ";
for(int j=0;j<=i;j++)
{
cout<<s[j]<<" ";
}
cout<<endl;
}
i++;
m=i;
}
for(int j=0;j<n;j++)
{temp[j]=n-j;}
sum=n;
for(int q=n;q<m;q++)
{
search1(q);
}
cout<<"number of missing pages is "<<sum<<endl;
double result = sum/(m*1.0)*100;
cout<<"page miss rate is "<<fixed<<setprecision(2)<<result<<"%"<<endl;
return 0;
}