Answers:
我认为这个例子可以澄清您的所有疑问。
例如:
假设在起始页上主存储器为空,则参考序列为:
3 2 3 0 8 4 2 5 0 9 8 3 2
每帧一个参考位(称为“已使用”位)
PU 3 PU 2 PU 3 PU 0 PU 8 PU 4 + --- + --- + + --- + --- + + --- + --- + + --- + --- + + --- + --- + +- --- + | | 0 | * | 3 | 1 | | 3 | 1 | | 3 | 1 | | 3 | 1 | | 3 | 1 | + --- + --- + + --- + --- + + --- + --- + + --- + --- + + --- + --- + +- --- + | | 0 | | | 0 | * | 2 | 1 | | 2 | 1 | | 2 | 1 | | 2 | 1 | + --- + --- + + --- + --- + + --- + --- + + --- + --- + + --- + --- + +- --- + | | 0 | | | 0 | | | 0 | * | | 0 | * | 0 | 1 | | 0 | 1 | + --- + --- + + --- + --- + + --- + --- + + --- + --- + + --- + --- + +- --- + | | 0 | | | 0 | | | 0 | | | 0 | | | 0 | * | 8 | 1 | + --- + --- + + --- + --- + + --- + --- + + --- + --- + + --- + --- + +- --- + | | 0 | | | 0 | | | 0 | | | 0 | | | 0 | | | 0 | * + --- + --- + + --- + --- + + --- + --- + + --- + --- + + --- + --- + +- ---- PU 2 PU 5 PU 0 PU 9 PU 8 PU 3 + --- + --- + + --- + --- + + --- + --- + + --- + --- + + --- + --- + +- --- + | 3 | 1 | * | 3 | 1 | * | 5 | 1 | | 5 | 1 | | 5 | 1 | | 5 | 1 | + --- + --- + + --- + --- + + --- + --- + + --- + --- + + --- + --- + +- --- + | 2 | 1 | | 2 | 1 | | 2 | 0 | * | 2 | 0 | * | 9 | 1 | | 9 | 1 | + --- + --- + + --- + --- + + --- + --- + + --- + --- + + --- + --- + +- --- + | 0 | 1 | | 0 | 1 | | 0 | 0 | | 0 | 1 | | 0 | 1 | * | 0 | 1 | * + --- + --- + + --- + --- + + --- + --- + + --- + --- + + --- + --- + +- --- + | 8 | 1 | | 8 | 1 | | 8 | 0 | | 8 | 0 | | 8 | 0 | | 8 | 1 | + --- + --- + + --- + --- + + --- + --- + + --- + --- + + --- + --- + +- --- + | 4 | 1 | | 4 | 1 | | 4 | 0 | | 4 | 0 | | 4 | 0 | | 4 | 0 | + --- + --- + + --- + --- + + --- + --- + + --- + --- + + --- + --- + +- ---- PU 2 PU + --- + --- + + --- + --- + | 5 | 1 | * | 5 | 0 | + --- + --- + + --- + --- + | 9 | 1 | | 9 | 0 | + --- + --- + + --- + --- + | 0 | 0 | | 2 | 1 | + --- + --- + + --- + --- + | 8 | 0 | | 8 | 0 | * + --- + --- + + --- + --- + | 3 | 1 | | 3 | 1 | + --- + --- + + --- + --- + * =指示标识下一个要扫描位置的指针 P =该帧中存储的页面号 U =使用的标志, 0 =最近未使用 1 =最近引用 这称为BSD Linux中使用的线性扫描算法或二次机会算法。 通常,它被实现为循环队列。
如果引用到达内存中已存在的页面,则根本不会调用替换算法。
该时钟替换算法试图实现一些LRU替换的好处,但没有在每一页命中操纵LRU位的庞大开销。
页面可以处于以下三种状态之一:
recently-used
位为true
。在这种情况下,访问页面时不会出现页面错误,因此不会改变任何位。recently-used
位为false
。在这种情况下,页面也会在页面表中标记出来,以使如果访问该页面,则会发生页面错误。(如果在这种情况下发生页面错误,则页面错误处理程序唯一要做的就是将状态更改为recently-used
。)clock-hand
。当clock-hand
指向recently-used
位已设置的页面时,true
我们将recently-used
位翻转到false
,然后递增clock-hand
以指向下一页。当我们找到一个recently-used
已清除的页面时,即替换该页面。然后,我们将新页面标记为,recently-used
然后将递增clock-hand
到下一页。从本质上讲,时钟是一种近似LRU的概率算法。如果访问该页面的速率远高于访问该页面clock-hand
回到同一页面的速率,则该页面被标记的可能性很高recently-used
。如果访问页面的速度比访问页面的速度低clock-hand
,则页面更有可能处于not 状态recently-used
。最近使用过的页面将永远不会被替换。(为什么?)