-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.pyw
411 lines (382 loc) · 13.6 KB
/
main.pyw
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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
import tkinter as tk
import tkinter.messagebox as messagebox
import tkinter.filedialog as filedialog
from math import *
pcbs = []
pcb = {"name":"Default","pc":0,"sp":0,"state":"None","pid":0,"text":[],"data":{},"stack":[],"scheduling":{"arrival":float("inf"),"executed":float("inf"),"chain":float("inf")}}
pcbi = -1
process_selected = -1
instruction = ":"
pids = []
pid_max = 0
t = 0
quantum = 3
def create_process(f):
global pid_max,pcbs
p = {}
pid_max += 1
p["state"] = "new"
p["name"] = f
p["pid"] = pid_max
p["pc"] = 0
p["sp"] = 0
p["data"] = {}
p["text"] = []
p["stack"] = []
p["scheduling"] = {"arrival":t,"executed":0,"chain":0}
pcbs.append(p)
__process_update__()
def __process_switch__(i):
global pcb,pcbi
if pcb["state"]!="terminated":
pcb["state"] = "ready"
pcbi = i
pcb = pcbs[pcbi]
if pcb["state"]!="terminated":
pcb["state"] = "running"
pcb["scheduling"]["chain"] = 0
__process_update__()
def var(name,value=None):
if value!=None:
pcb["data"][name] = value
return pcb["data"][name]
def jmp(label,condition=True):
if condition:
pcb["pc"] = pcb["data"][label]
def call(label):
pcb["stack"].append(pcb["pc"])
pcb["pc"] = pcb["data"][label]
pcb["sp"]+=1
def ret():
pc = pcb["stack"].pop()
pcb["pc"] = pc
pcb["sp"]-=1
def push(value):
pcb["stack"].append(value)
pcb["sp"]+=1
return value
def pop():
pcb["sp"]-=1
return pcb["stack"].pop()
def output(*value,sep=' ',end="\n"):
output_text["state"] = "normal"
for v in value[:-1]:
output_text.insert("end",str(v)+sep)
output_text.insert("end",str(value[-1])+end)
output_text["state"] = "disabled"
def __load_process__(i):
global pcb
p = pcbs[i]
fn = p["name"]
f = open(fn)
contents = f.read()
f.close()
lines = contents.splitlines()
i = 0
while i<len(lines):
line = lines[i].strip()
if line.endswith(':'):
p["data"][line[:-1]] = i
if line!="":
i+=1
else:
lines.pop(i)
p["text"] = lines
p["state"] = "ready"
__process_update__()
def __step__():
global t,instruction,pcb
t+=1
instruction = ":"
while instruction.endswith(':') and pcb["pc"]<len(pcb["text"]):
instruction = pcb["text"][pcb["pc"]].strip()
pcb["pc"]+=1
if pcb["pc"] > len(pcb["text"]) or instruction.endswith(':'):
pcb["state"] = "terminated"
__process_update__()
return None
#print(instruction,pcb["pc"])
eval(instruction)
#print(instruction,pcb["pc"])
pcb["scheduling"]["executed"] += 1
pcb["scheduling"]["chain"] += 1
if pcb["pc"] >= len(pcb["text"]):
pcb["state"] = "terminated"
__process_update__()
def __fcfs__():
global pcbi,pcb
if pid_max == 0:
messagebox.showerror("Processes","No processes")
return None
min_arrival = float("inf")
pi = None
new_processes = False #assume no new processes
for i,p in enumerate(pcbs):
if p["state"] == "new":
__load_process__(i)
new_processes = True
if p["state"] != "terminated":
if p["scheduling"]["arrival"]<min_arrival:
pi = i
min_arrival = p["scheduling"]["arrival"]
if new_processes:
return None
if pi==None:
messagebox.showerror("Processes","All processes terminated")
return None
if pcbi!=pi:
__process_switch__(pi)
pcbi = pi
pcb = pcbs[pi]
__step__()
def __rr__():
global pcbi,pcb
if pid_max == 0:
messagebox.showerror("Processes","No processes")
return None
new_processes = False
for i,p in enumerate(pcbs):
if p["state"]=='new':
__load_process__(i)
new_processes = True
if new_processes:
return None
if pcbi==-1 or pcb["scheduling"]["chain"]>=quantum or pcb["state"]=='terminated':
state = "terminated"
count = 0
i = pcbi
while state=="terminated":
i = (i+1)%pid_max
state = pcbs[i]["state"]
count+=1
if count>pid_max:
messagebox.showerror("Processes","All processes terminated")
return None
__process_switch__(i)
__step__()
running = True
root = tk.Tk()
root.title("Processes")
root.geometry("500x500")
def __root_close__():
global running
root.destroy()
running = False
root.protocol("WM_DELETE_WINDOW",__root_close__)
pcb_f = tk.Frame(root)
pcb_name_label = tk.Label(pcb_f,text="Name")
pcb_name_label.grid(row=1,column=1)
pcb_name = tk.Label(pcb_f,text="")
pcb_name.grid(row=1,column=2)
pcb_state_label = tk.Label(pcb_f,text="State")
pcb_state_label.grid(row=2,column=1)
pcb_state = tk.Label(pcb_f,text="")
pcb_state.grid(row=2,column=2)
pcb_pid_label = tk.Label(pcb_f,text="PID")
pcb_pid_label.grid(row=3,column=1)
pcb_pid = tk.Label(pcb_f,text="")
pcb_pid.grid(row=3,column=2)
pcb_arrival_label = tk.Label(pcb_f,text="Arrival")
pcb_arrival_label.grid(row=4,column=1)
pcb_arrival = tk.Label(pcb_f,text="")
pcb_arrival.grid(row=4,column=2)
pcb_executed_label = tk.Label(pcb_f,text="Executed")
pcb_executed_label.grid(row=5,column=1)
pcb_executed = tk.Label(pcb_f,text="")
pcb_executed.grid(row=5,column=2)
pcb_chain_label = tk.Label(pcb_f,text="Chain")
pcb_chain_label.grid(row=6,column=1)
pcb_chain = tk.Label(pcb_f,text="")
pcb_chain.grid(row=6,column=2)
pcb_pc_label = tk.Label(pcb_f,text="PC")
pcb_pc_label.grid(row=7,column=1)
pcb_pc = tk.Label(pcb_f,text="")
pcb_pc.grid(row=7,column=2)
pcb_sp_label = tk.Label(pcb_f,text="SP")
pcb_sp_label.grid(row=8,column=1)
pcb_sp = tk.Label(pcb_f,text="")
pcb_sp.grid(row=8,column=2)
pcb_instruction_label = tk.Label(pcb_f,text="Instruction")
pcb_instruction_label.grid(row=9,column=1)
pcb_instruction = tk.Label(pcb_f,text="")
pcb_instruction.grid(row=9,column=2)
pcb_f.pack()
processes_window = tk.Toplevel(root)
processes_window.geometry("500x500")
processes_window.title("Processes")
processes_window.withdraw()
def __processes_window_close__():
processes_window.withdraw()
processes_window.protocol("WM_DELETE_WINDOW",__processes_window_close__)
selected_window = tk.Toplevel(root)
selected_window.geometry("500x500")
selected_window.title("Processes")
sel_text = tk.Text(selected_window)
sel_text["state"] = "disabled"
sel_text.grid(row=1,column=1)
sel_data = tk.Text(selected_window)
sel_data["state"] = "disabled"
sel_data.grid(row=2,column=1)
sel_stack = tk.Text(selected_window)
sel_stack["state"] = "disabled"
sel_stack.grid(row=2,column=2)
sel_info = tk.Frame(selected_window)
sel_name_label = tk.Label(sel_info,text="Name")
sel_name_label.grid(row=1,column=1)
sel_name = tk.Label(sel_info,text="")
sel_name.grid(row=1,column=2)
sel_state_label = tk.Label(sel_info,text="State")
sel_state_label.grid(row=2,column=1)
sel_state = tk.Label(sel_info,text="")
sel_state.grid(row=2,column=2)
sel_pid_label = tk.Label(sel_info,text="PID")
sel_pid_label.grid(row=3,column=1)
sel_pid = tk.Label(sel_info,text="")
sel_pid.grid(row=3,column=2)
sel_arrival_label = tk.Label(sel_info,text="Arrival")
sel_arrival_label.grid(row=4,column=1)
sel_arrival = tk.Label(sel_info,text="")
sel_arrival.grid(row=4,column=2)
sel_executed_label = tk.Label(sel_info,text="Executed")
sel_executed_label.grid(row=5,column=1)
sel_executed = tk.Label(sel_info,text="")
sel_executed.grid(row=5,column=2)
sel_chain_label = tk.Label(sel_info,text="Chain")
sel_chain_label.grid(row=6,column=1)
sel_chain = tk.Label(sel_info,text="")
sel_chain.grid(row=6,column=2)
sel_pc_label = tk.Label(sel_info,text="PC")
sel_pc_label.grid(row=7,column=1)
sel_pc = tk.Label(sel_info,text="")
sel_pc.grid(row=7,column=2)
sel_sp_label = tk.Label(sel_info,text="SP")
sel_sp_label.grid(row=8,column=1)
sel_sp = tk.Label(sel_info,text="")
sel_sp.grid(row=8,column=2)
sel_info.grid(row=1,column=2)
selected_window.withdraw()
def __selected_window_close__():
selected_window.withdraw()
selected_window.protocol("WM_DELETE_WINDOW",__selected_window_close__)
output_window = tk.Toplevel(root)
output_window.geometry("500x500")
output_window.title("Output")
output_text = tk.Text(output_window)
output_text["state"] = "disabled"
output_text.pack()
output_window.withdraw()
def __output_window_close__():
output_window.withdraw()
output_window.protocol("WM_DELETE_WINDOW",__output_window_close__)
f = tk.Frame(processes_window)
f.pack()
def __process_update__():
global process_selected,pcb,pcbi,f
sel = process_selected
f.destroy()
f = tk.Frame(processes_window)
pid_column_label = tk.Label(f,text="PID")
pid_column_label.grid(row=1,column=1)
name_column_label = tk.Label(f,text="Name")
name_column_label.grid(row=1,column=2)
state_column_label = tk.Label(f,text="State")
state_column_label.grid(row=1,column=3)
select_column_label = tk.Label(f,text="Show")
select_column_label.grid(row=1,column=4)
for i,p in enumerate(pcbs):
#print(pcb["name"])
pid_label = tk.Label(f,text=str(p["pid"]))
pid_label.grid(row=i+2,column=1)
name_label = tk.Label(f,text=p["name"])
name_label.grid(row=i+2,column=2)
state_label = tk.Label(f,text=p["state"])
state_label.grid(row=i+2,column=3)
select_label = tk.Button(f,text="Select")
select_label.grid(row=i+2,column=4)
def proc_sel(i):
def select():
global process_selected
process_selected = i
__process_update__()
selected_window.deiconify()
#print(i)
return select
select_label["command"] = proc_sel(i)
f.pack()
pcb_name["text"] = pcb["name"]
pcb_state["text"] = pcb["state"]
pcb_pid["text"] = str(pcb["pid"])
pcb_pc["text"] = str(pcb["pc"])
pcb_sp["text"] = str(pcb["sp"])
pcb_arrival["text"] = str(pcb["scheduling"]["arrival"])
pcb_executed["text"] = str(pcb["scheduling"]["executed"])
pcb_chain["text"] = str(pcb["scheduling"]["chain"])
pcb_instruction["text"] = instruction
if sel!=-1:
p = pcbs[sel]
#print(p)
sel_text['state'] = 'normal'
sel_text.delete('1.0','end')
sel_text.insert('end',"Text:\n\n")
if p["state"] == "new":
sel_text.insert('end','Not Loaded')
for i,line in enumerate(p["text"]):
sel_text.insert('end',str(i)+"\t"+line+"\n")
i2 = p["pc"]
while i2<len(p["text"]) and p["text"][i2].endswith(":"):
i2+=1
sel_text.tag_add("pc",str(p["pc"]+3)+'.0',str(i2+3)+'.end')
sel_text.tag_config("pc",background="yellow",foreground="black")
sel_text['state'] = 'disabled'
sel_data['state'] = 'normal'
sel_data.delete('1.0','end')
sel_data.insert('end',"Data size: "+str(len(p["data"].keys()))+"\n\n")
for k,v in p["data"].items():
sel_data.insert('end',k.ljust(20,' ')+str(v)+'\n')
sel_data['state'] = 'disabled'
sel_stack['state'] = 'normal'
sel_stack.delete('1.0','end')
sel_stack.insert('end',"Stack size: "+str(len(p["stack"]))+"\n\n")
for i,v in enumerate(p["stack"]):
sel_stack.insert('end',str(i)+"\t"+str(v)+"\n")
sel_stack['state'] = 'disabled'
sel_name["text"] = p["name"]
sel_state["text"] = p["state"]
sel_pid["text"] = str(p["pid"])
sel_pc["text"] = str(p["pc"])
sel_sp["text"] = str(p["sp"])
sel_arrival["text"] = str(p["scheduling"]["arrival"])
sel_executed["text"] = str(p["scheduling"]["executed"])
sel_chain["text"] = str(p["scheduling"]["chain"])
__process_update__()
menu = tk.Menu(root)
load_menu = tk.Menu(menu,tearoff=0)
def __load_file__():
f = filedialog.askopenfilename()
if f!='':
create_process(f)
load_menu.add_command(label="File",command=__load_file__)
menu.add_cascade(label="Load",menu=load_menu)
view_menu = tk.Menu(root,tearoff=0)
def __processes__():
processes_window.deiconify()
view_menu.add_command(label="Processes",command=__processes__)
def __selected_process__():
selected_window.deiconify()
view_menu.add_command(label="Selected Process",command=__selected_process__)
def __output__():
output_window.deiconify()
view_menu.add_command(label="Output",command=__output__)
menu.add_cascade(label="View",menu=view_menu)
step_f = tk.Frame(root)
step_label = tk.Label(step_f,text="Step")
step_label.grid(row=1,column=1)
fcfs_button = tk.Button(step_f,text="First Come First Serve")
fcfs_button.grid(row=1,column=2)
fcfs_button["command"] = __fcfs__
rr_button = tk.Button(step_f,text="Round Robin")
rr_button.grid(row=1,column=3)
rr_button["command"] = __rr__
step_f.pack()
root["menu"] = menu
root.mainloop()