forked from nabil-rady/CPU-Scheduling-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exe.py
656 lines (524 loc) · 23.6 KB
/
exe.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
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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
import numpy as np
import matplotlib.pyplot as plt
import random
import tkinter as tk
from tkinter import messagebox
def FCFS(processes):
processes=processes.copy()
n=len(processes)
x_ticks = [sorted(processes,key=lambda x:x['arrival_time'])[0]['arrival_time']]
# x_ticks[0] = 0
wt=[]
# wt[0] = 0
total=0
total_time=0
processes_names = []
for i in range(n):
for j in range(i+1,n):
if(processes[i]['arrival_time']>processes[j]['arrival_time']):
processes[i],processes[j] = processes[j],processes[i]
for k in range(n):
x_ticks.append(x_ticks[k] +processes[k]['burst_time'])
wt.append(x_ticks[k] - processes[k]['arrival_time'])
total_time +=processes[k]['burst_time']
processes_names.append(processes[k]['name'])
total += wt[k]
average_waiting_time=total/n
# x_ticks().append(total_time)
return round(average_waiting_time,2),processes_names,x_ticks
def handle_priority_np(processes,time_line_processes,x_ticks,processes_times):
# check if the first arrival is not 0
if len(x_ticks) == 1 and x_ticks[0] > 0:
for i in range(len(processes)):
if processes[i]['arrival_time'] > x_ticks[0]:
processes[i]['arrival_time'] -= x_ticks[0]
else:
processes[i]['arrival_time'] = 0
# get the last executed process and manipulate the gantt chart
prev = processes[0]
processes.pop(0)
time_line_processes.append(prev)
x_ticks.append(x_ticks[len(x_ticks) - 1] + prev['burst_time'])
# calculate waiting time
for i in range(len(processes)):
for p in processes_times:
if p['name'] == processes[i]['name']:
if x_ticks[len(x_ticks) - 2] >= p['arrival_time']:
p['waiting_time'] += prev['burst_time']
elif x_ticks[len(x_ticks) - 1] > p['arrival_time']:
p['waiting_time'] += x_ticks[len(x_ticks) - 1] - p['arrival_time']
# handle the different arrival times
for i in range(len(processes)):
if processes[i]['arrival_time'] > 0:
if prev['burst_time'] >= processes[i]['arrival_time']:
processes[i]['arrival_time'] = 0
else:
processes[i]['arrival_time'] -= prev['burst_time']
processes = sorted(processes, key=lambda k: (k['arrival_time'], k['priority'], k['burst_time']))
return processes
def priority_np(processes):
for i in range(len(processes)):
processes[i] = processes[i].copy()
time_line_processes = []
processes_times = []
for process in processes:
p = {
'name': process['name'],
'arrival_time': process['arrival_time'],
'burst_time': process['burst_time'],
'waiting_time': 0
}
processes_times.append(p)
# make sure that the processes is sorted base on their priority
processes = sorted(processes, key=lambda k: (k['arrival_time'], k['priority'], k['burst_time']))
x_ticks = [processes[0]['arrival_time']] # Shift time to lowest arrival time
n = len(processes)
i = n
# for counter in range(n):
# i += processes[counter]['burst_time']
# burst_total = i
while i > 0:
processes = handle_priority_np(processes,time_line_processes,x_ticks,processes_times)
i-=1
total_waiting_time = 0
for p in processes_times:
total_waiting_time += p['waiting_time']
average_waiting_time = total_waiting_time / n
processes_names = []
for i in range(0, len(time_line_processes)):
processes_names.append(time_line_processes[i]['name'])
return round(average_waiting_time, 2), processes_names, np.asarray(x_ticks)
def handle_priority_p(processes,time_line_processes,x_ticks,processes_times):
# check if the first arrival is not 0
if len(x_ticks) == 1 and x_ticks[0] > 0:
for i in range(len(processes)):
if processes[i]['arrival_time'] > x_ticks[0]:
processes[i]['arrival_time'] -= x_ticks[0]
else:
processes[i]['arrival_time'] = 0
# get the last executed process
processes[0]['burst_time'] -= 1
prev = processes[0]
# manipulate the gantt chart
if len(time_line_processes) == 0 or prev['name'] != time_line_processes[len(time_line_processes) - 1]['name']:
time_line_processes.append(prev)
x_ticks.append(x_ticks[len(x_ticks) - 1] + 1)
else:
x_ticks[len(x_ticks) - 1] += 1
# calculate waiting time
for i in range(1, len(processes)):
if processes[i]['arrival_time'] == 0:
for p in processes_times:
if p['name'] == processes[i]['name']:
p['waiting_time'] += 1
break
# if the process has finished
if prev['burst_time'] == 0:
processes.pop(0)
# handle the different arrival times
for i in range(len(processes)):
if processes[i]['arrival_time'] > 0:
processes[i]['arrival_time'] -= 1
processes = sorted(processes, key=lambda k: (k['arrival_time'], k['priority'], k['burst_time']))
return processes
def priority_p(processes):
for i in range(len(processes)):
processes[i] = processes[i].copy()
time_line_processes = []
processes_times = []
for process in processes:
p = {
'name': process['name'],
'arrival_time': process['arrival_time'],
'burst_time': process['burst_time'],
'waiting_time': 0
}
processes_times.append(p)
# make sure that the processes is sorted base on their priority
processes = sorted(processes, key=lambda k: (k['arrival_time'], k['priority'], k['burst_time']))
x_ticks = [processes[0]['arrival_time']] # Shift time to lowest arrival time
n = len(processes)
i = 0
for counter in range(n):
i += processes[counter]['burst_time']
while i > 0:
processes = handle_priority_p(processes,time_line_processes,x_ticks,processes_times)
i-=1
total_waiting_time = 0
for p in processes_times:
total_waiting_time += p['waiting_time']
average_waiting_time = total_waiting_time / n
processes_names = []
for i in range(0, len(time_line_processes)):
processes_names.append(time_line_processes[i]['name'])
return round(average_waiting_time, 2), processes_names, np.asarray(x_ticks)
# Function to check if all processes are done
def prcoesses_done(burst_times):
for burst_time in burst_times:
if burst_time>0:
return False
return True
def roundrobin(processes,time_quantum):
burst_times = [process['burst_time'] for process in processes]
arrival_times = [process['arrival_time'] for process in processes]
number_of_processes = len(burst_times)
current_time = min(arrival_times)
counter = 0
burst_times = burst_times.copy()
x_ticks = [current_time] # This variable represents ticks on the X-axis
processes_names = list() # This variable represents the process on the respective X-axis tick
waiting_time = [0]*number_of_processes # Waiting time for each process
last_time_checked = arrival_times.copy() # Last time the process was active or the time of arrival
while not prcoesses_done(burst_times):
# Check if process is not finished
if (burst_times[counter]!=0):
if current_time >= arrival_times[counter]:
# Check if time quantum is smaller than process burst time
if (burst_times[counter]>=time_quantum):
x_ticks.append(current_time+time_quantum)
waiting_time[counter]+=current_time-last_time_checked[counter]
current_time+=time_quantum
burst_times[counter]-=time_quantum
processes_names.append(processes[counter]['name'])
last_time_checked[counter]=current_time
else:
x_ticks.append(current_time+burst_times[counter])
waiting_time[counter]+=current_time-last_time_checked[counter]
current_time+=burst_times[counter]
burst_times[counter]=0
processes_names.append(processes[counter]['name'])
last_time_checked[counter]=current_time
counter = (counter+1)%number_of_processes
average_waiting_time = sum(waiting_time)/len(waiting_time)
return round(average_waiting_time, 2), processes_names, np.asarray(x_ticks)
def SJF(processes):
number_of_processes = len(processes)
arrival_times = [process['arrival_time'] for process in processes]
shift_time = min(arrival_times)
x_ticks = [shift_time]
counter = 0
waiting_time=0
total_waiting_time=0
#sort the processes based on their arrival time
processes = sorted(processes, key=lambda k: (k['arrival_time']))
for process in range (number_of_processes):
for p in range (process, number_of_processes):
if shift_time >= processes[p]['arrival_time']:
processes[p]['arrival_time'] = counter
counter += 1
#sort the processes based on their arrival & burst times
processes = sorted(processes, key=lambda k: (k['arrival_time'], k['burst_time']))
#calculate x_ticks
shift_time += processes[process]['burst_time']
x_ticks.append(shift_time)
#calculate average waiting time
if process == 0:
waiting_time = 0
else:
waiting_time += processes[process-1]['burst_time']
total_waiting_time += waiting_time
average_waiting_time = total_waiting_time/number_of_processes
counter = 0
#get processes names after sorting them
processes_names = [process['name'] for process in processes]
return round(average_waiting_time, 2), processes_names, np.asarray(x_ticks)
def handle_SJF(processes,time_line_processes,x_ticks,processes_times):
# check if the first arrival is not 0
if len(x_ticks) == 1 and x_ticks[0] > 0:
for i in range(len(processes)):
if processes[i]['arrival_time'] > x_ticks[0]:
processes[i]['arrival_time'] -= x_ticks[0]
else:
processes[i]['arrival_time'] = 0
# get the last executed process
processes[0]['burst_time'] -= 1
prev = processes[0]
# manipulate the gantt chart
if len(time_line_processes) == 0 or prev['name'] != time_line_processes[len(time_line_processes) - 1]['name']:
time_line_processes.append(prev)
x_ticks.append(x_ticks[len(x_ticks) - 1] + 1)
else:
x_ticks[len(x_ticks) - 1] += 1
# calculate waiting time
for i in range(1, len(processes)):
if processes[i]['arrival_time'] == 0:
for p in processes_times:
if p['name'] == processes[i]['name']:
p['waiting_time'] += 1
break
# if the process has finished
if prev['burst_time'] == 0:
processes.pop(0)
# handle the different arrival times
for i in range(len(processes)):
if processes[i]['arrival_time'] > 0:
processes[i]['arrival_time'] -= 1
processes = sorted(processes, key=lambda k: (k['arrival_time'], k['burst_time']))
return processes
def SJF_P(processes):
for i in range(len(processes)):
processes[i] = processes[i].copy()
time_line_processes = []
processes_times = []
for process in processes:
p = {
'name': process['name'],
'arrival_time': process['arrival_time'],
'burst_time': process['burst_time'],
'waiting_time': 0
}
processes_times.append(p)
# make sure that the processes is sorted base on their priority
processes = sorted(processes, key=lambda k: (k['arrival_time'], k['burst_time']))
x_ticks = [processes[0]['arrival_time']] # Shift time to lowest arrival time
n = len(processes)
i = 0
for counter in range(n):
i += processes[counter]['burst_time']
while i > 0:
processes = handle_SJF(processes,time_line_processes,x_ticks,processes_times)
i-=1
total_waiting_time = 0
for p in processes_times:
total_waiting_time += p['waiting_time']
average_waiting_time = total_waiting_time / n
processes_names = []
for i in range(0, len(time_line_processes)):
processes_names.append(time_line_processes[i]['name'])
return round(average_waiting_time, 2), processes_names, np.asarray(x_ticks)
# colors
colors = ['black' , 'aqua' , 'orange','teal' , 'chocolate','maroon','darkmagenta','gold','orchid' , 'green' , 'palegreen' , 'gray' , 'greenyellow' ,'yellow' , 'deeppink' ,'darkviolet' , 'blue' , 'darkblue' ,'darkcyan' , 'crimson' ,'red', 'olive','turquoise']
def assign_colors(processes):
colors_of_processes = dict()
used_colors = dict()
for process in processes:
color = random.choice(colors)
while color in used_colors:
color = random.choice(colors)
used_colors[color] = None
colors_of_processes[process] = color
return colors_of_processes
def gantt_chart(processes, x_ticks):
# Declaring a figure "gnt"
fig, gnt = plt.subplots()
processes_names = sorted(list(set(processes)))
colors_of_processes = assign_colors(processes)
processes_start = []
for i in range(len(processes_names)):
p = {
'name': processes_names[i],
'start': i + 1
}
processes_start.append(p)
# Setting labels for x-axis and y-axis
gnt.set_xlabel('Time')
gnt.set_ylabel('Processes line')
gnt.set_ylim(0, 3)
# Setting ticks on x-axis
gnt.set_xticks([2*i for i in range(max(x_ticks) + 4)])
# gnt.set_yticks([0.5 * i for i in range(1,len(processes_names)+ 1)])
gnt.set_yticks([1,2,3])
# Setting graph attribute
gnt.grid(True)
facecolors = [colors_of_processes[process_name] for process_name in processes]
# Declaring a bar in schedule
for i in range(len(processes)):
start = 0
for process_start in processes_start:
if process_start['name'] == processes[i]:
start = process_start['start']
break
# gnt.broken_barh([(x_ticks[i], x_ticks[i + 1]-x_ticks[i])], (0.5*(start-len(processes_names)/20), len(processes_names)/20), color = facecolors[i], label='p'+str(i+1))
gnt.broken_barh([(x_ticks[i], x_ticks[i + 1]-x_ticks[i])],(0.75,0.5), color = facecolors[i], label=processes[i])
# plt.legend(facecolors[i],labels='p'+str(i+1))
plt.legend(loc='upper right')
# gnt.set_yticklabels(processes_names)
print(processes)
plt.show()
root = tk.Tk()
root.title('CPU scheduling algorithms')
root.geometry("500x500")
# Storing all CPU scheduling algorithms in a list.
scheduling_algorithms = [
"First-Come, First-Served",
"Shorted-Job-First Preemptive",
"Shorted-Job-First Non Preemptive",
"Round Robin",
"Priority Preemptive",
"Priority Non Preemptive"
]
algorithm_functions = {
"First-Come, First-Served" : FCFS,
"Shorted-Job-First Preemptive" : SJF_P,
"Shorted-Job-First Non Preemptive" : SJF,
"Round Robin" : roundrobin,
"Priority Preemptive" : priority_p,
"Priority Non Preemptive" : priority_np,
}
# This list will contain widgets to be deleted
global widgets
widgets = []
def show_widgets(event):
global widgets
for widget in widgets[:]:
widget.destroy()
widgets.remove(widget)
# storing process information in a list of dictionary
process_details = []
# Frame for the process number.
process_frame = tk.LabelFrame(root, padx=5, pady=5)
# Frame for the information/detials of each process.
process_details_frame = tk.LabelFrame(root, padx=10, pady=10)
# Frame for the algorithm waiting time.
waiting_time_frame = tk.LabelFrame(root, padx=5, pady=5)
# Input field for the number of avaliable processes.
process_name_label = tk.Label(process_frame, text = 'Enter number of processes: ').grid(row=0, column=0, padx=5)
process_number_entry = tk.Entry(process_frame, width=15, borderwidth=5)
process_number_entry.grid(row=0, column=1, padx=5)
# Input field for time quantum in case of round robin.
global time_quantum_entry
if selected_algorithm.get() == "Round Robin":
time_quantum_label = tk.Label(process_frame, text = 'Enter time quantum:').grid(row=1, column=0, padx=5)
time_quantum_entry = tk.Entry(process_frame, width=15, borderwidth=5)
time_quantum_entry.grid(row=1, column=1, padx=5)
global count
count = 1
def process_details_widgets():
# display process number
global process_number_label
process_number_label = tk.Label(process_details_frame, text = 'Process #{}'.format(count))
process_number_label.grid(row=0, column=1)
# entry for process name
process_name_label = tk.Label(process_details_frame, text = 'Enter process name:').grid(row=1, column=0, padx=5)
global process_name_entry
process_name_entry = tk.Entry(process_details_frame, width=15, borderwidth=5)
process_name_entry.grid(row=1, column=1, padx=5)
# entry for arrival time
arrival_time_label = tk.Label(process_details_frame, text = 'Enter process arrival time:').grid(row=2, column=0, padx=5)
global arrival_time_entry
arrival_time_entry = tk.Entry(process_details_frame, width=15, borderwidth=5)
arrival_time_entry.grid(row=2, column=1, padx=5)
# entry for brust time
burst_time_label = tk.Label(process_details_frame, text = 'Enter process brust time:').grid(row=3, column=0, padx=5)
global burst_time_entry
burst_time_entry = tk.Entry(process_details_frame, width=15, borderwidth=5)
burst_time_entry.grid(row=3, column=1, padx=5)
#entry for process priority
global priority_entry
if selected_algorithm.get() == "Priority Non Preemptive" or selected_algorithm.get() == "Priority Preemptive":
burst_time_label = tk.Label(process_details_frame, text = 'Enter process priority:').grid(row=4, column=0, padx=5)
priority_entry = tk.Entry(process_details_frame, width=15, borderwidth=5)
priority_entry.grid(row=4, column=1, padx=5)
process_details_button = tk.Button(process_details_frame, text="submit", command=submit_process_details)
process_details_button.grid(row=5, column=1, pady=10)
def call_gantt_chart(processes, x_ticks):
gantt_chart(processes,x_ticks)
def execution_gantt_chart():
algorithm_function = algorithm_functions[selected_algorithm.get()]
if algorithm_function.__name__ == 'roundrobin':
average_waiting_time,processes,x_ticks = algorithm_function(process_details, int(time_quantum) )
else:
average_waiting_time,processes,x_ticks = algorithm_function(process_details)
average_waiting_time_label = tk.Label(waiting_time_frame, text='Waiting time = {}'.format(average_waiting_time)).grid(row=0, column=0, padx=5)
execution_button = tk.Button(waiting_time_frame, text="View Gantt Chart", command= lambda:call_gantt_chart(processes, x_ticks))
execution_button.grid(row = 1, column=1, pady=10)
waiting_time_frame.pack()
def delete_details_frame():
process_details_frame.pack_forget()
process_details_frame.destroy()
execution_gantt_chart()
def clearInputEntries():
process_name_entry.delete(0, 'end')
arrival_time_entry.delete(0, 'end')
burst_time_entry.delete(0, 'end')
if selected_algorithm.get() == "Priority Non Preemptive" or selected_algorithm.get() == "Priority Preemptive":
priority_entry.delete(0, 'end')
def isValidInput():
arrival_time = arrival_time_entry.get()
burst_time = burst_time_entry.get()
if not arrival_time.isdigit():
response = messagebox.showerror("Error", "Enter a positive number for the arrival time.")
if response == 'ok':
return False
elif not burst_time.isdigit():
response = messagebox.showerror("Error", "Enter a positive number for the brust time.")
if response == 'ok':
return False
elif selected_algorithm.get() == "Priority Non Preemptive" or selected_algorithm.get() == "Priority Preemptive":
priority = priority_entry.get()
if not priority.isdigit():
response = messagebox.showerror("Error", "Enter a positive number for the process priority.")
if response == 'ok':
return False
return True
def submit_process_details():
global count
count += 1
if not isValidInput():
count -= 1
clearInputEntries()
return
name = process_name_entry.get()
arrival_time = arrival_time_entry.get()
burst_time = burst_time_entry.get()
if selected_algorithm.get() == "Priority Non Preemptive" or selected_algorithm.get() == "Priority Preemptive":
priority = priority_entry.get()
process_details.append (
{
'name' : name,
'arrival_time' : int(arrival_time),
'burst_time' : int (burst_time),
'priority' : int(priority)
}
)
else :
process_details.append (
{
'name' : name,
'arrival_time' : int(arrival_time),
'burst_time' : int (burst_time)
}
)
process_number_label.config(text='Process #{}'.format(count))
clearInputEntries()
# Testing that process fields are stored in process_details list
print(process_details)
if count > process_number:
delete_details_frame()
return
# Submit button for the entered process number.
def submit_process_number():
global process_number
global time_quantum
process_number = process_number_entry.get()
if selected_algorithm.get() == "Round Robin":
time_quantum = time_quantum_entry.get()
if not process_number.isdigit():
response = messagebox.showerror("Error", "Number of processes must be integer.")
if response == 'ok':
process_number_entry.delete(0, 'end')
if selected_algorithm.get() == "Round Robin":
time_quantum_entry.delete(0, 'end')
return
elif selected_algorithm.get() == "Round Robin":
if not time_quantum.isdigit():
response = messagebox.showerror("Error", "Enter a positive number for the time quantum.")
if response == 'ok':
process_number_entry.delete(0, 'end')
time_quantum_entry.delete(0, 'end')
return
process_number = int(process_number)
process_frame.pack_forget()
process_frame.destroy()
process_details_widgets()
process_number_button = tk.Button(process_frame, text="submit", command=submit_process_number)
process_number_button.grid(row = 2, column=1, pady=10)
widgets = widgets[:] + [process_frame, process_details_frame, waiting_time_frame]
for widget in widgets:
widget.pack()
# Drop Down Menu
selected_algorithm = tk.StringVar()
selected_algorithm.set("Select an algorithm")
drop_menu = tk.OptionMenu(root, selected_algorithm , *scheduling_algorithms, command=show_widgets)
drop_menu.pack(pady=10)
root.mainloop()