LRU算法操作系统山东建筑大学

山东建筑大学操作系统存储器管理

实验目的:

理解各类置换算法的原理和虚拟存储器管理的方法。

实验内容:

编程实现LRU算法或CLOCK/改进算法等置换算法(二选一)

实验步骤:

  1. 理解LRU或CLOCK改进算法等置换算法;

设计与算法相关的数据结构,如:LRU的堆栈或CLOCK改进算法中的循环结构;

按照最多5块的内存分配情况,编程实现所选算法,动态输入访问内存的块号序列,输出置换结果;

测试:输入合法、非法的访问序列数据,检查程序的正确性和健壮性。

详细代码:https://download.csdn.net/download/qq_38971487/10887430

头文件:LRU.h

/*
 * LRU.h
 *
 *  Created on: 2018年11月25日
 *      Author: hp
 */

#ifndef LRU_H_
#define LRU_H_

#include<string>
#include<sstream>
#include<iostream>

using namespace std;

const int memory_block = 4;

struct chainNode
{
	int value;
	chainNode* next;
	chainNode* prev;

	chainNode(){next = NULL;prev = NULL;} // @suppress("Class members should be properly initialized")
	chainNode(int value){
		this->value = value;
		next = NULL;
		prev = NULL;
	}
	chainNode(int value,chainNode* next,chainNode* prev)
	{
		this->value = value;
		this->next = next;
		this->prev = prev;
	}
};

class LRUCache{
private:
	int listSize;
	chainNode *head;

public:
	LRUCache(int memory_block);//构造函数
	~LRUCache();//析构函数
	void visitLRU();//访问函数
	void inserthead(int value);//将新数据插入到链表头部
	void putList();//遍历函数
	void erase(chainNode *p);

};


//访问函数,核心
void LRUCache::visitLRU(){
	int n;
	int value;
	int size = memory_block +1;
	cout<<"请输入要访问的页面的总次数"<<endl;
	cin>>n;
	cout<<"请输入所访问页面的页面号序列"<<endl;
	for(int i = 0;i < n;++i){
		inserthead(-1);
		chainNode *x = head->next;
		cin>>x->value;
		value = x->value;
		chainNode *p = x->next;
		while(p->value != value){
			if(p == head)
				break;
			p = p->next;
		}
		if(p != head){
			erase(p);//删掉已经存在的节点
		}
		if(listSize > size){
			erase(head->prev);
		}
		cout<<"链表内容\t是否命中\n"<<endl;
		chainNode *q;
		q = head->next;
		while(q != head){
			cout<<q->value<<" ";
			q = q->next;
		}
		if(p != head)
			cout<<"命中"<<endl;
		if(p == head){
			cout<<"缺页"<<endl;
		}
	}

}




void LRUCache::putList(){
	chainNode *p = head->next;
	while(p != head){
		cout<<p->value<<" ";
		p = p->next;
	}
}







#endif /* LRU_H_ */

test.cpp:

#include "LRU.h"
#include<iostream>

int main(void){
	LRUCache y(5);
	y.visitLRU();

	return 0;
}

 

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值