1.实验要求
已知系统为一进程分配的物理块数,进程运行过程中引用的页面号,编程使用LRU算法
输出置换的页号、缺页中断次数及缺页率。
2.算法描述
从内存调出一页程序或数据到磁盘的对换区,把选择换出的页面的算法称为页面置换算法。
置换算法的好坏将直接影响系统的性能。
LRU置换算法(基于计数器法)是选择最近最久未使用的页面予以淘汰
。该算法赋予每个页面一个访问字段,用来记录一个页面自上次被访问以来所经历的时间 t
,当须淘汰一个页面时,选择现有页面中其 t 值最大的
,即最近最久未使用的页面被淘汰掉。
3.实验源码
#include<iostream>
#include<queue>
using namespace std;
// 操作系统实验 页面置换算法 LRU置换算法 最近最久未使用的页面予以淘汰 基于计数器
struct page
{
int no; //页号
int t; //访问字段t值, 用来记录当前页面自上次访问以来所经历的时间
}p[100];
queue<int> dl;
int main()
{
int N; //请求页号的队列长度
int x; //请求页号
int n; //内存中物理块数
int old;
int sp; //缺页中断次数
bool flag;
cout << "run page queue length" << endl;
cin >> N;
cout << "your queue" << endl;
for (int i = 0; i < N; i++)
{
cin >> x;
dl.push(x);
}
cout << "how many max page" << endl;
cin >> n;
//初始化
for (int i = 0; i < n; i++)
{
p[i].no = -1;
int t= 0;
}
sp = 0;
cout << "memory note>>" << endl;
while (!dl.empty())
{
x = dl.front();//取得队首元素
dl.pop();//去除队首元素
flag = false;
for (int i = 0; i < n; i++)
{
//如果是动态运行时装入\动态运行时链接方式,此处也要算进缺页中断次数,删除flag那条语句
if (p[i].no == -1)
{
p[i].no = x;
p[i].t = 1;
flag = true;
break;
}
else if (p[i].no == x) //请求的页号已在内存,t值置1
{
p[i].t = 1;
flag = true;
break;
}
}
//不存在的页号调入内存,产生缺页中断sp++,
if (!flag)
{
sp++;
old = 0;
for (int i = 0; i < n; i++)
{
if (p[i].t > p[old].t) //选择t值最大的置换
{
old = i;
}
}
p[old].no = x;
p[old].t = 1;
}
//其他的页号 t值加1
for (int i = 0; i < n; i++)
{
if (p[i].no != -1)
{
p[i].t++;
}
}
for (int i = 0; i < n; i++)
{
if (p[i].no != -1)
{
cout << p[i].no << " ";
}
}
if (!flag)//标记:调入页号产生了中断
{
cout << "#";
}
cout << endl;
}
cout << "swap page counts: " << sp << endl; //中断次数
cout << "swap page percent: " << (sp*100.0) / (N*1.0) << "%" << endl; //缺页率
return 0;
}
补 基于时间戳实现LRU
基本思路:在每次内存访问时,给相应的页面打上时间戳,然后在缺页中断时,选择最老的页面淘汰。
#include<iostream>
#include<queue>
using namespace std;
// 操作系统实验 页面置换算法 LRU置换算法 最近最久未使用的页面予以淘汰 基于时间戳
struct page
{
int no; //页号
int t; //访问字段t值, 用来记录当前页面访问时的时间
}p[100];
queue<int> dl;
int main()
{
int N; //请求页号的队列长度
int x; //请求页号
int n; //内存中物理块数
int old;
int sp; //缺页中断次数
bool flag=false;
int temp=1;//记录当前最晚的时间t,从1秒开始
cout << "run page queue length" << endl;
cin >> N;
cout << "your queue" << endl;
for (int i = 0; i < N; i++)
{
cin >> x;
dl.push(x);
}
cout << "how many max page" << endl;
cin >> n;
//初始化
for (int i = 0; i < n; i++)
{
p[i].no = -1;
int t = 999;
}
sp = 0;
cout << "memory note>>" << endl;
while (!dl.empty())
{
x = dl.front();//取得队首元素
dl.pop();//去除队首元素
for (int i = 0; i < n; i++)
{
if (p[i].no == -1)
{
p[i].no = x;
p[i].t = temp;
temp++;
flag = true;
break;
}
else if (p[i].no == x) //请求的页号已在内存
{
p[i].t = temp;
flag = true;
temp++;
break;
}
}
//不存在的页号调入内存,产生缺页中断sp++
if (!flag)
{
sp++;
old = 0;
for (int i = 0; i < n; i++)
{
if (p[i].t < p[old].t) //选择t值最小即最老的置换 123 -> 423 -> 453
{
old = i;
}
}
p[old].no = x;
p[old].t = temp;
temp++;
}
for (int i = 0; i < n; i++)
{
if (p[i].no != -1)
{
cout << p[i].no << " ";
}
}
if (!flag || temp<=4)//标记:调入页号产生了中断。temp<=4,把前3次算上
{
cout << "#";
}
//还原。由于初始123次改变了全局的flag ==true
flag = false;
cout << endl;
}
cout << "swap page counts: " << sp+3 << endl; //中断次数
cout << "swap page percent: " << ((sp+3)*100.0) / (N*1.0) << "%" << endl; //缺页率
return 0;
}