山东建筑大学操作系统存储器管理
实验目的:
理解各类置换算法的原理和虚拟存储器管理的方法。
实验内容:
编程实现LRU算法或CLOCK/改进算法等置换算法(二选一)
实验步骤:
- 理解LRU或CLOCK改进算法等置换算法;
设计与算法相关的数据结构,如:LRU的堆栈或CLOCK改进算法中的循环结构;
按照最多5块的内存分配情况,编程实现所选算法,动态输入访问内存的块号序列,输出置换结果;
测试:输入合法、非法的访问序列数据,检查程序的正确性和健壮性。
详细代码:https://download.youkuaiyun.com/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;
}