-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalgorithms.py
More file actions
481 lines (361 loc) · 14.5 KB
/
algorithms.py
File metadata and controls
481 lines (361 loc) · 14.5 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
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
import random
import copy
import pprint
from collections import deque
PRIORITY_RANGE_MIN = 1
PRIORITY_RANGE_MAX = 10
BURST_TIME_RANGE_MAX = 10
BURST_TIME_RANGE_MIN = 1
ARRIVAL_TIME_RANGE_MIN = 0
ARRIVAL_TIME_RANGE_MAX = 10
MAX_SIMULATION_TIME = 999
SIMULATION_SPEED = 1
NUMBER_OF_PROCESSES = 10
TIME_QUANTUM = 3
FIRST_COME_FIRST_SERVE = 'First Come First Serve'
SHORTEST_JOB_FIRST = 'Shortest Job First'
SHORTEST_REMAINING_TIME_FIRST = 'Shortest Remaining Time First'
ROUND_ROBIN = 'Round Robin'
PRIORITY_SCHEDULING = 'Priority Scheduling'
RESULTS = {}
GANTT = {}
ALGORITHMS = [FIRST_COME_FIRST_SERVE,
SHORTEST_JOB_FIRST,
SHORTEST_REMAINING_TIME_FIRST,
ROUND_ROBIN,
PRIORITY_SCHEDULING]
class CPU:
def __init__(self):
self.process = None
def ingest_process(self, process):
self.process = process
def is_process_complete(self):
if self.process is None:
return True
if self.process.remaining_burst_time <= 0:
return True
return False
def decrement_process(self):
if self.process is not None:
self.process.remaining_burst_time -= 1
def record_process_start_time(self, time):
if self.process is not None:
if self.process.burst_time == self.process.remaining_burst_time:
self.process.start_time = time
def record_process_finish_time(self, time):
if self.process is not None:
if self.process.finished is not True and self.process.remaining_burst_time == 0:
self.process.finish_time = time
self.process.finished = True
def record_gantt(self, algorithm):
if self.process is None: # CPU not processing anything at this time instance
pid = None
else:
pid = self.process.pid
if algorithm in GANTT:
GANTT[algorithm].append(pid)
else:
GANTT[algorithm] = [pid]
def reset(self):
self.process = None
class Process:
def __init__(self, pid):
self.pid = pid
self.arrival_time = 0
self.burst_time = 0
self.remaining_burst_time = 0
self.priority = 0
self.start_time = 0
self.finish_time = 0
self.finished = False
self.setup()
def setup(self):
self.arrival_time = int(random.uniform(ARRIVAL_TIME_RANGE_MIN, ARRIVAL_TIME_RANGE_MAX))
self.priority = int(random.uniform(PRIORITY_RANGE_MIN, PRIORITY_RANGE_MAX))
self.burst_time = int(random.uniform(BURST_TIME_RANGE_MIN, BURST_TIME_RANGE_MAX))
self.remaining_burst_time = self.burst_time
class Simulation():
def __init__(self):
self.process_pool = set()
self.setup()
def setup(self):
# Generate Process instances and add them to the pool
# pid is assigned sequentially
for pid in range(1, NUMBER_OF_PROCESSES + 1):
self.process_pool.add(Process(pid))
def pprint_process_pool(self):
processes = sorted(self.process_pool)
for process in processes:
print('pid %s: Arrival time: %s, Burst Time: %s, Prio: %s, remainingburst: %s') % (
process.pid, process.arrival_time, process.burst_time, process.priority, process.remaining_burst_time)
@classmethod
def determine_simulation_time(self):
""" Determines the simulation time based on the latest None in GANTT chart"""
assert len(GANTT) != 0, 'GANTT chart is not populated yet'
indicies = []
for algorithm in GANTT:
# Counting backwards in GANTT for each algorithm
for i, x in reversed(list(enumerate(GANTT[algorithm]))):
if x is not None: # If None value reached
indicies.append(i + 1)
return max(indicies)
class Algorithm():
def __init__(self):
pass
def check_process_arrivals(self, time):
""" Add process to deque the process if arrival time matches current time """
for process in self.simulation.process_pool:
if time == process.arrival_time:
self.deque.append(process)
def calculate_results(self):
total_waiting_time = 0
total_turnaround_time = 0
for process in self.simulation.process_pool:
waiting_time = process.start_time - process.arrival_time
turnaround_time = process.finish_time - process.arrival_time
total_waiting_time += waiting_time
total_turnaround_time += turnaround_time
if self.name in RESULTS:
RESULTS[self.name].update({
process.pid: {
'waiting_time': waiting_time,
'turnaround_time': turnaround_time,
'arrival_time': process.arrival_time,
'burst_time': process.burst_time,
'priority': process.priority,
'start_time': process.start_time,
'finish_time': process.finish_time
}
})
else:
RESULTS[self.name] = {
process.pid: {
'waiting_time': waiting_time,
'turnaround_time': turnaround_time,
'arrival_time': process.arrival_time,
'burst_time': process.burst_time,
'priority': process.priority,
'start_time': process.start_time,
'finish_time': process.finish_time,
}
}
RESULTS[self.name].update({
'average_waiting_time': round(float(total_waiting_time) / NUMBER_OF_PROCESSES, 2),
'average_turnaround_time': round(float(total_turnaround_time) / NUMBER_OF_PROCESSES, 2),
})
class FirstComeFirstServe(Algorithm):
def __init__(self, simulation):
Algorithm.__init__(self)
self.name = FIRST_COME_FIRST_SERVE
self.simulation = copy.deepcopy(simulation)
self.deque = deque()
def get_first_process(self):
try:
return self.deque.popleft()
except Exception:
return # noop
class ShortestJobFirst(Algorithm):
def __init__(self, simulation):
Algorithm.__init__(self)
self.name = SHORTEST_JOB_FIRST
self.simulation = copy.deepcopy(simulation)
self.deque = deque()
def remove_expired_jobs_from_pool(self):
try:
for process in self.deque:
if process.remaining_burst_time <= 0:
self.deque.remove(process)
except Exception:
return # noop
def get_shortest_job_from_pool(self):
""" Returns the process with the shortest burst time """
shortest_process = None
shortest_process_pid_time = 999
try:
for process in self.deque:
if process.burst_time < shortest_process_pid_time:
shortest_process = process
shortest_process_pid_time = process.burst_time
return shortest_process
except IndexError:
return # noop
class ShortestRemainingTimeFirst(Algorithm):
def __init__(self, simulation):
Algorithm.__init__(self)
self.name = SHORTEST_REMAINING_TIME_FIRST
self.simulation = copy.deepcopy(simulation)
self.deque = deque()
def remove_expired_jobs_from_pool(self):
try:
for process in self.deque:
if process.remaining_burst_time <= 0:
self.deque.remove(process)
except Exception:
return # noop
def get_shortest_remaining_time_from_pool(self):
""" Returns the process with the shortest remaining burst time """
shortest_remaining_burst_time_process = None
shortest_process_pid_remaining_burst_time = 999
try:
for process in self.deque:
if process.remaining_burst_time < shortest_process_pid_remaining_burst_time:
shortest_remaining_burst_time_process = process
shortest_process_pid_remaining_burst_time = process.remaining_burst_time
return shortest_remaining_burst_time_process
except IndexError:
return # noop
class RoundRobin(Algorithm):
def __init__(self, simulation):
Algorithm.__init__(self)
self.name = ROUND_ROBIN
self.simulation = copy.deepcopy(simulation)
self.current_round_robin_process_index = 0
self.deque = deque()
def get_current_process(self):
return self.deque[self.current_round_robin_process_index]
def get_next_process(self):
""" Returns the process in the job pool - round robin style """
index = self.current_round_robin_process_index
count = 0
try:
while count < len(self.deque):
if index + 1 == len(self.deque):
index = 0
else:
index += 1
count += 1
# Do not return finished processes
if self.deque[index].remaining_burst_time <= 0:
continue
self.current_round_robin_process_index = index
return self.deque[index]
except Exception:
print('e')
return # noop
class PriorityScheduling(Algorithm):
def __init__(self, simulation):
Algorithm.__init__(self)
self.name = PRIORITY_SCHEDULING
self.simulation = simulation
self.deque = deque()
def remove_expired_jobs_from_pool(self):
try:
for process in self.deque:
if process.remaining_burst_time <= 0:
self.deque.remove(process)
except Exception:
return # noop
def get_lowest_priority_from_pool(self):
""" Returns the process with the lowest priority from pool """
lowest_priority_process = None
lowest_priority = 999
try:
for process in self.deque:
if process.priority < lowest_priority:
lowest_priority_process = process
lowest_priority = process.priority
return lowest_priority_process
except Exception:
return # noop
def run_simulation(priority_range_max=PRIORITY_RANGE_MAX, burst_time_range_max=BURST_TIME_RANGE_MAX,
arrival_time_range_max=ARRIVAL_TIME_RANGE_MAX, time_quantum=TIME_QUANTUM,
number_of_processes=NUMBER_OF_PROCESSES):
global NUMBER_OF_PROCESSES
NUMBER_OF_PROCESSES = number_of_processes
global PRIORITY_RANGE_MAX
PRIORITY_RANGE_MAX = priority_range_max
global BURST_TIME_RANGE_MAX
BURST_TIME_RANGE_MAX = burst_time_range_max
global ARRIVAL_TIME_RANGE_MAX
ARRIVAL_TIME_RANGE_MAX = arrival_time_range_max
global TIME_QUANTUM
TIME_QUANTUM = time_quantum
global GANTT
GANTT = {}
simulation = Simulation()
cpu = CPU()
''' First Come First Serve '''
cpu.reset() # Reset cpu for next algorithm simulation
FCFS = FirstComeFirstServe(simulation)
for time in range(0, MAX_SIMULATION_TIME):
FCFS.check_process_arrivals(time)
# Non-premptive algorithm
if cpu.is_process_complete():
cpu.ingest_process(FCFS.get_first_process())
cpu.record_process_start_time(time)
cpu.decrement_process()
cpu.record_process_finish_time(time)
cpu.record_gantt(FCFS.name)
''' Shortest Job First '''
cpu.reset() # Reset cpu for next algorithm simulation
SJF = ShortestJobFirst(simulation)
for time in range(0, MAX_SIMULATION_TIME):
SJF.check_process_arrivals(time)
SJF.remove_expired_jobs_from_pool()
# Non-premptive algorithm
if cpu.is_process_complete():
cpu.ingest_process(SJF.get_shortest_job_from_pool())
cpu.record_process_start_time(time)
cpu.decrement_process()
cpu.record_process_finish_time(time)
cpu.record_gantt(SJF.name)
''' Shortest Remaining Time First '''
cpu.reset() # Reset cpu for next algorithm simulation
SRTF = ShortestRemainingTimeFirst(simulation)
for time in range(0, MAX_SIMULATION_TIME):
SRTF.check_process_arrivals(time)
SRTF.remove_expired_jobs_from_pool()
cpu.ingest_process(SRTF.get_shortest_remaining_time_from_pool())
cpu.record_process_start_time(time)
cpu.decrement_process()
cpu.record_process_finish_time(time)
cpu.record_gantt(SRTF.name)
''' Round Robin '''
cpu.reset() # Reset cpu for next algorithm simulation
RR = RoundRobin(simulation)
current_time_quantum = 0
current_process = None
for time in range(0, MAX_SIMULATION_TIME):
RR.check_process_arrivals(time)
if current_process is None or current_time_quantum >= TIME_QUANTUM or current_process.remaining_burst_time == 0:
current_process = RR.get_next_process()
current_time_quantum = 0
else:
current_process = RR.get_current_process()
cpu.ingest_process(current_process)
cpu.record_process_start_time(time)
cpu.decrement_process()
cpu.record_process_finish_time(time)
cpu.record_gantt(RR.name)
current_time_quantum += 1
''' Priority Scheduling '''
cpu.reset() # Reset cpu for next algorithm simulation
PS = PriorityScheduling(simulation)
for time in range(0, MAX_SIMULATION_TIME):
PS.check_process_arrivals(time)
PS.remove_expired_jobs_from_pool()
cpu.ingest_process(PS.get_lowest_priority_from_pool())
cpu.record_process_start_time(time)
cpu.decrement_process()
cpu.record_process_finish_time(time)
cpu.record_gantt(PS.name)
''' Analysis '''
FCFS.calculate_results()
SJF.calculate_results()
SRTF.calculate_results()
RR.calculate_results()
PS.calculate_results()
''' Compile the results to pass to the App '''
# TODO Fix the way results are saved and stored - pretty clunky
max_simulation_time = simulation.determine_simulation_time()
print('FFF', max_simulation_time)
for algorithm in ALGORITHMS:
# Trimming the GANTT chart "None" tail
parsed_gantt = GANTT[algorithm][0:max_simulation_time]
RESULTS[algorithm].update({
'GANTT': parsed_gantt,
})
return RESULTS
if __name__ == '__main__':
run_simulation()
pprint.pprint(RESULTS)