LRUアルゴリズムのオペレーティングシステム、山東大学Jianzhu

山東Jianzhu大学オペレーティングシステムのメモリ管理

目的:

置換アルゴリズムのすべてのタイプの方法および仮想メモリ管理の原理を理解します。

実験:

LRUアルゴリズムまたはプログラミングCLOCK /置換アルゴリズム向上アルゴリズム(代替)

実験手順:

  1. 改善された理解CLOCKまたはLRU置換アルゴリズムアルゴリズム。

設計データ構造などのような関連アルゴリズム:環状構造を改善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;
}

 

おすすめ

転載: blog.csdn.net/qq_38971487/article/details/85253550