操作系统四种页面置换算法(OPT、FIFO、LRU、CLOCK)的C++实现

本文深入探讨了四种主要的页面置换算法:最佳置换算法(OPT)、最近最少使用算法(LRU)、先进先出算法(FIFO)及Clock算法,通过代码实现展示了每种算法的工作原理及其在不同场景下的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 算法原理?不要想太多了,马上复制我的代码运行一下

#include "stdafx.h"
#include <iostream>
#include<map>
#include<set>
#include <algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#define N 200
using namespace std;

int page[N];//页面引用号
int block[N];//物理块,内存
int dist[N][N];//表示第i次访问内存的时候,内存中的页面j 在以后被访问的最小时间

int n;//页面引用号个数
int m;//物理块数目
int page_max;//最大页面号

int pre[N];//page[i]在page中的索引
int opt() {//最佳页面置换算法
	int page_lack = 0;
	memset(pre, 0, sizeof(pre));
	memset(dist, 0x3f, sizeof(dist));
	memset(block, -1, sizeof(block));
	for (int i = n; i >= 1; --i) {
		for (int j = 0; j <= page_max; ++j)
			if (pre[j])
				dist[i][j] = pre[j] - i;
		pre[page[i]] = i;
	}

	for (int i = 1; i <= n; ++i) {//开始访问页面,初始是内存中没有分页
		int j;
		int max_dist = 0, p;
		for (j = 1; j <= m; ++j) {
			if (block[j] == -1) {//改块没有放入页面,则直接放入, 产生缺页
				block[j] = page[i];
				cout << "页面" << page[i] << "不在内存,直接放入物理块" << j << "中!" << endl;
				page_lack++;
				break;
			}
			else if (block[j] == page[i])//页面存在内存中
				break;

			if (max_dist < dist[i][block[j]]) {
				max_dist = dist[i][block[j]];//说明block[j] 对应的页面以后会长时间不会用到
				p = j;//block[] 第j个页面会被替换掉
			}
		}
		if (j > m) {//此时内存中不能在放入新的分页,而且没有找到page[i]对应的分页,接下来进行页面替换
			cout << "页面" << page[i] << "不在内存,将物理块" << p << "中的页面" << block[p] << "替换掉!" << endl;
			block[p] = page[i];
			page_lack++;
		}
		cout << endl << "当前内存中页面的情况:" << endl;
		for (int k = 1; k <= m; ++k)//内存中页面加载情况
			cout << block[k] << " ";
		cout << endl << endl;
	}
	cout << "OPT进行了" << page_lack - m << "次页面置换" << endl;
	return page_lack;//返回缺页次数
}

int lru() {//最近最久未使用算法和opt算法差不多,只不过是lru是向前看, opt是向后看
	int page_lack = 0;
	memset(pre, 0, sizeof(pre));
	memset(dist, 0x3f, sizeof(dist));
	memset(block, -1, sizeof(block));
	for (int i = 1; i <= n; ++i) {
		for (int j = 0; j <= page_max; ++j)
			if (pre[j])
				dist[i][j] = i - pre[j];
		pre[page[i]] = i;
	}

	for (int i = 1; i <= n; ++i) {//开始访问页面,初始是内存中没有分页
		int j;
		int max_dist = 0, p;
		for (j = 1; j <= m; ++j) {
			if (block[j] == -1) {//改块没有放入页面,则直接放入, 产生缺页
				block[j] = page[i];
				cout << "页面" << page[i] << "不在内存,直接放入物理块" << j << "中!" << endl;
				page_lack++;
				break;
			}
			else if (block[j] == page[i])//页面存在内存中
				break;

			if (max_dist < dist[i][block[j]]) {
				max_dist = dist[i][block[j]];//说明block[j] 对应的页面以后会长时间不会用到
				p = j;//block[] 第j个页面会被替换掉
			}
		}
		if (j > m) {//此时内存中不能在放入新的分页,而且没有找到page[i]对应的分页,接下来进行页面替换
			cout << "页面" << page[i] << "不在内存,将物理块" << p << "中的页面" << block[p] << "替换掉!" << endl;
			block[p] = page[i];
			page_lack++;
		}
		cout << endl << "当前内存中页面的情况:" << endl;
		for (int k = 1; k <= m; ++k)//内存中页面加载情况
			cout << block[k] << " ";
		cout << endl << endl;
	}
	cout << "LRU进行了" << page_lack - m << "次页面置换" << endl;
	return page_lack;//返回缺页次数
}

set<int>page_set;
int fifo() {//先进先出页面置换算法
	int page_lack = 0;
	memset(block, -1, sizeof(block));
	int index = 1;
	for (int i = 1; i <= n; ++i) {
		if (index > m) index = 1;
		set<int>::iterator it;
		it = page_set.find(page[i]);
		if (it == page_set.end()) {
			if (block[index] != -1)
				page_set.erase(block[index]);
			page_set.insert(page[i]);
			block[index++] = page[i];
			++page_lack;
		}
		for (int k = 1; k <= m; ++k)
			cout << block[k] << " ";
		cout << endl;
	}
	cout << "FIFO进行了" << page_lack - m << "次页面置换" << endl;
	return page_lack;
}

int nru[N];//表示 物理块 i 最近时候被访问过
int page_in_block[N];//页面 i 在 block的下标索引
int clock() {
	int index = 1;
	int page_lack = 0;
	memset(block, -1, sizeof(block));
	for (int i = 1; i <= n; ++i) {
		if (page_in_block[page[i]]) {//如果page[i]已经在内存中
			nru[page_in_block[page[i]]] = 1;//重新标记这个物理块中的页面被访问过了
			cout << endl << "第" << i << "次: 页面" << page[i] << "已经存在物理块" << page_in_block[page[i]] << "中!" << endl;
		}
		else {
			while (true) {
				if (index > m) index = 1;
				if (block[index] == -1) {
					nru[index] = 1;
					page_in_block[page[i]] = index;
					block[index++] = page[i];
					++page_lack;
					break;
				}
				if (block[index] == page[i]) {
					nru[index++] = 1;
					break;
				}
				else {
					if (nru[index] == 0) {//替换该页面
						nru[index] = 1;
						page_in_block[block[index]] = 0;
						cout << endl << "第" << i << "次: 物理块" << index << "中的页面" << block[index] << "最近未被使用,将要被页面" << page[i] << "替换!" << endl;
						page_in_block[page[i]] = index;
						block[index++] = page[i];
						++page_lack;
						break;
					}
					else
						nru[index++] = 0;
				}
			}
		}
		for (int k = 1; k <= m; ++k)
			cout << block[k] << " ";
		cout << endl;
	}
	cout << "CLOCK进行了" << page_lack - m << "次页面置换" << endl;
	return page_lack;
}

int main() {
	cout << "页面个数:";
	cin >> n;
	cout << "物理块数目:";
	cin >> m;
	cout << "请输入" << n << "个页面号:" << endl;
	for (int i = 1; i <= n; ++i) {
		cin >> page[i];
		page_max = max(page_max, page[i]);
	}
	cout << "缺页中断次数:" << opt() << endl;
	cout << "***********************************" << endl;
	cout << "缺页中断次数:" << lru() << endl;
	cout << "***********************************" << endl;
	cout << "缺页中断次数:" << fifo() << endl;
	cout << "***********************************" << endl;
	cout << "缺页中断次数:" << clock() << endl;
	return 0;
}

 输入样例:(参考《计算机操作系统第四版》P177第13题)

时钟算法 页面置换算法 #pragma once #include <iostream> #include "Pclass.h" using namespace std; class Allocation { private: int pnum;//页面数 int bnum;//分配块数 //int ID; int num;//访问页面次数 Pclass * block;//块数组 int *page;//页访问顺序 //int p; public: Allocation(int pn,int bl,int n) { pnum=pn; bnum=bl; num=n; //ID=id; block=new Pclass [bnum]; page=new int [num]; cout<<"页面访问顺序:"; for(int i=0;i<num;i++) { page[i]=rand()%pnum+1; cout<<page[i]<<" "; } cout<<endl; //p=0; } void FIFO() { cout<<"先进先出算法:"<<endl; int p=0; int nm=0; for(int i=0;i<num;i++) { if(Haven(page[i])!=(-1)) { nm++; } else { block[p].num=page[i]; p=(++p)%bnum; } for(int j=0;j<bnum;j++) { if(block[j].num!=0) { cout<<block[j].num<<" "; } } cout<<endl; } cout<<"命中率为:"<<(double)nm/(double)num<<endl; } int Haven(int n) { for(int i=0;i<bnum;i++) { if(block[i].num==n) { return i; } } return -1; } void LRU() { int p=0; int nm=0; for(int i=0;i<bnum;i++) { block[i].num=0; block[i].visited=false; } for(int i=0;i<num;i++) { int temp=Haven(page[i]); if(temp!=(-1)) { block[temp].visited=true; nm++; } else { //int j=0; while(1) { if(block[p].visited==false) { block[p].num=page[i]; p=(++p)%bnum; break; } else { block[p].visited=false; p=(++p)%bnum; } } } for(int j=0;j<bnum;j++) { if(block[j].num!=0) { cout<<block[j].num<<" "; } } cout<<endl; } cout<<"命中率为:"<<(double)nm/(double)num<<endl; } void SLRU() { int p=0; int nm=0; for(int i=0;i<bnum;i++) { block[i].num=0; block[i].visited=false; } for(int i=0;i<num;i++) { int temp=Haven(page[i]); if(temp!=(-1)) { block[temp].visited=true; nm++; } else { for(int j=0;j<bnum;j++) { if(block[p].visited==false&&block[p].changed==false) { block[p].num=page[i]; if(rand()%2) { block[p].changed=true; } p=(++p)%bnum; goto over; } else { block[p].visited=false; p=(++p)%bnum; } } while(1) { if(block[p].changed==false) { block[p].num=page[i]; p=(++p)%bnum; break; } else { block[p].changed=false; p=(++p)%bnum; } } } over: for(int j=0;j<bnum;j++) { if(block[j].num!=0) { cout<<block[j].num<<" "; } } cout<<endl; } cout<<"命中率为:"<<(double)nm/(double)num<<endl; } ~Allocation(void) { } };
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Skr.B

WUHOOO~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值