-
Notifications
You must be signed in to change notification settings - Fork 9
/
Optimal Page Replacement Algorithm (OPT).py
37 lines (36 loc) · 1.18 KB
/
Optimal Page Replacement Algorithm (OPT).py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#Optimal page replacement algorithm (OPT or OPR) implementation in python
#Created By: Suman Adhikari
print("Enter the number of frames: ",end="")
capacity = int(input())
f,fault,pf = [],0,'No'
print("Enter the reference string: ",end="")
s = list(map(int,input().strip().split()))
print("\nString|Frame →\t",end='')
for i in range(capacity):
print(i,end=' ')
print("Fault\n ↓\n")
occurance = [None for i in range(capacity)]
for i in range(len(s)):
if s[i] not in f:
if len(f)<capacity:
f.append(s[i])
else:
for x in range(len(f)):
if f[x] not in s[i+1:]:
f[x] = s[i]
break
else:
occurance[x] = s[i+1:].index(f[x])
else:
f[occurance.index(max(occurance))] = s[i]
fault += 1
pf = 'Yes'
else:
pf = 'No'
print(" %d\t\t"%s[i],end='')
for x in f:
print(x,end=' ')
for x in range(capacity-len(f)):
print(' ',end=' ')
print(" %s"%pf)
print("\nTotal requests: %d\nTotal Page Faults: %d\nFault Rate: %0.2f%%"%(len(s),fault,(fault/len(s))*100))