实验目的
通过模拟实现请求页式存储管理的几种基本页面置换算法,了解虚拟存储技术的特点,掌握虚拟存储请求页式存储管理中几种基本页面置换算法的基本思想和实现过程,并比较它们的效率。
实验内容
设计一个虚拟存储区和内存工作区,并使用下述算法计算访问命中率。
1、最佳淘汰算法(OPT)
2、先进先出的算法(FIFO)
3、最近最久未使用算法(LRU)
4、最不经常使用算法(LFU)
5、最近未使用算法(NUR)
命中率=1-页面失效次数/页地址流长度
实验准备
本实验的程序设计基本上按照实验内容进行。即首先用srand( )和rand( )函数定义和产生指令序列,然后将指令序列变换成相应的页地址流,并针对不同的算法计算出相应的命中率。
(1)通过随机数产生一个指令序列,共320条指令。指令的地址按下述原则生成:
A:50%的指令是顺序执行的
B:25%的指令是均匀分布在前地址部分
C:25%的指令是均匀分布在后地址部分
具体的实施方法是:
A:在[0,319]的指令地址之间随机选取一起点m
B:顺序执行一条指令,即执行地址为m+1的指令
C:在前地址[0,m+1]中随机选取一条指令并执行,该指令的地址为m’
D:顺序执行一条指令,其地址为m’+1
E:在后地址[m’+2,319]中随机选取一条指令并执行
F:重复步骤A-E,直到320次指令
(2)将指令序列变换为页地址流
设:页面大小为1K;
用户内存容量4页到32页;
用户虚存容量为32K。
在用户虚存中,按每K存放10条指令排列虚存地址,即320条指令在虚存中的存放方式为:
第 0 条-第 9 条指令为第0页(对应虚存地址为[0,9])
第10条-第19条指令为第1页(对应虚存地址为[10,19])
………………………………
第310条-第319条指令为第31页(对应虚存地址为[310,319])
按以上方式,用户指令可组成32页。
实验指导
一、虚拟存储系统
UNIX中,为了提高内存利用率,提供了内外存进程对换机制;内存空间的分配和回收均以页为单位进行;一个进程只需将其一部分(段或页)调入内存便可运行;还支持请求调页的存储管理方式。
当进程在运行中需要访问某部分程序和数据时,发现其所在页面不在内存,就立即提出请求(向CPU发出缺中断),由系统将其所需页面调入内存。这种页面调入方式叫请求调页。
为实现请求调页,核心配置了四种数据结构:页表、页框号、访问位、修改位、有效位、保护位等。
二、页面置换算法
当CPU接收到缺页中断信号,中断处理程序先保存现场,分析中断原因,转入缺页中断处理程序。该程序通过查找页表,得到该页所在外存的物理块号。如果此时内存未满,能容纳新页,则启动磁盘I/O将所缺之页调入内存,然后修改页表。如果内存已满,则须按某种置换算法从内存中选出一页准备换出,是否重新写盘由页表的修改位决定,然后将缺页调入,修改页表。利用修改后的页表,去形成所要访问数据的物理地址,再去访问内存数据。整个页面的调入过程对用户是透明的。
常用的页面置换算法有
1、最佳置换算法(Optimal)
2、先进先出法(Fisrt In First Out)
3、最近最久未使用(Least Recently Used)
4、最不经常使用法(Least Frequently Used)
5、最近未使用法(No Used Recently)
代码
#include <iostream>
#include <ctime>
#include <random>
using namespace std;
#define TRUE 1
#define FALSE 0
#define INVALID -1
#define NULL 0
#define total_instruction 320 //指令数量
#define total_vp 32 //页表数量
#define clear_period 50 //清0周期
struct pl_type { /*页表结构*/
int pn, pfn, counter, time;
};
pl_type pl[total_vp]; //页表数组
struct pfc_type { /*内存表结构*/
int pn, pfn;
pfc_type *next;
};
pfc_type pfc[total_vp], *freepf_head, *busypf_head, *busypf_tail;
int diseffect; // 未命中次数
int a[total_instruction]; // 存储320条指令
int page[total_instruction]; // 每条指令对应的页表号
int offset[total_instruction];
int initialize(int);
int FIFO(int);
int LRU(int);
int NUR(int);
int LFU(int);
int OPT(int);
int main(){
int s;
srand(unsigned(time(0)));
for (int i = 0; i < total_instruction; i += 4) { /*产生指令队列*/
s = rand() % 320;
a[i] = s + 1; //顺序执行一条指令
a[i + 1] = rand() % (a[i] + 1); //执行前地址指令m'
a[i + 2] = a[i + 1] + 1; //顺序执行一条指令
a[i + 3] = rand() % (319 - a[i + 2]) + (a[i + 2] + 1); // 执行后地址指令
if (a[i] > 319 || a[i + 1] > 319 || a[i + 2] > 319 || a[i+3] > 319) {
i -= 4;
}
}
for (int i = 0; i < total_instruction; i++) { /*将指令序列变换成页表地址流*/
page[i] = a[i] / 10;
offset[i] = a[i] % 10;
}
for (int i = 4; i <= 32; i++) { /*用户内存工作区从4个页面到32个页面*/
printf("---%2d page frames---\n", i);
FIFO(i);
LRU(i);
NUR(i);
LFU(i);
OPT(i);
}
return 0;
}
int initialize(int total_pf) { /*初始化相关数据结构*/
diseffect = 0;
// 初始化页表
for (int i = 0; i<total_vp; i++){
pl[i].pn = i;
pl[i].pfn = INVALID; // 初始,该页不在内存中
pl[i].counter = 0; // 访问次数或作为引用位
pl[i].time = -1; // 时间
}
// 初始内存表,建立pfc[i-1]和pfc[i]之间的链接
for (int i = 0; i < total_pf - 1; i++){
pfc[i].next = &pfc[i + 1];
pfc[i].pfn = i;
}
pfc[total_pf - 1].next = NULL;
pfc[total_pf - 1].pfn = total_pf - 1;
freepf_head = &pfc[0]; //内存空页面队列的头指针为pfc[0]
return 0;
}
int FIFO(int total_pf) { /*先进先出算法*/
pfc_type *p;
initialize(total_pf); //初始化相关页面控制用数据结构
busypf_head = busypf_tail = NULL; //内存页的队列头,队列尾指针接
for (int i = 0; i<total_instruction; i++){
if (pl[page[i]].pfn == INVALID) { //页表项不在内存中
diseffect += 1; //失效次数
if (freepf_head == NULL) { //内存无空闲页面
p = busypf_head->next;
pl[busypf_head->pn].pfn = INVALID;
freepf_head = busypf_head; //释放忙页面队列的第一个页面
freepf_head->next = NULL;
busypf_head = p;
}
// 按FIFO方式调新页面入内存页面
p = freepf_head->next; // 先保存内存表中当前位置的下一位置
freepf_head->next = NULL;
freepf_head->pn = page[i]; // 页表号
pl[page[i]].pfn = freepf_head->pfn; // 内存块号
if (busypf_tail == NULL) {
// busypf_head指向最老的,busypf_tail指向最新的
busypf_head = busypf_tail = freepf_head;
}
else{
busypf_tail->next = freepf_head; //free页面减少一个
busypf_tail = freepf_head;
}
freepf_head = p;
}
}
printf("FIFO:%6.4f\n", 1 - diseffect / 320.0);
return 0;
}
int LRU(int total_pf) { /*最近最久未使用算法(使用时钟计数器)*/
int min, minj, present_time;
initialize(total_pf);
present_time = 0;
for (int i = 0; i<total_instruction; i++){
if (pl[page[i]].pfn == INVALID) { //页面失效,不在内存中
diseffect++;
if (freepf_head == NULL) { //内存无空闲页面
min = 32767;
for (int j = 0; j < total_vp; j++) { //找出内存块中time的最小值
if (min > pl[j].time && pl[j].pfn != INVALID) // 查询页表
{
min = pl[j].time;
minj = j; // 记下内存块号
}
}
freepf_head = &pfc[pl[minj].pfn]; //腾出一个单元(对应的内存块)
pl[minj].pfn = INVALID;
pl[minj].time = -1;
freepf_head->next = NULL;
}
pl[page[i]].pfn = freepf_head->pfn; //有空闲页面,改为有效(内存块号)
pl[page[i]].time = present_time;
freepf_head = freepf_head->next; //减少一个free 页面
}
else {
pl[page[i]].time = present_time; //命中则设置时间
}
present_time++;
}
printf("LRU:%6.4f\n", 1 - diseffect / 320.0);
return 0;
}
int NUR(int total_pf) { /*最近未使用算法(每执行50条指令引用位清零一次)*/
int dp, cont_flag, old_dp;
initialize(total_pf);
dp = 0;
for (int i = 0; i < total_instruction; i++) {
if (pl[page[i]].pfn == INVALID) { //页面失效,不在内存中
diseffect++;
if (freepf_head == NULL) { //无空闲页面
cont_flag = TRUE;
old_dp = dp;
while (cont_flag) {
// 查页表(对应引用位0,在内存中)
if (pl[dp].counter == 0 && pl[dp].pfn != INVALID) {
cont_flag = FALSE;
}
else {
dp++;
if (dp == total_vp) {
dp = 0;
}
if (dp == old_dp) {
// 第2轮扫描结束,失败(引用位全部置为0)
for (int j = 0; j < total_vp; j++) {
pl[j].counter = 0;
}
}
}
}
freepf_head = &pfc[pl[dp].pfn]; //腾出一个单元(对应的内存块)
pl[dp].pfn = INVALID;
freepf_head->next = NULL;
}
pl[page[i]].pfn = freepf_head->pfn;
freepf_head = freepf_head->next;
}
else {
pl[page[i]].counter = 1; // 在内存中,“引用位”置为1
}
// 每执行50条指令,引用位清零一次
if (i%clear_period == 0) {
for (int j = 0; j < total_vp; j++) {
pl[j].counter = 0;
}
}
}
printf("NUR:%6.4f\n", 1 - diseffect / 320.0);
return 0;
}
int OPT(int total_pf) { /*最佳置换算法*/
int max, maxpage, d, dist[total_vp];
initialize(total_pf);
for (int i = 0; i < total_instruction; i++) {
if (pl[page[i]].pfn == INVALID) { //页面失效,不在内存中
diseffect++;
if (freepf_head == NULL) { //无空闲页面
for (int j = 0; j < total_vp; j++) {
if (pl[j].pfn != INVALID) {
dist[j] = 32767; /* 最大"距离" */
}
else {
dist[j] = 0;
}
}
d = 1;
for (int j = i + 1; j < total_instruction; j++) {
if (pl[page[j]].pfn != INVALID && dist[page[j]] == 32767) {
dist[page[j]] = d;
}
d++;
}
max = -1;
for (int j = 0; j < total_vp; j++) {
if (max < dist[j]) {
max = dist[j];
maxpage = j;
}
}
freepf_head = &pfc[pl[maxpage].pfn];
freepf_head->next = NULL;
pl[maxpage].pfn = INVALID;
}
pl[page[i]].pfn = freepf_head->pfn;
freepf_head = freepf_head->next;
}
}
printf("OPT:%6.4f\n", 1 - diseffect / 320.0);
return 0;
}
int LFU(int total_pf){ /*最不经常使用置换法*/
int i, j, min, minpage;
initialize(total_pf);
for (i = 0; i < total_instruction; i++){
if (pl[page[i]].pfn == INVALID) { //页面失效,不在内存中
diseffect++;
if (freepf_head == NULL) { //无空闲页面
min = 32767;
for (j = 0; j < total_vp; j++) {
if (min > pl[j].counter && pl[j].pfn != INVALID) {
min = pl[j].counter;
minpage = j;
}
//pl[j].counter = 0;
//pl[j].counter >>= 1;
}
freepf_head = &pfc[pl[minpage].pfn];
pl[minpage].pfn = INVALID;
freepf_head->next = NULL;
}
pl[page[i]].pfn = freepf_head->pfn; //有空闲页面,改为有效
pl[page[i]].counter++;
freepf_head = freepf_head->next; //减少一个free页面
}
else {
pl[page[i]].counter++; // 软件计数器(被访问次数)
}
// 定期右移
if (i % 15 == 0) {
for (int j = 0; j < total_vp; j++) {
pl[j].counter >>= 1;
}
}
}
printf("LFU:%6.4f\n", 1 - diseffect / 320.0);
return 0;
}