页面置换算法-LRU(Least Recently Used)c++实现
2024-08-31 07:32:09
最近最久未使用(LRU)置换算法
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <assert.h> using namespace std;
const int Maxn = ; typedef struct Page_Node{
struct Page_Node *next;
int page_id;
Page_Node(int v){page_id = v;}
}*PNode, Node; class LRU{
public:
LRU(int num):blocks_limit(num){
head = NULL;
} ~LRU(){
delete head;
} void update_order(int page_id){
PNode p = head, last_node = NULL;
while(p){
if(p -> page_id == page_id){
PNode next_node = p -> next;
p -> next = head;
if(last_node){
last_node -> next = next_node;
}head = p;
break;
}
last_node = p;
p = p -> next;
}
} void insert_page(int page_id){
PNode new_node = new Node(page_id);
PNode tmp = head;
head = new_node;
head -> next = tmp;
existed[page_id] = ;
} void delete_tail_page(){
assert(NULL != head);
PNode p = head, last_node = NULL;
while(p){
if(p -> next == NULL){
existed.erase(p->page_id);
if(last_node)
last_node -> next = NULL;
else{
head = NULL;
}
}
last_node = p;
p = p -> next;
}
} void disp_page_in_memory(){
cout<<"页面情况:"<<endl;
PNode p = head;
while(p){
cout<<" "<<p->page_id;
p = p -> next;
}cout<<endl;
} void call_page(int page_id){
if(existed[page_id]){
cout<<"In Memory"<<endl;
update_order(page_id);
}else{
cout<<"Out of Memory"<<endl;
if(existed.size() > blocks_limit){
cout<<"页面置换"<<endl;
delete_tail_page();
insert_page(page_id);
}else{
cout<<"直接调页"<<endl;
insert_page(page_id);
}
}
disp_page_in_memory();
} private:
PNode head;
map<int, int>existed;
int blocks_limit;
}; int main()
{
LRU lru();
while(){
int t;
cin>>t;
lru.call_page(t);
} return ;
}
最少使用次数(LFU)置换算法
先进先出置换算法(FIFO)
最新文章
- 【AutoMapper官方文档】DTO与Domin Model相互转换(上)
- 未能加载文件或程序集“System.Web.DataVisualization...”
- input输入内容时放大问题
- jQuery UI--jquery-autohide解读
- 2016.7.13abstract
- C#中创建、打开、读取、写入、保存Excel的一般性代码
- SGU131--NYOJ435
- linux命令-查看当前目录下所有文件的大小:“ll -h”
- HTML5新标签
- 道破Redis的VM
- kairosdb + cassandra Setup
- Eclipse导入项目常见问题----服务器版本问题02
- dotweb框架之旅 [二] - 常用对象-App(dotweb)
- WordPress彩色背景标签云实现
- 为什么目前无法再docker for windows中调用GPU
- Android中自定义广播的实现
- (转)mybatis数据库物理分页插件PageHelper
- docker安装指定版本TAG的镜像
- 001-mybatis框架
- 相关TableLayoutPanel分页显示自定义控件
热门文章
- UVA 674_Coin Change
- [bzoj3196][Tyvj1730]二逼平衡树_树套树_位置线段树套非旋转Treap/树状数组套主席树/权值线段树套位置线段树
- win10笔记本相机打开黑屏无法打开笔记本相机
- Ubuntu 16.04添加多张虚拟网卡
- 【转】配置windows路由表,使电脑同时连接内网外网方法
- 在对象内部尽量直接訪问实例变量 --Effictive Objective-C 抄书
- VMWare中的Host-only、NAT、Bridge的比較
- web 开发之js---理解并解决IE的内存泄漏方式
- git 在一台机器上配置多个账户
- 在js中取选中的radio值