2 任务:OPT算法 2.1 任务描述 设计OPT页面置换算法模拟程序:从键盘输入访问串。计算OPT算法在不同内存页框数时的缺页数和缺页率。要求程序模拟驻留集变化过程,即能模拟页框装入与释放过程。 2.2任务要求 输入串长度作为总页框数目,补充程序完成OPT算法。 2.3算法思路 OPT算法的原理是置换将来被访问距当前最远的页。实际算法设计是给每页设置一个距离dist。缺页时,若需替换,从当前页开始向后遍历所有页,计算距离。淘汰距离最大的页面。
#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