-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFixedPartitionSimulator.py
More file actions
378 lines (327 loc) · 14.1 KB
/
Copy pathFixedPartitionSimulator.py
File metadata and controls
378 lines (327 loc) · 14.1 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
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
from PyQt5.QtWidgets import QWidget, QVBoxLayout, QHBoxLayout, QGroupBox, QLabel, QLineEdit, QComboBox, QPushButton, QTextEdit, QMessageBox, QScrollArea, QSpacerItem, QSizePolicy
from PyQt5.QtCore import Qt
from utils import MemoryJob
class FixedPartitionSimulator(QWidget):
def __init__(self, app):
super().__init__()
self.app = app
self.partitions = []
self.jobs = []
self.waiting_queue = []
self.num_partitions = 0
self.num_jobs = 0
self.partition_entries = []
self.job_entries = []
self.resize(1200, 900)
self.setMinimumSize(1000, 800)
self.init_ui()
self.create_initial_widgets()
self.show()
def init_ui(self):
self.setWindowTitle("固定式分区分配仿真")
self.setStyleSheet("""
QWidget {
font-family: Arial;
font-size: 18px;
}
QPushButton {
padding: 12px;
background-color: #2196F3;
color: white;
border-radius: 5px;
font-size: 20px;
}
QPushButton:hover {
background-color: #1976D2;
}
QLineEdit, QComboBox, QTextEdit {
padding: 8px;
border: 1px solid #ccc;
border-radius: 3px;
font-size: 18px;
}
QGroupBox {
font-weight: bold;
margin-top: 15px;
padding: 10px;
font-size: 22px;
}
QLabel {
font-size: 18px;
}
QScrollArea {
border: 1px solid #ccc;
border-radius: 3px;
background-color: #f9f9f9;
}
""")
self.layout = QVBoxLayout()
self.layout.setSpacing(15)
self.setLayout(self.layout)
def create_initial_widgets(self):
self.initial_group = QGroupBox("请输入分区和作业数量")
initial_layout = QVBoxLayout()
initial_layout.setSpacing(10)
part_row = QHBoxLayout()
part_row.addWidget(QLabel("分区数量:"))
self.partition_num_entry = QLineEdit()
self.partition_num_entry.setFixedWidth(200)
part_row.addWidget(self.partition_num_entry)
part_row.addStretch()
initial_layout.addLayout(part_row)
job_row = QHBoxLayout()
job_row.addWidget(QLabel("作业数量:"))
self.job_num_entry = QLineEdit()
self.job_num_entry.setFixedWidth(200)
job_row.addWidget(self.job_num_entry)
job_row.addStretch()
initial_layout.addLayout(job_row)
confirm_btn = QPushButton("确定")
confirm_btn.clicked.connect(self.create_input_widgets)
confirm_btn.setMinimumSize(150, 40)
initial_layout.addWidget(confirm_btn)
self.initial_group.setLayout(initial_layout)
self.layout.addWidget(self.initial_group)
def create_input_widgets(self):
try:
self.num_partitions = int(self.partition_num_entry.text())
self.num_jobs = int(self.job_num_entry.text())
if self.num_partitions <= 0 or self.num_jobs <= 0:
raise ValueError("Number of partitions and jobs must be positive.")
except ValueError:
QMessageBox.critical(self, "Input Error", "Please enter valid positive integers for partitions and jobs.")
return
self.initial_group.deleteLater()
# Main horizontal layout for side-by-side design
main_split_layout = QHBoxLayout()
main_split_layout.setSpacing(15)
# Left side: Inputs and Operations
left_layout = QVBoxLayout()
left_layout.setSpacing(15)
self.input_group = QGroupBox("请输入分区和作业参数")
input_layout = QVBoxLayout()
input_layout.setSpacing(15)
# Combined widget for both sections
combined_widget = QWidget()
combined_layout = QVBoxLayout()
combined_layout.setSpacing(10)
# Partition inputs
combined_layout.addWidget(QLabel("分区参数:", styleSheet="font-weight: bold; font-size: 18px"))
part_widget = QWidget()
part_layout = QVBoxLayout()
part_layout.setSpacing(10)
part_header = QHBoxLayout()
part_header.addWidget(QLabel("分区 ID"))
part_header.addWidget(QLabel("大小 (KB)"))
part_header.addStretch()
part_layout.addLayout(part_header)
self.partition_entries = []
for i in range(self.num_partitions):
row = QHBoxLayout()
row.addWidget(QLabel(f"Partition {i+1}"))
entry = QLineEdit()
entry.setFixedWidth(200)
row.addWidget(entry)
row.addStretch()
part_layout.addLayout(row)
self.partition_entries.append(entry)
part_widget.setLayout(part_layout)
combined_layout.addWidget(part_widget)
# Add vertical spacer between sections
combined_layout.addSpacerItem(QSpacerItem(0, 40, QSizePolicy.Minimum, QSizePolicy.Fixed))
# Job inputs
combined_layout.addWidget(QLabel("作业参数:", styleSheet="font-weight: bold; font-size: 18px"))
job_widget = QWidget()
job_layout = QVBoxLayout()
job_layout.setSpacing(10)
job_header = QHBoxLayout()
job_header.addWidget(QLabel("作业名"))
job_header.addWidget(QLabel("内存大小 (KB)"))
job_header.addStretch()
job_layout.addLayout(job_header)
self.job_entries = []
for i in range(self.num_jobs):
row = QHBoxLayout()
entry_name = QLineEdit()
entry_name.setFixedWidth(200)
entry_name.setText(f"J{i+1}")
row.addWidget(entry_name)
entry_size = QLineEdit()
entry_size.setFixedWidth(200)
row.addWidget(entry_size)
row.addStretch()
job_layout.addLayout(row)
self.job_entries.append((entry_name, entry_size))
job_widget.setLayout(job_layout)
combined_layout.addWidget(job_widget)
combined_widget.setLayout(combined_layout)
combined_scroll = QScrollArea()
combined_scroll.setWidgetResizable(True)
combined_scroll.setWidget(combined_widget)
combined_scroll.setMaximumHeight(500) # Adjusted to fit left side
combined_scroll.setFixedWidth(580) # Slightly less than half to account for spacing
input_layout.addWidget(combined_scroll)
self.input_group.setLayout(input_layout)
left_layout.addWidget(self.input_group)
self.operation_group = QGroupBox("操作")
op_layout = QVBoxLayout()
op_layout.setSpacing(10)
alloc_row = QHBoxLayout()
alloc_row.addWidget(QLabel("选择作业分配:"))
self.job_alloc_combo = QComboBox()
self.job_alloc_combo.setFixedWidth(200)
alloc_row.addWidget(self.job_alloc_combo)
alloc_btn = QPushButton("分配作业")
alloc_btn.clicked.connect(self.allocate_job)
alloc_btn.setMinimumSize(150, 40)
alloc_row.addWidget(alloc_btn)
alloc_row.addStretch()
op_layout.addLayout(alloc_row)
dealloc_row = QHBoxLayout()
dealloc_row.addWidget(QLabel("选择作业释放:"))
self.job_dealloc_combo = QComboBox()
self.job_dealloc_combo.setFixedWidth(200)
dealloc_row.addWidget(self.job_dealloc_combo)
dealloc_btn = QPushButton("释放作业")
dealloc_btn.clicked.connect(self.deallocate_job)
dealloc_btn.setMinimumSize(150, 40)
dealloc_row.addWidget(dealloc_btn)
dealloc_row.addStretch()
op_layout.addLayout(dealloc_row)
back_btn = QPushButton("返回")
back_btn.clicked.connect(self.back)
back_btn.setMinimumSize(150, 40)
op_layout.addWidget(back_btn)
self.operation_group.setLayout(op_layout)
left_layout.addWidget(self.operation_group)
left_layout.addStretch()
main_split_layout.addLayout(left_layout)
# Right side: Simulation Results
self.output_group = QGroupBox("仿真结果")
out_layout = QVBoxLayout()
self.partition_text = QTextEdit()
self.partition_text.setReadOnly(True)
self.partition_text.setFixedHeight(300) # Adjusted for right side
out_layout.addWidget(self.partition_text)
self.waiting_text = QTextEdit()
self.waiting_text.setReadOnly(True)
self.waiting_text.setFixedHeight(150)
out_layout.addWidget(self.waiting_text)
self.log_text = QTextEdit()
self.log_text.setReadOnly(True)
self.log_text.setFixedHeight(200)
out_layout.addWidget(self.log_text)
out_layout.addStretch()
self.output_group.setLayout(out_layout)
main_split_layout.addWidget(self.output_group)
self.layout.addLayout(main_split_layout)
def validate_inputs(self):
self.partitions = []
for i, entry in enumerate(self.partition_entries):
try:
size = int(entry.text())
if size <= 0:
raise ValueError("Partition size must be positive.")
self.partitions.append({"id": i+1, "size": size, "status": "Free", "job": None})
except ValueError:
QMessageBox.critical(self, "Input Error", f"Invalid size for Partition {i+1}.")
return False
self.jobs = []
for i, (entry_name, entry_size) in enumerate(self.job_entries):
name = entry_name.text()
try:
size = int(entry_size.text())
if size <= 0:
raise ValueError("Job memory size must be positive.")
self.jobs.append(MemoryJob(name, size))
except ValueError:
QMessageBox.critical(self, "Input Error", f"Invalid memory size for Job {name}.")
return False
self.waiting_queue = self.jobs.copy()
self.update_comboboxes()
self.display_partition_table()
self.display_waiting_queue()
self.log_text.setText("初始化分区与作业列表.")
return True
def update_comboboxes(self):
self.job_alloc_combo.clear()
alloc_jobs = [job.name for job in self.waiting_queue]
self.job_alloc_combo.addItems(alloc_jobs if alloc_jobs else [""])
self.job_dealloc_combo.clear()
dealloc_jobs = [p["job"].name for p in self.partitions if p["status"] == "Occupied"]
self.job_dealloc_combo.addItems(dealloc_jobs if dealloc_jobs else [""])
def display_partition_table(self):
self.partition_text.setText("分区表:\nID\t大小 (KB)\t状态\t作业\n")
for p in self.partitions:
job_name = p["job"].name if p["status"] == "Occupied" else "-"
self.partition_text.append(f"{p['id']}\t{p['size']}\t\t{p['status']}\t{job_name}")
def display_waiting_queue(self):
self.waiting_text.setText("等待队列:\n")
if self.waiting_queue:
self.waiting_text.append(", ".join(f"{j.name} ({j.memory_size} KB)" for j in self.waiting_queue))
else:
self.waiting_text.append("Empty")
def allocate_job(self):
if not self.jobs:
if not self.validate_inputs():
return
selected_job_name = self.job_alloc_combo.currentText()
if not selected_job_name:
QMessageBox.warning(self, "Warning", "No jobs available to allocate.")
return
job = next((j for j in self.waiting_queue if j.name == selected_job_name), None)
if not job:
QMessageBox.critical(self, "Error", "Selected job not found.")
return
allocated = False
for partition in self.partitions:
if partition["status"] == "Free" and partition["size"] >= job.memory_size:
partition["status"] = "Occupied"
partition["job"] = job
self.waiting_queue.remove(job)
allocated = True
self.log_text.append(f"分配 {job.name} ({job.memory_size} KB) 到 Partition {partition['id']}.")
break
if not allocated:
self.log_text.append(f"无法分配 {job.name} ({job.memory_size} KB): 没有足够的分区.")
self.update_comboboxes()
self.display_partition_table()
self.display_waiting_queue()
def deallocate_job(self):
selected_job_name = self.job_dealloc_combo.currentText()
if not selected_job_name:
QMessageBox.warning(self, "Warning", "No jobs available to deallocate.")
return
deallocated = False
for partition in self.partitions:
if partition["status"] == "Occupied" and partition["job"].name == selected_job_name:
partition["status"] = "Free"
partition["job"] = None
self.log_text.append(f"从 Partition {partition['id']} 释放了 {selected_job_name} .")
deallocated = True
break
if not deallocated:
QMessageBox.critical(self, "Error", "Selected job not found in any partition.")
return
remaining_jobs = self.waiting_queue.copy()
self.waiting_queue = []
for job in remaining_jobs:
allocated = False
for partition in self.partitions:
if partition["status"] == "Free" and partition["size"] >= job.memory_size:
partition["status"] = "Occupied"
partition["job"] = job
self.log_text.append(f"释放后分配 {job.name} ({job.memory_size} KB) 到 Partition {partition['id']}.")
allocated = True
break
if not allocated:
self.waiting_queue.append(job)
self.update_comboboxes()
self.display_partition_table()
self.display_waiting_queue()
def back(self):
self.close()
self.app.show_module_selection()
def closeEvent(self, event):
self.app.show_module_selection()
event.accept()