#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
void Print_block(vector <int> block) {//打印block
for (auto it = block.begin(); it != block.end(); it++) {
cout << *it << " ";
}
}
bool Judge_flag(vector<int> flag, int& index) {//判断flag数组是否一个0,并返回元素为0的下标
int count = 0;
int tmp;
for (auto it = flag.begin(); it != flag.end(); it++) {
if (*it == 0) {
count++;
tmp = distance(flag.begin(), it);
}
}
if (count == 1) {
index = tmp;
return true;
}
else {
return false;
}
}
void Reset(vector<vector<int>>& ref_mod) {//时钟中断重置
for (int i=0; i<ref_mod.size(); i++) {
if (ref_mod[i][0] == 1 && ref_mod[i][1] == 1) {
ref_mod[i][0] = 0;
}
}
}
void NRU(vector<int> process, vector<int> block, int n) {
cout << "NRU算法:" << endl;
cout << "process" << " " << "block" << endl;
int missing_num = 0;
int exchange_num = 0;
// 初始化引用位和修改位
vector<vector<int>> ref_mod(n, vector<int>(2, 0));
int tmp = 0;
for (auto it1 = process.begin(); it1 != process.end(); it1++) {
auto it2 = find(block.begin(), block.end(), *it1);
vector <int> tmp_vector = { 1,1 };
if (it2 == block.end()) { // 进程不在block中
if (block.size() < n) { // block未满
block.insert(block.begin(), *it1);
ref_mod[tmp] = tmp_vector;
tmp++;
}
else { // block已满,需要替换一个页面
for (int i = 0; i < 4; ++i) {
for (int j = 0; j < n; ++j) {
if (ref_mod[j][0] + ref_mod[j][1] == i) { // 找到最低类别的页面
block[j] = *it1; // 替换页面
ref_mod[j][0] = 1; // 重置引用位
ref_mod[j][1] = 1; // 重置修改位
exchange_num++;
goto end;
}
}
}
}
end:missing_num++;
}
else { // 进程在block中
int index = distance(block.begin(), it2);
ref_mod[index][0] = 1; // 设置引用位
ref_mod[index][1] = 0; // 设置修改位
}
//模拟时钟中断
int index = distance(process.begin(), it1);
if (index % 3 == 0) {//模拟时钟中断
Reset(ref_mod);
}
cout << " ";
cout << *it1 << "\t ";
Print_block(block);
cout << endl;
}
cout << "缺页" << missing_num << "次";
cout << " ";
cout << "置换" << exchange_num << "次";
}
void LRU(vector <int> process, vector <int> block, int n) {
cout << "LRU算法:" << endl;
cout << "process" << " " << "block" << endl;
int missing_num = 0;
int exchange_num = 0;
for (auto it1 = process.begin(); it1 != process.end(); it1++) {
if (find(block.begin(), block.end(), *it1) == block.end()) {//进程不在block中
if (block.size() < n) {//block未满
block.insert(block.begin(), *it1);
missing_num++;
}
else {//block已满
block.pop_back();
block.insert(block.begin(), *it1);
missing_num++;
exchange_num++;
}
}
else {//进程在block中
vector <int>::iterator it = find(block.begin(), block.end(), *it1);
int tmp = *it;
block.erase(it);
block.insert(block.begin(), tmp);
}
cout << " ";
cout << *it1 << "\t ";
Print_block(block);
cout << endl;
}
cout << "缺页" << missing_num << "次";
cout << " ";
cout << "置换" << exchange_num << "次";
}
void FIFO(vector <int> process, vector <int> block, int n){
cout << "FIFO算法:" << endl;
cout << "process" << " " << "block" << endl;
int missing_num = 0;
int exchange_num = 0;
for (auto it1 = process.begin(); it1 != process.end(); it1++) {
if (find(block.begin(), block.end(), *it1) == block.end()) {//进程不在block中
if (block.size() < n) {//block未满
block.insert(block.begin(), *it1);
missing_num++;
}
else {//block已满
block.pop_back();
block.insert(block.begin(), *it1);
missing_num++;
exchange_num++;
}
}
cout << " ";
cout << *it1 << "\t ";
Print_block(block);
cout << endl;
}
cout << "缺页" << missing_num << "次";
cout << " ";
cout << "置换" << exchange_num << "次";
}
void OPT(vector <int> process, vector <int> block, int n) {
cout << "OPT算法:" << endl;
cout << "process" << " " << "block"<<endl;
int missing_num = 0;
int exchange_num = 0;
for (auto it1 = process.begin(); it1 != process.end(); it1++) {
if (find(block.begin(), block.end(), *it1) == block.end()) {//进程不在block中
if (block.size() < n) {//block未满
block.push_back(*it1);
missing_num++;
}
else {
vector <int> flag(n, 0);
int index = -1;
for (auto it2 = it1 + 1; it2 != process.end(); it2++) {
if (find(block.begin(), block.end(), *it2) != block.end()) {
int tmp_index = distance(block.begin(), find(block.begin(), block.end(), *it2));//得到此进程在block中的下标
flag[tmp_index] = 1;//变量设为1
}
if (Judge_flag(flag, index)) {//判断flag中是否只有一个0
block[index] = *it1;
missing_num++;
exchange_num++;
break;
}
}
}
}
cout << " ";
cout << *it1 << "\t ";
Print_block(block);
cout << endl;
}
cout << "缺页" << missing_num << "次";
cout << " ";
cout << "置换" << exchange_num << "次";
}
int main(void) {
vector<int> process = { 7,0,1,2,0,3,0,4,2,3,0,3,2,1,2,0,1,7,0,1,5,3,2,2,6,1,4,6,3,7,8,2,3,4,5,6,2,1,4,0,5,6,8 };
int n;
/*cout << "请输入进程序列" << endl;
while (true) {
int m;
cin >> m;
if (m == -1) {
break;
}
else {
process.push_back(m);
}
}*/
vector <int> block;
cout << "请输入物理块数:";
cin >> n;
OPT(process, block, n);
cout << "\n\n";
FIFO(process, block, n);
cout << "\n\n";
LRU(process, block, n);
cout << "\n\n";
NRU(process, block, n);
}
输出示例(OPT算法):