《操作系统》的实验代码。
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

46 regels
1.7 KiB

  1. # 理解页置换算法
  2. ## 选择四种替换算法(0:LRU置换算法,1:改进的clock 页置换算法,2:工作集页置换算法,3:缺页率置换算法)中的一种来设计一个应用程序(可基于python, ruby, C, C++,LISP等)模拟实现,并给出测试。请参考page-replacement-policy.py 代码或独自实现。
  3. ###page-replacement-policy.py 代码
  4. #### 对模拟环境的抽象
  5. 虚拟页访问序列:addresses=1,2,3,4,0,5.... 这里面的最大值代表最大页号
  6. 页替换算法:policy=FIFO, LRU, OPT, CLOCK
  7. CLOCK算法用的bit位数:clockbits=1
  8. 物理页帧大小:cachesize
  9. 实际保持的也访问序列:addrList [1,2,3,4,0,5,...]
  10. 物理页帧内容:memory [] 初始为空
  11. 当前占用的页帧数量:count 初始位0
  12. #### 执行过程描述
  13. ```
  14. for nStr in addrList:
  15. # for clock need to track the ref by reference bits
  16. try:
  17. idx = memory.index(n)
  18. hits = hits + 1
  19. if policy == 'LRU' :
  20. ....
  21. except:
  22. idx = -1 #missing
  23. miss = miss + 1
  24. if idx=-1 and ...: #missing and need replacement
  25. #if phy page frames are full
  26. # for FIFO , LRU
  27. # replace victim item from memory by " victim = memory.pop(0)"
  28. # for CLOCK
  29. # find one page for the beginning of scan
  30. # check ref[page] and update ref[page]
  31. # find a victim which ref[page]=0 by memory.remove(page)
  32. else:
  33. # miss, but no replacement needed (phy page frame not full)
  34. # now add to memory
  35. #update ref for clock replacement
  36. ```