操作系统页面调度算法

来源:互联网 发布:java反射的用途 编辑:程序博客网 时间:2024/06/06 03:39

算法由python实现

1.先进先出置换算法

    该算法是置换最早出现在stack里的页面,是最简单的页面置换算法,没有考虑程序的局部性原理

   在这里设置stack的大小为3,为了模拟删除和插入一个页面,用了pop和insert方法,可以直接用p[locate] = value

def FIFC(pageList):    p = []    locate = 0    num = 0    for i in pageList:        if i not in p:            if len(p) < 3:                p.append(i)            else:                print ('current pageStack content :', p)                p.pop(locate)                p.insert(locate,i)                locate = (locate + 1) % 3            num += 1    print ('current pageStack content :', p)    print ('中断次数: {},页面置换{}次,  缺页率{}'.format(num,num-3,num/len(pageList) * 100))

2.最近最久为使用算法

   这个算法考虑了程序的局部性原理,用最近的过去,模拟未来.

   函数f是一个辅助函数,在开始更改每一个页面的未被访问的时间时候,用for,需要两次,使用这函数,直接调用map,用times来记录当前stack里的页面多久未被访问,数字越大,就是越久未被访问


def f(enumVlaue):    if enumVlaue[0] == INDEX:        enumVlaue[1] = 0    else:        enumVlaue[1] += 1    return enumVlaue[1]def LRU(pageList):    p = []    times = [0 for i in range(3)]    num = 0    for i in pageList:        if i not in p:            if len(p) < 3:                p.append(i)                for j in range(len(p)-1):                    times[j] += 1            else:                global INDEX                INDEX = times.index(max(times))                print ('current pageStack content :{}  current times : {}'.format(p,times))                p.pop(INDEX)                p.insert(INDEX,i)                times = list(map(f,[list(i) for i in enumerate(times)]))            num += 1        else:            INDEX = p.index(i)            times = list(map(f,[list(i) for i in enumerate(times)]))    print ('current pageStack content :',p)    print ('中断次数: {},页面置换{}次,  缺页率{}'.format(num,num-3,num/len(pageList) * 100))

  3.最佳置换算法 

     这个算法只是理论上的,无法实践,不知道后面页面的序列,算法从未来考虑当前

     

def OPT(pageList):    p = []    num = 0    times = [0 for i in range(3)]    for i in pageStack:        if i not in p:            if len(p) < 3:                p.append(i)            else:                s = 0                loc = pageList.index(i)                for j in pageList[loc:]:                    if j in p:                        times[p.index(j)] = 1                        s += 1                    if s == 2:                        break                locate = times.index(0)                p.pop(locate)                p.insert(locate,i)                times = [0 for i in range(3)]                print ('current pageStack: content :',p)            num += 1    print ('中断次数: {},页面置换{}次,  缺页率{}'.format(num,num-3,num/len(pageList) * 100))
页面调用是数字表示

附一张运行最近最久未使用置换算法 截图

输入的数据是: 7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0 1 7 0 1



原创粉丝点击