实施第二次机会需求页面替换算法
Implementing second chance demand page replacement algorithm
我遇到的问题总是会错过,而不是替换一个元素,而是用它拉入的整数填充了每个帧。我很难弄清楚哪里从这里去。" requests.txt"是一个随机整数的文件,示例输出应该看起来像:
2: page hit, page 2 is in frame 8
45: page hit, page 45 is in frame 2
但我的看起来像:
99: page miss, page 99replaces page 0in frame 0
99: page miss, page 99replaces page 0in frame 1
99: page miss, page 99replaces page 0in frame 2
99: page miss, page 99replaces page 0in frame 3
99: page miss, page 99replaces page 0in frame 4
99: page miss, page 99replaces page 0in frame 5
99: page miss, page 99replaces page 0in frame 6
99: page miss, page 99replaces page 0in frame 7
99: page miss, page 99replaces page 0in frame 8
99: page miss, page 99replaces page 0in frame -1
4: page miss, page 4replaces page 0in frame 0
4: page miss, page 4replaces page 0in frame 1
4: page miss, page 4replaces page 0in frame 2
4: page miss, page 4replaces page 0in frame 3
4: page miss, page 4replaces page 0in frame 4
4: page miss, page 4replaces page 0in frame 5
4: page miss, page 4replaces page 0in frame 6
4: page miss, page 4replaces page 0in frame 7
4: page miss, page 4replaces page 0in frame 8
代码:
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
void search(int input, int P_list[], bool R_bit[], int & victim);
int main()
{
//Basic initilization
int victim = 0;
bool R_bit [10];
int P_list [10];
for(int i = 0; i <10; i++)
{
//Sets one array to all false
R_bit [i] = false;
//Sets every integer in the other array to -1
P_list [i] = -1;
}
int input;
ifstream inFile;
//Opens up the text file "requests.txt"
inFile.open("requests.txt");
//If It is not able to open the file
if (!inFile)
{
//Display error message
cout << "Unable to open specified file";
//Exit with an error(1)
exit(1);
}
while(inFile >> input )
{
if(input < 0)
{
cout << "invalid input ";
exit(1);
}
else if(input > 99)
{
cout << "invalid input ";
exit(1);
}
//search array 2, replace and end
search(input, P_list, R_bit, victim);
} return 0;
}
void search(int input, int P_list[], bool R_bit[], int &victim)
{
//local var only unless you pass them input is the only thing you passed
for(int i = 0; i <10; i++)
{
if(input == P_list [i])
{//Output if it is a hit
cout << input << ": page hit, " << input << "is in frame " << i << endl;
R_bit [i] = 1;
return;
}
int oldpage;
//Sets up a bool to end the while loop
bool flag = true;
//While true
while(flag){
//looks for the first 0 in the R-Bit array
if (R_bit [victim] == 0)
{
//If it is zero
flag = false;
//Recording previous page for output
oldpage = R_bit [victim];
//Replaces with input
R_bit [victim] = input;
victim = victim + 1;
}
else
{ //Makes the R-Bit zero
R_bit [victim] = 0;
//Increments, makes it zero
++victim;
}
//If the victim is going to go out of bounds
if(victim > 9)
{
//Set to zero
victim = 0;
}
}
//output if it misses and the frame it is in
cout << input << ": page miss, page " << input << " replaces page " << oldpage << " in frame " << (victim - 1) << endl;
}
}
您将r_bit(最近的标志)与p_list(保存数据)
混合 //Recording previous page for output
oldpage = R_bit [victim];
//Replaces with input
R_bit [victim] = input;
应该是
//Recording previous page for output
oldpage = P_list [victim];
//Replaces with input
P_list [victim] = input;
我认为,在您的搜索功能中,您永远不会设置p_list [someIndex] value。
我相信,这就是正在发生的事情:
- 您致电搜索第一个号码
- p_list将没有它,因为您将所有内容都初始化为-1
- 因此,您决定将更换一些东西,但您永远不会将当前整数添加到p_list
希望您可以从这里进行。
相关文章:
- 模板参数替换失败,并且未完成隐式转换
- 为什么这个运算符<重载函数对 STL 算法不可见?
- 基于ELO的团队匹配算法
- C++选择排序算法中的逻辑错误
- 如何用转义符替换字符串中的所有特殊字符
- 有没有办法将谓词中的元素偏移量传递给 std 算法?
- C++A*算法并不总是在路径中具有目标节点
- 使用 Rcpp 加速替换迭代算法中的列表和向量元素是否合法?
- 最佳页面替换算法
- 实施第二次机会需求页面替换算法
- 正在寻找元组替换算法
- 如何使用std算法来替换for循环
- 如何替换我的"for"循环以通过 STL mimax 算法找到最小值/最大值
- 与排序相关的算法(将每个项目替换为排序排序中的索引)
- 时钟分页替换算法
- 在std::list中合并(将两个项目融合在一起,用融合替换)项目的算法(即破坏性聚类)
- 用STL算法替换for循环,同时允许并行性
- 将算法c++的无效操作数替换为二进制表达式
- 快速数组比较和替换最接近值的元素的算法.(跟踪点)
- 最近未使用 (NUR) 页面替换算法