页面替换算法
学生姓名:王昊飞
学号:16281269
- 算法原理及过程:
- 最佳置换算法(OPT):
- 原理:所选择的被淘汰的页面将是以后永不使用的,或是在将来最长时间内不再被访问的页面。在这样理想的情况下,OPT算法能够达到最低的缺页率,但是其条件是理想的无法实现的,一般用该算法评价其他算法。
- 过程:当发生页面置换时,检查页面是否包含在内存中,是的话直接显示,否则检查内存是否满,在满的情况下淘汰引用串和。
- 随机置换算法(PRA):
- 原理:所选择的被淘汰的页面将是内存里随机的一个页面。
- 过程:当发生页面置换时,查询页面是否在内存中,在的话直接显示页面,否则检验内存是否满,在满的情况下随机淘汰一个内存中页面,不满的情况下直接插入内存空位。
- 先进先出置换算法(FIFO):
- 原理:所选择的被淘汰页面是最先进入内存的页面。
- 过程:当发生页面置换时,查询页面是否在内存中,在的话直接显示页面,否则查询内存是否满,在满的情况下淘汰一个内存中最早进入的页面,不满的情况下直接插入内存空位。
- 最近最久未使用置换算法(LRU):
- 原理:所选择的被淘汰的页面是最近最久未使用的页面。
- 过程:当发生页面置换时,查询页面是否在内存中,在的话直接显示页面,否则查询内存是否满,在满的情况下淘汰一个内存中最近最久未使用的页面,不满的情况下直接插入内存空位。
- 简单时钟置换算法(First-Clock):
- 原理:所选择的被淘汰的页面是访问位为0的页面。
- 过程:当发生页面置换时,查询页面是否在内存中,在的话直接显示页面,否则查询内存是否满,在满的情况下,依次访问内存中的页面访问位,为1则置为0访问下一位;为0则淘汰页面,将新插入的页面访问位置为1,指针指向下一位。在不满的情况下直接插入内存空位,且页面访问位置为1,指针指向内存下一位.
- 改进型时钟置换算法(Second-Clock):
- 原理:所选择的最佳被淘汰页面是访问位为0,修改位也为0的页面。
- 过程:当发生页面置换时,首先判断是否为修改程序:如果是则修改插入页面的修改位为1否则为0,接着查询页面是否在内存中,在的话直接显示页面,否则查询内存是否满,在满的情况下,
-
- 依次访问内存页面的修改位和访问位,如果修改位与访问位同时为0则淘汰该页面,同时将新插入的页面访问位置为1,指针指向下一位;
- 遍历后不存在同时为0的情况,那么重新依次查找访问位为0修改位为1的页面,如果指针位置的访问位为1,则将其置为0,为0则淘汰该页面,并且将新插入的页面访问位置为1,指针指向下一位.
- 这时遍历如果还未找到,则再次重新遍历,按1)与2)的过程就能找到合适页面,淘汰该页面并且将新插入的页面访问位置为1。
-
- 用于测试的引用串
- 原理:引用串的关键在于如何模拟程序的局部性。
- 过程:
- 确定虚拟内存的尺寸P,工作面的起始位置p,工作面中包含的页数e,工作面移动率m,以及一个范围在0和1之间的值t
- 生成m个取值范围在p和p+e间的随机数,并记录到引用串中
- 生成一个随机数r,0 ≤ r ≤ 1, 如果r < t,则为p生成一个新值,否则p=(p+1)modP
- 如果想继续加大引用串的长度,请返回第二步,否则结束
- 最佳置换算法(OPT):
-
算法流程图
- 结果分析
- FIFO算法是否比随机替换算法优越?LRU算法比FIFO 算法优越 多少?
-
FIFO算法在总体上比随机替换算法优先,但是在某些特定的引用串的情况下,随即替换算法比FIFO算法更加优越,LRU算法的置换次数是FIFO置换算法低一些,所以效率提升不是很明显,但是在具体实现的过程中,却需要付出很大的代价,即为每页配置的寄存器,或者一个栈,这样计算下来,LRU置换算法在现实中的应用价值很低,其只存在理论上的在置换次数方面的优越性。
-
- LRU算法和最优算法有何差距?
-
LRU算法与最优算法的差距体现在两个方面,一个是磁环效率方面的差距,最优置换算法的置换次数大概是LRU置换算法低较多,但是在某些引用串时,LRU的置换效率与OPT的差别不大;另一个方面是在其具体实现的时候,需要付出的硬件代价比较大。
-
- Clock算法和LRU算法有何差距?
-
Clock置换算法与LRU置换算法的差距主要体现在置换效率方面,其置换次数比LRU略多,差距并不大;而在具体实现方面,Clock实现的硬件代价比LRU小,因为Clock只需要一位标志位,而LRU需要配置一个和页面数相等的n位寄存器。
- 实验问题
-
主要解决页面替换算法的逻辑和相关数据结构的适配。
- 心得体会
-
本次实验完成了对页面替换算法的实现,分别用了不同的数据结构诸如链表和队列实现相关的模拟,程序流程相互比较类似,实现思路比较清楚。