2 任务:LRU算法 2.1 任务描述 设计LRU页面置换算法模拟程序:从键盘输入访问串。计算LRU算法在不同内存页框数时的缺页数和缺页率。要求程序模拟驻留集变化过程,即能模拟页框装入与释放过程。 2.2任务要求 输入串长度作为总页框数目,补充程序完成LRU算法。 2.3算法思路 LRU算法的原理是置换过去被访问距当前最远的页。LRU的实现有几种方法,如计时法、计数法、队列法。这里采用计时法。给每页设一个访问时间time,初始值都为-1。每访问一页,把它的time置为当前时间。缺页时,若需替换,淘汰time值最小的页面,淘汰后把该time置为-1。程序修改如下:
#include <stdio.h>
#include <stdlib.h>#ifndef TRUE
#define TRUE 1
#define FALSE 0
#endif#ifndef NULL
#define NULL 0
#endif#define INVALID -1 //-1表示缺页页表结构类型
typedef struct pl_type {int pn; //页号int fn; //页框号int time; //访问时间int dist; //下次访问离当前页的距离
}pl_type;//页框链结构类型
typedef struct fl_type {int pn; //页号int fn; //页框号struct fl_type *next; //链接指针
}fl_type;//结构变量
pl_type pl[512]; //页表
fl_type fl[512],*free_head,*busy_head,*busy_tail; //页框int pag