###page-replacement-policy.py 代码
虚拟页访问序列:addresses=1,2,3,4,0,5.... 这里面的最大值代表最大页号 页替换算法:policy=FIFO, LRU, OPT, CLOCK CLOCK算法用的bit位数:clockbits=1 物理页帧大小:cachesize 实际保持的也访问序列:addrList [1,2,3,4,0,5,...] 物理页帧内容:memory [] 初始为空 当前占用的页帧数量:count 初始位0
for nStr in addrList:
# for clock need to track the ref by reference bits
try:
idx = memory.index(n)
hits = hits + 1
if policy == 'LRU' :
....
except:
idx = -1 #missing
miss = miss + 1
if idx=-1 and ...: #missing and need replacement
#if phy page frames are full
# for FIFO , LRU
# replace victim item from memory by " victim = memory.pop(0)"
# for CLOCK
# find one page for the beginning of scan
# check ref[page] and update ref[page]
# find a victim which ref[page]=0 by memory.remove(page)
else:
# miss, but no replacement needed (phy page frame not full)
# now add to memory
#update ref for clock replacement