python模拟页面调度LRU算法
所謂LRU算法,是指在發生缺頁并且沒有空閑主存塊時,把最近最少使用的頁面換出主存塊,騰出地方來調入新頁面。
問題描述:一進程獲得n個主存塊的使用權,對于給定的進程訪問頁面次序,問當采用LRU算法時,輸出發生的缺頁次數。
?
這個題為京東2015年筆試考題,主要考察對LRU算法的理解
代碼如下
n = int(input())
def LRU(pages, maxNum):
??? temp = []
??? times = 0
??? for page in lst:
??????? num = len(temp)
??????? if num < n:
??????????? times += 1
??????????? temp.append(page)
??????? elif num == n:??????????????? #要訪問的新頁面已在主存塊中
??????????? if page in temp:????????? #處理“主存塊”,把最新訪問的頁面交換到列表尾部
??????????????? pos = temp.index(page)
??????????????? temp = temp[:pos] + temp[pos+1:] + [page]
??????????? else:???????????????????? #把最早訪問的頁面踢掉,調入新頁面
??????????????? temp.pop(0)
??????????????? temp.append(page)
??????????????? times += 1
??? return times
lst = (1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5)
print(LRU(lst, 3))
截圖如下
轉載于:https://www.cnblogs.com/liuzhaowei/p/10770783.html
總結
以上是生活随笔為你收集整理的python模拟页面调度LRU算法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 2.18比赛(T2,T3留坑)
- 下一篇: 致所有的开发者们