-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMemory Allocation Simulator.py
More file actions
135 lines (107 loc) · 4.62 KB
/
Memory Allocation Simulator.py
File metadata and controls
135 lines (107 loc) · 4.62 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
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
import tkinter as tk
from tkinter import messagebox, ttk
class MemoryAllocationSimulator:
def __init__(self, root):
self.root = root
self.root.title("Memory Allocation Simulator")
self.root.geometry("700x550")
self.create_widgets()
def create_widgets(self):
title = tk.Label(self.root, text="Memory Allocation Simulator",
font=("Arial", 16, "bold"))
title.pack(pady=10)
# Memory Blocks Input
tk.Label(self.root, text="Enter Memory Block Sizes (comma separated):").pack()
self.block_entry = tk.Entry(self.root, width=60)
self.block_entry.pack(pady=5)
# Process Sizes Input
tk.Label(self.root, text="Enter Process Sizes (comma separated):").pack()
self.process_entry = tk.Entry(self.root, width=60)
self.process_entry.pack(pady=5)
# Allocation Strategy
tk.Label(self.root, text="Select Allocation Strategy:").pack(pady=5)
self.strategy = ttk.Combobox(self.root, state="readonly",
values=["First Fit", "Best Fit", "Worst Fit"])
self.strategy.current(0)
self.strategy.pack(pady=5)
# Allocate Button
allocate_btn = tk.Button(self.root, text="Allocate Memory",
command=self.allocate_memory)
allocate_btn.pack(pady=10)
# Results Area
self.result_text = tk.Text(self.root, height=15, width=80)
self.result_text.pack(pady=10)
def parse_input(self, input_text):
try:
return [int(x.strip()) for x in input_text.split(",")]
except:
return None
def first_fit(self, blocks, processes):
allocation = [-1] * len(processes)
for i in range(len(processes)):
for j in range(len(blocks)):
if blocks[j] >= processes[i]:
allocation[i] = j
blocks[j] -= processes[i]
break
return allocation
def best_fit(self, blocks, processes):
allocation = [-1] * len(processes)
for i in range(len(processes)):
best_index = -1
for j in range(len(blocks)):
if blocks[j] >= processes[i]:
if best_index == -1 or blocks[j] < blocks[best_index]:
best_index = j
if best_index != -1:
allocation[i] = best_index
blocks[best_index] -= processes[i]
return allocation
def worst_fit(self, blocks, processes):
allocation = [-1] * len(processes)
for i in range(len(processes)):
worst_index = -1
for j in range(len(blocks)):
if blocks[j] >= processes[i]:
if worst_index == -1 or blocks[j] > blocks[worst_index]:
worst_index = j
if worst_index != -1:
allocation[i] = worst_index
blocks[worst_index] -= processes[i]
return allocation
def allocate_memory(self):
self.result_text.delete("1.0", tk.END)
blocks = self.parse_input(self.block_entry.get())
processes = self.parse_input(self.process_entry.get())
if blocks is None or processes is None:
messagebox.showerror("Invalid Input",
"Please enter valid comma-separated numbers.")
return
strategy = self.strategy.get()
blocks_copy = blocks.copy()
if strategy == "First Fit":
allocation = self.first_fit(blocks_copy, processes)
elif strategy == "Best Fit":
allocation = self.best_fit(blocks_copy, processes)
else:
allocation = self.worst_fit(blocks_copy, processes)
self.display_results(processes, allocation)
def display_results(self, processes, allocation):
self.result_text.insert(tk.END, "Allocation Results:\n\n")
for i in range(len(processes)):
if allocation[i] != -1:
self.result_text.insert(
tk.END,
f"Process {i + 1} (Size {processes[i]}) "
f"-> Block {allocation[i] + 1}\n"
)
else:
self.result_text.insert(
tk.END,
f"Process {i + 1} (Size {processes[i]}) "
f"-> Not Allocated\n"
)
if __name__ == "__main__":
root = tk.Tk()
app = MemoryAllocationSimulator(root)
root.mainloop()