模拟请求分页管理中地址转换和缺页中断处理C++实现

模拟请求分页管理中地址转换和缺页中断处理C++实现_第1张图片

#include
#include
using namespace std;
#define SIZE 1024

class PAGE {
public:
	int id;
	bool local;//内存false 外存true
	int nId;//主存块号
	bool flag;//更改true 未更改false
	int wId;//磁盘块号
	PAGE(int _id, bool _local, int _nId, bool _flag, int _wId) {
		id = _id;
		local = _local;
		nId = _nId;
		flag = _flag;
		wId = _wId;
	}
};

int main() {
	vector<PAGE*> q(3);
	vector<PAGE*> s(8);
	s[0] = new PAGE(0, 0, 0, 0, 0);
	s[1] = new PAGE(1, 0, 1, 0, 0);
	s[2] = new PAGE(2, 0, 2, 0, 0);
	s[3] = new PAGE(3, 1, 0, 0, 0);
	s[4] = new PAGE(4, 1, 0, 0, 4);
	s[5] = new PAGE(5, 1, 0, 0, 5);
	s[6] = new PAGE(6, 1, 0, 0, 6);
	s[7] = new PAGE(7, 1, 0, 0, 7);
	q[0] = s[0];
	q[1] = s[1];
	q[2] = s[2];
	while (1) {
		int logicLocal, page, pageLocal;
		bool flag = false;
		cout << "输入逻辑地址:";
		cin >> logicLocal;
		if (logicLocal <= 0 || logicLocal >= 7 * 1024) {
			cout << "输入错误";
			return 0;
		}
		page = logicLocal / SIZE;
		pageLocal = logicLocal % SIZE;
		A:for (int i = 0; i < 3; i++) {
			if (!q[i]->local && q[i]->id == page) {
				cout << "page:" << i << "\tpageLocal:" << pageLocal << endl;
				flag = true;
				break;
			}
		}
		if (!flag) {
			for (int i = 0; i < 8; i++) {
				if (s[i]->local && s[i]->id == page) {
					q.front()->local = true;
					q.erase(q.begin());
					s[i]->local = false;
					q.push_back(s[i]);
					break;
				}
			}
			goto A;
		}
	}
	return 0;
}

你可能感兴趣的:(c++)