-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot.py
More file actions
82 lines (65 loc) · 2.2 KB
/
plot.py
File metadata and controls
82 lines (65 loc) · 2.2 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
78
79
80
81
82
import matplotlib.pyplot as plt
# Reading data from the file generated by C
try:
with open("output.txt", "r") as file:
data = file.read().split()
# Mapping data to variables
fifo_faults = int(data[0])
lru_faults = int(data[1])
first_frag = int(data[2])
best_frag = int(data[3])
worst_frag = int(data[4])
except FileNotFoundError:
print("Error: output.txt not found. Run the C program first.")
exit()
# --- Plot 1: Page Faults ---
plt.figure(figsize=(10, 5))
plt.subplot(1, 2, 1) # Side-by-side plots
plt.bar(['FIFO', 'LRU'], [fifo_faults, lru_faults], color=['blue', 'green'])
plt.ylabel("Page Faults")
plt.title("Page Replacement Comparison")
# --- Plot 2: Fragmentation ---
plt.subplot(1, 2, 2)
# FIX: Removed the quotes so it uses the variables, not strings
plt.bar(['First Fit', 'Best Fit', 'Worst Fit'], [first_frag, best_frag, worst_frag], color=['orange', 'red', 'purple'])
plt.ylabel("Total Fragmentation")
plt.title("Memory Allocation Comparison")
plt.tight_layout()
# --Page Table--
pages = []
frames = []
valids = []
with open("output.txt", "r") as file:
lines = file.readlines()
# Skip first line (summary)
for line in lines[1:]:
parts = line.strip().split()
# Only process lines with exactly 3 elements
if len(parts) != 3:
continue
# Convert values
p, f, v = int(parts[0]), int(parts[1]), int(parts[2])
pages.append(p)
frames.append(f)
valids.append(v)
if len(pages) == 0:
print("Error: No page table data found. Check output.txt format.")
exit()
# Convert -1 to '-' for display
frame_display = [str(f) if f != -1 else '-' for f in frames]
# Create table data
table_data = []
for i in range(len(pages)):
table_data.append([pages[i], frame_display[i], valids[i]])
# Plot table
fig, ax = plt.subplots(figsize=(6, 10))
ax.axis('tight')
ax.axis('off')
table = ax.table(cellText=table_data,
colLabels=["Page", "Frame", "Valid"],
loc='center')
table.auto_set_font_size(False)
table.set_fontsize(10)
table.scale(1, 1.5)
plt.title("Page Table Visualization", fontsize=14)
plt.show()