import copy
print("内存块个数为:",end='')
a = int(input())
print("请输入页面串(按空格分隔):",end='')
b = input().split()
c = len(b)*[a*[' ']]
e = copy.copy(c)
d = copy.copy(c)
n = 0
for i in range(len(b)):
if b[i] not in d[i]:
d[i][n%a] = b[i]
n += 1
if len(b) > i+1:
d[i + 1] = copy.copy(d[i])
print("FIFO算法:")
print(*b,sep=' ')
print()
for i in range(a):
for j in range(len(b)):
print(d[j][i],end=' ')
print()
print("缺页中断率为:{}".format(n/len(b)))
n = 0
e = len(b)*[a*[' ']]
x = 0
for i in range(len(b)):
if b[i] not in e[i]:
if e[i][-1] == ' ':
e[i][x] = b[i]
x += 1
n += 1
else:
p = b[i:]
q = 0
for j in range(1,a):
if e[i][q] not in p:
break
if e[i][j] not in p:
q = j
break
if p.index(e[i][j]) > p.index(e[i][q]):
q = j
e[i][q] = b[i]
n += 1
if len(b) > i+1:
e[i + 1] = copy.copy(e[i])
print("OPT算法:")
print(*b,sep=' ')
print()
for i in range(a):
for j in range(len(b)):
print(e[j][i],end=' ')
print()
print("缺页中断率为:{}".format(n/len(b)))
n = 0
f = len(b)*[a*[' ']]
x = 0
ui = []
for i in range(len(b)):
if b[i] not in f[i]:
if f[i][-1] == ' ':
f[i][x] = b[i]
x += 1
n += 1
ui.append(b[i])
else:
yu = f[i].index(ui[0])
f[i][yu] = b[i]
ui = ui[1:]
ui.append(b[i])
n += 1
else:
ui.remove(b[i])
ui.append(b[i])
if len(b) > i+1:
f[i + 1] = copy.copy(f[i])
print("LRU算法:")
print(*b,sep=' ')
print()
for i in range(a):
for j in range(len(b)):
print(f[j][i],end=' ')
print()
print("缺页中断率为:{}".format(n/len(b)))
|