12 B Shravan Exp11
12 B Shravan Exp11
Algorithm :
Data Set : Assume Appropriate Data Set
Outcome : 1) FIFO
Code:
def fifo(pages, capacity):
memory = []
page_faults = 0
pages = [7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2]
capacity = 4
OUTPUT:
2) OPTIMAL
CODE:
for i in range(len(pages)):
if pages[i] not in memory:
if len(memory) < capacity:
memory.append(pages[i])
else:
# Find the page not used for the longest time in future
future = pages[i+1:]
index_to_replace = -1
farthest = -1
for j in range(len(memory)):
try:
next_use = future.index(memory[j])
except ValueError:
next_use = float('inf')
memory[index_to_replace] = pages[i]
page_faults += 1
return page_faults
pages = [7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2]
capacity = 4
print("Optimal Page Faults:", optimal(pages, capacity))
OUTPUT:
3) LRU
CODE:
def lru(pages, capacity):
memory = []
recent = []
page_faults = 0
pages = [7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2]
capacity = 4
print("Lru Page Faults:", lru(pages, capacity))
OUTPUT:
References: www.moodle.dbit.in