0
945views
Write a program to calculate page fault by using LRU (least recently used) for the following reference string: 1, 3, 2, 3, 4, 2, 1, 3, 4, 2, 1, 4, 5, 1, 3.

Write a program to calculate page fault by using LRU (least recently used) for the following reference string: 1, 3, 2, 3, 4, 2, 1, 3, 4, 2, 1, 4, 5, 1, 3.

1 Answer
0
22views

Write a program to calculate page fault by using LRU String:- 1 3 2 3 4 2 1 3 4 2 1 4 5 1 3

print("Enter the number of frames: ",end="")
capacity = int(input())
f,st,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")
for i in s:
    if i not in f:
        if len(f)<capacity:
            f.append(i)
            st.append(len(f)-1)
        else:
            ind = st.pop(0)
            f[ind] = i
            st.append(ind)
        pf = 'Yes'
        fault += 1
    else:
        st.append(st.pop(st.index(f.index(i))))
        pf = 'No'
    print("   %d\t\t"%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\n"%(len(s),fault))

Run the code:- https://onlinegdb.com/p5uNfLEJp

Please log in to add an answer.