-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
179 lines (129 loc) · 6.79 KB
/
main.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
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import tkinter as tk
from tkinter import messagebox
from tkinter import messagebox, simpledialog
class MemoryAllocationSimulator:
def __init__(self, master):
self.master = master
self.master.title("Memory Allocation Simulator")
self.memory_size = 0
self.partitions = []
self.memory_blocks = []
self.processes = []
self.get_initial_memory_setup() # For initial memory setup from the user
# left side
self.canvas = tk.Canvas(self.master, height=900, width=800, bg="white")
self.canvas.pack(side=tk.LEFT, padx=10, pady=10)
self.display_memory()
# Take Process size
self.label_process_size = tk.Label(self.master, text="Process Size:")
self.label_process_size.pack(pady=(10, 0))
self.entry_process_size = tk.Entry(self.master)
self.entry_process_size.pack(pady=5)
self.label_algorithm = tk.Label(self.master, text="Allocation Algorithm:")
self.label_algorithm.pack(pady=5)
self.algorithm_var = tk.StringVar()
self.algorithm_var.set("First Fit")
self.algorithm_menu = tk.OptionMenu(self.master, self.algorithm_var, "First Fit", "Best Fit", "Worst Fit", "Next Fit"
)
self.algorithm_menu.pack(pady=5)
self.allocate_button = tk.Button(self.master, text="Allocate", command=self.allocate_memory)
self.allocate_button.pack(pady=5)
def get_initial_memory_setup(self):
self.memory_size = simpledialog.askinteger("Memory Setup", "Enter Total Memory Size (in KB):", parent=self.master)
partition_sizes_str = simpledialog.askstring("Memory Setup", "Enter Partition Sizes (separated by commas):", parent=self.master)
self.partitions = [int(size) for size in partition_sizes_str.split(',')]
if self.memory_size is None or any(size <= 0 for size in self.partitions):
messagebox.showerror("Error", "Invalid input. Please provide valid memory and partition sizes.")
self.master.destroy()
return
start_position = 50
self.memory_blocks = [(size, start_position) for size in self.partitions]
def display_memory(self):
self.canvas.delete("all")
gap = 5
x, y = 50, 50
for i, (size, start) in enumerate(self.memory_blocks):
height = (size / self.memory_size) * 500
end_x, end_y = x + 100, y + height
allocated_height = 0
for process_index, process_size in self.processes:
if process_index == i:
allocated_height = (process_size / size) * height # Height of the allocated portion
self.canvas.create_rectangle(x, y, end_x, y + allocated_height, fill="lightgreen", outline="black")
self.canvas.create_text((x + end_x) / 2, y + allocated_height / 2, text=f"{process_size}Kb", fill="black")
height -= allocated_height
if allocated_height == 0:
self.canvas.create_rectangle(x, y, end_x, end_y, fill="lightblue", outline="black")
self.canvas.create_text((x + end_x) / 2, (y + end_y) / 2, text=f"{size}Kb", fill="black")
else:
remaining_size = max(size - sum(process_size for p_idx, process_size in self.processes if p_idx == i), 0)
self.canvas.create_rectangle(x, y + allocated_height, end_x, end_y, fill="lightblue", outline="black")
self.canvas.create_text((x + end_x) / 2, (y + allocated_height + end_y) / 2, text=f"{remaining_size}Kb", fill="black")
y = end_y + gap
self.canvas.create_text(400, 325, text=f"Memory Size: {self.memory_size}Kb", fill="black") # Total Size
def allocate_memory(self):
process_size = int(self.entry_process_size.get())
algorithm = self.algorithm_var.get()
if process_size <= 0:
messagebox.showerror("Error", "Process size must be greater than 0.")
return
if algorithm == "Best Fit":
block_index = self.best_fit(process_size)
elif algorithm == "First Fit":
block_index = self.first_fit(process_size)
elif algorithm == "Next Fit":
block_index = self.next_fit(process_size)
elif algorithm == "Worst Fit":
block_index = self.worst_fit(process_size)
else:
messagebox.showerror("Error", "Invalid allocation algorithm.")
return
if block_index is not None:
self.processes.append((block_index, process_size))
self.display_memory()
else:
messagebox.showinfo("Info", "Memory allocation failed. No suitable block found.")
def best_fit(self, process_size):
best_fit_index = None
best_fit_remaining_size = float('inf')
for i, (size, start) in enumerate(self.memory_blocks):
if any(process_index == i for process_index, _ in self.processes):
continue # Skip already allocated blocks
remaining_size = size - process_size
if remaining_size >= 0 and remaining_size < best_fit_remaining_size:
best_fit_index = i
best_fit_remaining_size = remaining_size
return best_fit_index
def first_fit(self, process_size):
for i, (size, start) in enumerate(self.memory_blocks):
if any(process_index == i for process_index, _ in self.processes):
continue # Skip already allocated blocks
remaining_size = size - process_size
if remaining_size >= 0:
return i
return None
def next_fit(self, process_size):
if not hasattr(self, 'last_allocated_block'):
self.last_allocated_block = 0
for i in range(len(self.memory_blocks)):
index = (i + self.last_allocated_block) % len(self.memory_blocks)
size, start = self.memory_blocks[index]
if size >= process_size and not any(process_index == index for process_index, _ in self.processes):
self.last_allocated_block = index
return index
return None
def worst_fit(self, process_size):
worst_fit_index = None
max_remaining_size = -1
for i, (size, start) in enumerate(self.memory_blocks):
if any(process_index == i for process_index, _ in self.processes):
continue # Skip already allocated blocks
remaining_size = size - process_size
if remaining_size >= 0 and remaining_size > max_remaining_size:
worst_fit_index = i
max_remaining_size = remaining_size
return worst_fit_index
if __name__ == "__main__":
root = tk.Tk()
app = MemoryAllocationSimulator(root)
root.mainloop()