-
Notifications
You must be signed in to change notification settings - Fork 0
/
CPUScheduling1 - Copy.py
450 lines (426 loc) · 21.2 KB
/
CPUScheduling1 - Copy.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
from tkinter import *
import random
def app():
def unpack_all():
c.pack_forget()
fcfs.pack_forget()
sjn.pack_forget()
pbs.pack_forget()
rr.pack_forget()
ltr.pack_forget()
mlq.pack_forget()
l1.pack_forget()
l2.pack_forget()
l3.pack_forget()
l5.pack_forget()
l6.pack_forget()
l7.pack_forget()
destroy.pack_forget()
def fcfs():
unpack_all()
n = random.randint(3, 10)
arrivalTime = [i for i in range(n)]
execTime = [random.randint(1, 10) for i in range(n)]
process = ['P' + str(i) for i in range(n)]
f.pack()
Label(f, text="\nFirst Come First Serve\n", font=('Bodoni', 20)).pack()
Label(f,
text="First come first serve (FCFS) scheduling algorithm simply schedules the jobs according to their arrival time. ",
font=('TimesNewRoman', 16)).pack()
Label(f, text="The lesser the arrival time of the job, the sooner will the job get the CPU. ",
font=('TimesNewRoman', 16)).pack()
Label(f,
text=" FCFS scheduling may cause the problem of starvation if the burst time of the first process is the longest among all the jobs.",
font=('TimesNewRoman', 16)).pack()
Label(f, text="The job which comes first in the ready queue will get the CPU first.",
font=('TimesNewRoman', 16)).pack()
Label(f, text="Arrival Time: " + str(arrivalTime), font=('Robinson Typeface', 12)).pack(anchor=W)
Label(f, text="Process: " + str(process), font=('Robinson Typeface', 12)).pack(anchor=W)
Label(f, text="Burst Time: " + str(execTime), font=('Robinson Typeface', 12)).pack(anchor=W)
for i in range(n):
for j in range(i, n):
if arrivalTime[i] > arrivalTime[j]:
temp = arrivalTime[i]
arrivalTime[i] = arrivalTime[j]
arrivalTime[j] = temp
temp = execTime[i]
execTime[i] = execTime[j]
execTime[j] = temp
temp = process[i]
process[i] = process[j]
process[j] = temp
CompletionTime = list()
TatTime = list()
CompletionTime.append(execTime[0] + arrivalTime[0])
for i in range(1, n):
CompletionTime.append(execTime[i] + CompletionTime[i - 1])
for i in range(0, n):
TatTime.append(CompletionTime[i] - arrivalTime[i])
waitTime = [TatTime[i] - execTime[i] for i in range(n)]
for i in range(len(waitTime)):
if waitTime[i] < 0:
waitTime[i] = 0
avg = sum(waitTime) / float(n)
Label(f, text="\n----- After Performing First Come First Serve Scheduling Algorithm -----\n",
font=('Robinson Typeface', 12)).pack(anchor=W)
Label(f, text="Arrival Time: " + str(arrivalTime), font=('Robinson Typeface', 12)).pack(anchor=W)
Label(f, text="Process: " + str(process), font=('Robinson Typeface', 12)).pack(anchor=W)
Label(f, text="Burst Time: " + str(execTime), font=('Robinson Typeface', 12)).pack(anchor=W)
Label(f, text="Completion Time: " + str(CompletionTime), font=('Robinson Typeface', 12)).pack(anchor=W)
Label(f, text="Turn Around Time: " + str(TatTime), font=('Robinson Typeface', 12)).pack(anchor=W)
Label(f, text="Wait Time: " + str(waitTime), font=('Robinson Typeface', 12)).pack(anchor=W)
Label(f, text="Average Wait Time: " + str(avg), font=('Robinson Typeface', 12)).pack(anchor=W)
l1 = Label(root, text='')
l1.pack()
runOther.pack(padx=120, side=LEFT)
destroy.pack(padx=10, side=LEFT)
def sjn():
unpack_all()
n = random.randint(3, 10)
arrivalTime = [i for i in range(n)]
execTime = [random.randint(1, 10) for i in range(n)]
process = ['P' + str(i) for i in range(n)]
f = Frame(root)
f.pack()
Label(f, text="\nShortest Job First\n", font=('Bodoni', 20)).pack()
Label(f,
text="Shortest Job First (SJF) is an algorithm in which the process having the smallest execution time is chosen for the next execution.",
font=('TimesNewRoman', 16)).pack()
Label(f, text="This scheduling method can be preemptive or non-preemptive. ", font=('TimesNewRoman', 16)).pack()
Label(f, text="It significantly reduces the average waiting time for other processes awaiting execution.",
font=('TimesNewRoman', 16)).pack()
Label(f, text="Arrival Time: " + str(arrivalTime), font=('Robinson Typeface', 12)).pack(anchor=W)
Label(f, text="Burst Time: " + str(execTime), font=('Robinson Typeface', 12)).pack(anchor=W)
Label(f, text="Process: " + str(process), font=('Robinson Typeface', 12)).pack(anchor=W)
for i in range(n):
for j in range(i, n):
if execTime[i] > execTime[j]:
temp = arrivalTime[i]
arrivalTime[i] = arrivalTime[j]
arrivalTime[j] = temp
temp = execTime[i]
execTime[i] = execTime[j]
execTime[j] = temp
temp = process[i]
process[i] = process[j]
process[j] = temp
elif execTime[i] == execTime[j]:
if arrivalTime[i] > arrivalTime[j]:
temp = arrivalTime[i]
arrivalTime[i] = arrivalTime[j]
arrivalTime[j] = temp
temp = execTime[i]
execTime[i] = execTime[j]
execTime[j] = temp
temp = process[i]
process[i] = process[j]
process[j] = temp
CompletionTime = list()
TatTime = list()
CompletionTime.append(execTime[0] + arrivalTime[0])
for i in range(1, n):
if CompletionTime[i - 1] < arrivalTime[i]:
CompletionTime.append(execTime[i] + arrivalTime[i])
else:
CompletionTime.append(execTime[i] + CompletionTime[i - 1])
for i in range(0, n):
TatTime.append(CompletionTime[i] - arrivalTime[i])
waitTime = [TatTime[i] - execTime[i] for i in range(n)]
for i in range(len(waitTime)):
if waitTime[i] < 0:
waitTime[i] = 0
avg = sum(waitTime) / float(n)
Label(f, text="\n----- After Performing Shortest Job First Scheduling Algorithm -----\n",
font=('Robinson Typeface', 12)).pack(anchor=W)
Label(f, text="Arrival Time: " + str(arrivalTime), font=('Robinson Typeface', 12)).pack(anchor=W)
Label(f, text="Burst Time: " + str(execTime), font=('Robinson Typeface', 12)).pack(anchor=W)
Label(f, text="Process: " + str(process), font=('Robinson Typeface', 12)).pack(anchor=W)
Label(f, text="Completion Time: " + str(CompletionTime), font=('Robinson Typeface', 12)).pack(anchor=W)
Label(f, text="Turn Around Time: " + str(TatTime), font=('Robinson Typeface', 12)).pack(anchor=W)
Label(f, text="Wait Time: " + str(waitTime), font=('Robinson Typeface', 12)).pack(anchor=W)
Label(f, text="Average Wait Time: " + str(avg), font=('Robinson Typeface', 12)).pack(anchor=W)
l1 = Label(root, text='')
l1.pack()
runOther.pack(padx=120, side=LEFT)
destroy.pack(padx=10, side=LEFT)
def pbs():
unpack_all()
n = random.randint(3, 10)
arrivalTime = [i for i in range(n)]
execTime = [random.randint(1, 10) for i in range(n)]
process = ['P' + str(i) for i in range(n)]
priority = [random.randint(0, n) for i in range(n)]
f = Frame(root)
f.pack()
Label(f, text="\nPriority Based Scheduling\n", font=('Bodoni', 20)).pack()
Label(f, text="Priority Scheduling is a method of scheduling processes that is based on priority.",
font=('TimesNewRoman', 16)).pack()
Label(f, text=" In this algorithm, the scheduler selects the tasks to work as per the priority.",
font=('TimesNewRoman', 16)).pack()
Label(f, text=" Priority depends upon memory requirements, time requirements, etc.",
font=('TimesNewRoman', 16)).pack()
Label(f, text="Arrival Time: " + str(arrivalTime), font=('Robinson Typeface', 12)).pack(anchor=W)
Label(f, text="Process: " + str(process), font=('Robinson Typeface', 12)).pack(anchor=W)
Label(f, text="Priority: " + str(priority), font=('Robinson Typeface', 12)).pack(anchor=W)
Label(f, text="Burst Time: " + str(execTime), font=('Robinson Typeface', 12)).pack(anchor=W)
for i in range(n):
for j in range(i, n):
if priority[i] < priority[j]:
temp = arrivalTime[i]
arrivalTime[i] = arrivalTime[j]
arrivalTime[j] = temp
temp = execTime[i]
execTime[i] = execTime[j]
execTime[j] = temp
temp = process[i]
process[i] = process[j]
process[j] = temp
temp = priority[i]
priority[i] = priority[j]
priority[j] = temp
CompletionTime = list()
TatTime = list()
CompletionTime.append(execTime[0] + arrivalTime[0])
for i in range(1, n):
if arrivalTime[i] > CompletionTime[i - 1]:
CompletionTime.append(execTime[i] + arrivalTime[i - 1])
else:
CompletionTime.append(execTime[i] + CompletionTime[i - 1])
for i in range(0, n):
TatTime.append(CompletionTime[i] - arrivalTime[i])
waitTime = [TatTime[i] - execTime[i] for i in range(n)]
for i in range(len(waitTime)):
if waitTime[i] < 0:
waitTime[i] = 0
avg = sum(waitTime) / float(n)
Label(f, text="\n----- After Performing Priority based Scheduling Algorithm -----\n",
font=('Robinson Typeface', 12)).pack(anchor=W)
Label(f, text="Arrival Time: " + str(arrivalTime), font=('Robinson Typeface', 12)).pack(anchor=W)
Label(f, text="Process: " + str(process), font=('Robinson Typeface', 12)).pack(anchor=W)
Label(f, text="Burst Time: " + str(execTime), font=('Robinson Typeface', 12)).pack(anchor=W)
Label(f, text="Priority: " + str(priority), font=('Robinson Typeface', 12)).pack(anchor=W)
Label(f, text="Completion Time: " + str(CompletionTime), font=('Robinson Typeface', 12)).pack(anchor=W)
Label(f, text="Turn around Time: " + str(TatTime), font=('Robinson Typeface', 12)).pack(anchor=W)
Label(f, text="Wait Time: " + str(waitTime), font=('Robinson Typeface', 12)).pack(anchor=W)
Label(f, text="Average Wait Time: " + str(avg), font=('Robinson Typeface', 12)).pack(anchor=W)
l1 = Label(root, text='')
l1.pack()
runOther.pack(padx=120, side=LEFT)
destroy.pack(padx=10, side=LEFT)
def rr():
unpack_all()
n = random.randint(3, 10)
arrivalTime = [i for i in range(n)]
execTime = [random.randint(1, 10) for i in range(n)]
process = ['P' + str(i) for i in range(n)]
quantom = random.randint(2, 5)
burstTime = [0 for _ in range(n)]
d = arrivalTime[:]
e = execTime[:]
p = process[:]
f = Frame(root)
f.pack()
Label(f, text="\nRound Robin Schduling\n", font=('Helveltika bold', 12)).pack()
Label(f, text="Arrival Time: " + str(arrivalTime)).pack(anchor=W)
Label(f, text="Process: " + str(process)).pack(anchor=W)
Label(f, text="Quantom: " + str(quantom)).pack(anchor=W)
Label(f, text="Burst Time: " + str(execTime)).pack(anchor=W)
flag = False
cnt = burstTime[:]
while len(d) > 0:
flag = True
if e[0] > quantom:
flag = False
e[0] -= quantom
e.append(e[0])
p.append(p[0])
d.append(d[0])
del d[0]
if flag:
for i in d:
cnt[i] += e[0]
else:
for i in d:
cnt[i] += quantom
del p[0]
del e[0]
for x in arrivalTime:
t = execTime[x] % quantom
if t == 0:
burstTime[x] = cnt[x] + quantom
else:
burstTime[x] = cnt[x] + t
TatTime = list()
for i in range(0, n):
TatTime.append(burstTime[i] - arrivalTime[i])
waitTime = [TatTime[i] - execTime[i] for i in range(n)]
for i in range(len(waitTime)):
if waitTime[i] < 0:
waitTime[i] = 0
avg = sum(waitTime) / float(n)
Label(f, text="\n----- After Performing Round Robin Scheduling Algorithm -----\n").pack(anchor=W)
Label(f, text="Arrival Time: " + str(arrivalTime)).pack(anchor=W)
Label(f, text="Process: " + str(process)).pack(anchor=W)
Label(f, text="Quantom: " + str(quantom)).pack(anchor=W)
Label(f, text="Burst Time: " + str(execTime)).pack(anchor=W)
Label(f, text="Completion Time: " + str(burstTime)).pack(anchor=W)
Label(f, text="Turn Around Time: " + str(TatTime)).pack(anchor=W)
Label(f, text="Wait Time: " + str(waitTime)).pack(anchor=W)
Label(f, text="Average Wait Time: " + str(avg)).pack(anchor=W)
l1 = Label(root, text='')
l1.pack()
runOther.pack(padx=120, side=LEFT)
destroy.pack(padx=10, side=LEFT)
def ltr():
unpack_all()
n = random.randint(3, 10)
arrivalTime = [i for i in range(n)]
execTime = [random.randint(1, 10) for i in range(n)]
process = ['P' + str(i) for i in range(n)]
completionTime = [0 for _ in range(n)]
d = arrivalTime[:]
e = execTime[:]
execOrder = list()
arrivalTime = [0 for i in range(n)]
f = Frame(root)
f.pack()
Label(f, text="\nLottery Schduling\n", font=('Helveltika bold', 12)).pack()
Label(f, text="Arrival Time: " + str(arrivalTime)).pack(anchor=W)
Label(f, text="Process: " + str(process)).pack(anchor=W)
Label(f, text="Burst Time: " + str(execTime)).pack(anchor=W)
arrivalTime = [i for i in range(n)]
while len(d) > 0:
curr = random.randint(0, len(d) - 1)
for i in d:
completionTime[i] += (e[curr])
execOrder.append('P' + str(d[curr]))
del d[curr]
del e[curr]
taTime = [completionTime[i] - 0 for i in range(n)]
waitTime = [completionTime[i] - 0 - execTime[i] for i in range(n)]
arrivalTime = [0 for i in range(n)]
for i in range(len(waitTime)):
if waitTime[i] < 0:
waitTime[i] = 0
avg = sum(waitTime) / float(n)
Label(f, text="\n----- After Performing Lottery Scheduling Algorithm -----\n").pack(anchor=W)
Label(f, text="Arrival Time: " + str(arrivalTime)).pack(anchor=W)
Label(f, text="Process: " + str(process)).pack(anchor=W)
Label(f, text="Burst Time: " + str(execTime)).pack(anchor=W)
# Label(f,text = "Burst Time: "+ str(execTime)).pack(anchor = W)
Label(f, text="Completion Time: " + str(taTime)).pack(anchor=W)
Label(f, text="Turn Around Time: " + str(taTime)).pack(anchor=W)
Label(f, text="Wait Time: " + str(waitTime)).pack(anchor=W)
Label(f, text="Average Wait Time: " + str(avg)).pack(anchor=W)
l1 = Label(root, text='')
l1.pack()
runOther.pack(padx=120, side=LEFT)
destroy.pack(padx=10, side=LEFT)
def mlq():
unpack_all()
n1 = random.randint(3, 10)
arrivalTime1 = [i for i in range(n1)]
execTime1 = [random.randint(1, 10) for i in range(n1)]
process1 = ['P' + str(i) for i in range(n1)]
burstTime1 = [0 for _ in range(n1)]
n2 = random.randint(3, 10)
arrivalTime2 = [i for i in range(n2)]
execTime2 = [random.randint(1, 10) for i in range(n2)]
process2 = ['P' + str(i) for i in range(n2)]
burstTime2 = [0 for _ in range(n2)]
d1 = arrivalTime1[:]
e1 = execTime1[:]
d2 = arrivalTime2[:]
e2 = execTime2[:]
f.pack()
Label(f, text="\nMultiple Level Queue Scheduling\n", font=('Helveltika bold', 12)).pack()
Label(f, text="Arrival Time of CPU-Bound Processes: " + str(arrivalTime1)).pack(anchor=W)
Label(f, text="Process of CPU-Bound Processes: " + str(process1)).pack(anchor=W)
Label(f, text="Burst Time of CPU-Bound Processes: " + str(execTime1)).pack(anchor=W)
Label(f, text="Arrival Time of I/O-Bound Processes: " + str(arrivalTime2)).pack(anchor=W)
Label(f, text="Process of I/O-Bound Processes: " + str(process2)).pack(anchor=W)
Label(f, text="Burst Time of I/O-Bound Processes: " + str(execTime2)).pack(anchor=W)
while len(d1) > 0 or len(d2) > 0:
if len(d1) > 0:
for i in d1:
burstTime1[i] += e1[0]
for i in d2:
burstTime2[i] += e1[0]
del d1[0]
del e1[0]
if len(d2) > 0:
for i in d1:
burstTime1[i] += e2[0]
for i in d2:
burstTime2[i] += e2[0]
del d2[0]
del e2[0]
waitTime1 = [burstTime1[i] - arrivalTime1[i] - execTime1[i] for i in range(n1)]
for i in range(len(waitTime1)):
if waitTime1[i] < 0:
waitTime1[i] = 0
waitTime2 = [burstTime2[i] - arrivalTime2[i] - execTime2[i] for i in range(n2)]
for i in range(len(waitTime2)):
if waitTime2[i] < 0:
waitTime2[i] = 0
avg1 = sum(waitTime1) / float(len(waitTime1))
avg2 = sum(waitTime2) / float(len(waitTime2))
Label(f, text="\n----- After Performing First Come First Serve Scheduling Algorithm -----\n").pack(anchor=W)
Label(f, text="Arrival Time of CPU-bound Processes: " + str(arrivalTime1)).pack(anchor=W)
Label(f, text="Process of CPU-bound Processes: " + str(process1)).pack(anchor=W)
Label(f, text="Burst Time of CPU-bound Processes: " + str(execTime1)).pack(anchor=W)
Label(f, text="Turn Around Time of CPU-bound Processes: " + str(burstTime1)).pack(anchor=W)
Label(f, text="Wait Time of CPU-bound Processes: " + str(waitTime1)).pack(anchor=W)
Label(f, text="Average Wait Time: " + str(avg1)).pack(anchor=W)
Label(f, text="Arrival Time of I/O-bound Processes: " + str(arrivalTime2)).pack(anchor=W)
Label(f, text="Process of I/O-bound Processes: " + str(process2)).pack(anchor=W)
Label(f, text="Burst Time of I/O-bound Processes: " + str(execTime2)).pack(anchor=W)
Label(f, text="Turn Around Time of I/O-bound Processes: " + str(burstTime2)).pack(anchor=W)
Label(f, text="Wait Time of I/O-bound Processes: " + str(waitTime2)).pack(anchor=W)
Label(f, text="Average Wait Time: " + str(avg2)).pack(anchor=W)
l1 = Label(root, text='')
l1.pack()
runOther.pack(padx=120, side=LEFT)
destroy.pack(padx=10, side=LEFT)
root = Tk()
root.title("Scheduling Algorithm Simulator")
Label(root, text='OS Project').pack()
Label(root, text='').pack()
Label(root, text='').pack()
destroy = Button(root, text="Quit", command=quit)
runOther = Button(root, text="Run Other", command=app)
f = Frame(root)
f.pack()
f.pack_forget()
w = Label(root, text="\nScheduling Algorithm Simulator\n", font=('Helveltika bold', 14))
c = Label(root, text="\nChoose any one from below:\n")
w.pack()
c.pack()
fcfs = Button(root, text="First Come First Serve", width=30, command=fcfs)
fcfs.pack(padx=10)
l1 = Label(root, text='')
l1.pack()
sjn = Button(root, text="Shortest Job First", width=30, command=sjn)
sjn.pack(padx=10)
l2 = Label(root, text='')
l2.pack()
pbs = Button(root, text="Priority Based Scheduling", width=30, command=pbs)
pbs.pack(padx=10)
l3 = Label(root, text='')
l3.pack()
rr = Button(root, text="Round Robin Scheduling", width=30, command=rr)
rr.pack(padx=10)
l5 = Label(root, text='')
l5.pack()
ltr = Button(root, text="Lottery Scheduling", width=30, command=ltr)
ltr.pack(padx=10)
l6 = Label(root, text='')
l6.pack()
mlq = Button(root, text="Multi Level Queue Scheduling", width=30, command=mlq)
mlq.pack(padx=10)
l7 = Label(root, text='')
l7.pack()
destroy.pack(padx=10)
root.mainloop()
if __name__ == '__main__':
app()