-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpage_replacement_visualizer.py
More file actions
77 lines (53 loc) · 2.13 KB
/
page_replacement_visualizer.py
File metadata and controls
77 lines (53 loc) · 2.13 KB
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
def fifo_page_replacement(reference_string, frames):
memory = []
page_faults = 0
print("\n--- FIFO (First In First Out) ---")
print("Page\tMemory Frames\t\tPage Fault?")
print("----\t-------------\t\t-----------")
for page in reference_string:
fault_status = "No"
if page not in memory:
page_faults += 1
fault_status = "Yes"
if len(memory) == frames:
memory.pop(0)
memory.append(page)
mem_str = ' '.join(map(str, memory))
print(f"{page}\t[{mem_str:<15}]\t{fault_status}")
return page_faults
def lru_page_replacement(reference_string, frames):
memory = []
page_faults = 0
print("\n--- LRU (Least Recently Used) ---")
print("Page\tMemory Frames\t\tPage Fault?")
print("----\t-------------\t\t-----------")
for page in reference_string:
fault_status = "No"
if page not in memory:
page_faults += 1
fault_status = "Yes"
if len(memory) == frames:
memory.pop(0)
memory.append(page)
else:
memory.remove(page)
memory.append(page)
mem_str = ' '.join(map(str, memory))
print(f"{page}\t[{mem_str:<15}]\t{fault_status}")
return page_faults
if __name__ == "__main__":
frames_input = input("Enter the number of memory frames : ")
frames = int(frames_input)
if frames < 0 or frames > 8:
print("Enter valid number of frames")
pages_input = input("Enter the reference string : ")
reference_string = [int(p) for p in pages_input.split()]
if len(reference_string) == 0 or len(reference_string)>15:
print("Enter valid reference string with less than 15 pages")
print(f"\nReference String: {reference_string}")
print(f"Number of Frames: {frames}")
fifo_faults = fifo_page_replacement(reference_string, frames)
lru_faults = lru_page_replacement(reference_string, frames)
print("\n\n--- Final Page Fault Count ---")
print(f"FIFO Algorithm Total Page Faults: {fifo_faults} ")
print(f"LRU Algorithm Total Page Faults: {lru_faults} ")