OPT、FIFO、NRU、LRU页面置换算法(重庆邮电大学操作系统作业)

#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算法):

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值