forked from nabil-rady/CPU-Scheduling-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gui.py
238 lines (196 loc) · 9.26 KB
/
gui.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
from algorithms.roundrobin import roundrobin
from algorithms.priority_np import priority_np
from algorithms.priority_p import priority_p
from algorithms.FCFS import FCFS
from gantt_chart import gantt_chart
from algorithms.SJF_np import SJF
from algorithms.SJF import SJF_P
import tkinter as tk
from tkinter import messagebox
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()