-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScheduler.py
527 lines (471 loc) · 29.9 KB
/
Scheduler.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
from Algorithm_Analyze import analyze_algorithm_on_the_queue
from Process_Analyze import analyze_processes_of_queue
from Queue import *
from Scheduler_Utils import create_directory, create_file
def admit(job_queue, ready_queue):
enqueue(ready_queue, dequeue(job_queue))
report_text = "\tProcess-{} Moved From Job-Queue to Ready-Queue.\n".format(rear(ready_queue).process_id)
return report_text
def dispatch(running_process, current_time):
if running_process.response_time == -1:
running_process.response_time = current_time
return "\tProcess-{} Moved From Ready-Queue to Running-State.\n".format(running_process.process_id)
def preempt(ready_queue, running_process):
enqueue(ready_queue, running_process)
return "\tProcess-{}'s Time Quantum Was Finished and Was Moved From Running-State to Ready-Queue." \
"\n".format(rear(ready_queue).process_id)
def IO_request(waiting_queue, running_process):
enqueue(waiting_queue, running_process)
return f"\tProcess-{rear(waiting_queue).process_id} Moved From Running-State to Waiting-Queue to" \
f" Execute Its IO Burst.\n"
def IO_completion(waiting_queue, ready_queue):
temp = dequeue(waiting_queue)
enqueue(ready_queue, temp)
return "\tProcess-{}'s I/O Waiting Time Was Finished and Was Moved From" \
" Waiting-Queue to Ready-Queue.\n".format(temp.process_id)
def terminate(running_process, terminated_queue, current_time):
enqueue(terminated_queue, running_process)
running_process.termination_time = current_time + 1
running_process.turn_around_time = running_process.termination_time - (
running_process.arrival_time)
running_process.waiting_time = running_process.turn_around_time - (
running_process.CPU_burst_time_1 + running_process.IO_burst_time
+ running_process.CPU_burst_time_2)
return f"\tProcess-{rear(terminated_queue).process_id} Was Terminated " \
f"(Moved From Running-State to Terminated-Queue).\n"
def FCFS_scheduling_algorithm(job_queue):
create_directory("./output/FCFS")
algorithm_procedure_output_file = create_file("./output/FCFS/", "FCFS - Scheduling Timeline Flow", ".log")
algorithm_analysis_output_file = create_file("./output/FCFS/", "FCFS - Algorithm's Analysis", ".log")
processes_analysis_output_file = create_file("./output/FCFS/", "FCFS - Job-Queue's Processes' Analysis", ".log")
job_queue_copy = copy_queue(job_queue)
ready_queue = Queue(job_queue.capacity)
running_process = None
waiting_queue = Queue(job_queue.capacity)
terminated_queue = Queue(job_queue.capacity)
current_time = 0
algorithm_procedure_output_file.write("***"
" First Come, First Served (FCFS) Scheduling Algorithm in Operating System "
"***\n\n")
while not is_full(terminated_queue):
algorithm_procedure_output_file.write(f"Time = {current_time}-{current_time + 1}:\n")
if not is_empty(job_queue) and (front(job_queue).arrival_time <= current_time):
admit_report = admit(job_queue, ready_queue)
algorithm_procedure_output_file.write(admit_report)
if not is_empty(ready_queue) and running_process is None:
running_process = dequeue(ready_queue)
dispatch_report = dispatch(running_process, current_time)
algorithm_procedure_output_file.write(dispatch_report)
if not is_empty(waiting_queue):
front(waiting_queue).IO_burst_time -= 1
algorithm_procedure_output_file.write("\tProcess-{}'s Waited for I/O Resource for 1 Second (Remaining I/O"
" Waiting Time = {}).\n".format(front(waiting_queue).process_id,
front(waiting_queue).IO_burst_time))
if front(waiting_queue).IO_burst_time == 0:
IO_comp_report = IO_completion(waiting_queue, ready_queue)
algorithm_procedure_output_file.write(IO_comp_report)
if running_process is not None:
if running_process.CPU_burst_time_1 == 0:
if running_process.IO_burst_time == 0 and running_process.CPU_burst_time_2 != 0:
running_process.CPU_burst_time_2 -= 1
algorithm_procedure_output_file.write(
f"\tProcess-{running_process.process_id}'s Second CPU Burst Was Executed for 1 Second"
f" (Remaining Second CPU Burst Time = {running_process.CPU_burst_time_2}).\n")
if running_process.CPU_burst_time_2 == 0:
terminate_report = terminate(running_process, terminated_queue, current_time)
running_process = None
algorithm_procedure_output_file.write(terminate_report)
else:
running_process.CPU_burst_time_1 -= 1
algorithm_procedure_output_file.write(
f"\tProcess-{running_process.process_id}'s First CPU Burst Was Executed for 1 Second "
f"(Remaining First CPU Burst Time = {running_process.CPU_burst_time_1}).\n")
if running_process.CPU_burst_time_1 == 0 and running_process.IO_burst_time != 0:
IO_req_report = IO_request(waiting_queue, running_process)
running_process = None
algorithm_procedure_output_file.write(IO_req_report)
current_time += 1
algorithm_procedure_output_file.close()
analyze_algorithm_on_the_queue(job_queue_copy, terminated_queue, algorithm_analysis_output_file)
analyze_processes_of_queue(terminated_queue, processes_analysis_output_file)
algorithm_analysis_output_file.close()
processes_analysis_output_file.close()
def RR_scheduling_algorithm(job_queue):
create_directory("./output/RR")
algorithm_procedure_output_file = create_file("./output/RR/", "RR - Scheduling Timeline Flow", ".log")
algorithm_analysis_output_file = create_file("./output/RR/", "RR - Algorithm's Analysis", ".log")
processes_analysis_output_file = create_file("./output/RR/", "RR - Job-Queue's Processes' Analysis", ".log")
job_queue_copy = copy_queue(job_queue)
ready_queue = Queue(job_queue.capacity)
running_process = None
waiting_queue = Queue(job_queue.capacity)
terminated_queue = Queue(job_queue.capacity)
current_time = 0
algorithm_procedure_output_file.write("*** Round-Robin (RR) Scheduling Algorithm in Operating System "
"***\n\n")
time_quantum = int(input("Enter your desired time-quantum: "))
temp_elapsed_time = 0
while not is_full(terminated_queue):
algorithm_procedure_output_file.write("Time = {}-{}:\n".format(current_time, current_time + 1))
if not is_empty(job_queue) and front(job_queue).arrival_time <= current_time:
admit_report = admit(job_queue, ready_queue)
algorithm_procedure_output_file.write(admit_report)
if not is_empty(ready_queue) and running_process is None:
running_process = dequeue(ready_queue)
dispatch_report = dispatch(running_process, current_time)
algorithm_procedure_output_file.write(dispatch_report)
if not is_empty(waiting_queue):
front(waiting_queue).IO_burst_time -= 1
algorithm_procedure_output_file.write(
"\tProcess-{}'s Waited for I/O Resource for 1 Second (Remaining I/O Waiting Time = {}).\n".format(
front(waiting_queue).process_id, front(waiting_queue).IO_burst_time))
if front(waiting_queue).IO_burst_time == 0:
IO_comp_report = IO_completion(waiting_queue, ready_queue)
algorithm_procedure_output_file.write(IO_comp_report)
if running_process is not None:
if running_process.CPU_burst_time_1 == 0:
if running_process.IO_burst_time == 0 and running_process.CPU_burst_time_2 != 0:
running_process.CPU_burst_time_2 -= 1
temp_elapsed_time += 1
algorithm_procedure_output_file.write(
"\tProcess-{}'s Second CPU Burst Was Executed for 1 Second "
"(Remaining Second CPU Burst Time = {}).\n".format(
running_process.process_id, running_process.CPU_burst_time_2))
if running_process.CPU_burst_time_2 == 0:
terminate_report = terminate(running_process, terminated_queue, current_time)
running_process = None
algorithm_procedure_output_file.write(terminate_report)
if temp_elapsed_time == time_quantum:
preempt_report = preempt(ready_queue, running_process)
running_process = None
algorithm_procedure_output_file.write(preempt_report)
else:
running_process.CPU_burst_time_1 -= 1
temp_elapsed_time += 1
algorithm_procedure_output_file.write(
"\tProcess-{}'s First CPU Burst Was Executed for 1 Second (Remaining First CPU Burst Time = {})."
"\n".format(
running_process.process_id, running_process.CPU_burst_time_1))
if running_process.CPU_burst_time_1 == 0 and running_process.IO_burst_time != 0:
IO_req_report = IO_request(waiting_queue, running_process)
running_process = None
algorithm_procedure_output_file.write(IO_req_report)
elif temp_elapsed_time == time_quantum:
preempt_report = preempt(ready_queue, running_process)
running_process = None
algorithm_procedure_output_file.write(preempt_report)
current_time += 1
algorithm_procedure_output_file.close()
analyze_algorithm_on_the_queue(job_queue_copy, terminated_queue, algorithm_analysis_output_file)
analyze_processes_of_queue(terminated_queue, processes_analysis_output_file)
algorithm_analysis_output_file.close()
processes_analysis_output_file.close()
def SPN_scheduling_algorithm(job_queue):
create_directory("./output/SPN")
algorithm_procedure_output_file = create_file("./output/SPN/", "SPN - Scheduling Timeline Flow", ".log")
algorithm_analysis_output_file = create_file("./output/SPN/", "SPN - Algorithm's Analysis", ".log")
processes_analysis_output_file = create_file("./output/SPN/", "SPN - Job-Queue's Processes' Analysis", ".log")
job_queue_copy = copy_queue(job_queue)
ready_queue = Queue(job_queue.capacity)
running_process = None
waiting_queue = Queue(job_queue.capacity)
terminated_queue = Queue(job_queue.capacity)
current_time = 0
algorithm_procedure_output_file.write("*** Shortest-Process-Next (SPN) Scheduling Algorithm in Operating System "
"***\n\n")
while not is_full(terminated_queue):
algorithm_procedure_output_file.write("Time = {}-{}:\n".format(current_time, current_time + 1))
if not is_empty(job_queue) and front(job_queue).arrival_time <= current_time:
admit_report = admit(job_queue, ready_queue)
algorithm_procedure_output_file.write(admit_report)
if not is_empty(ready_queue) and running_process is None:
running_process = remove_process_with_minimum_CPU_burst_time(ready_queue)
dispatch_report = dispatch(running_process, current_time)
algorithm_procedure_output_file.write(dispatch_report)
if not is_empty(waiting_queue):
front(waiting_queue).IO_burst_time -= 1
algorithm_procedure_output_file.write(
"\tProcess-{}'s Waited for I/O Resource for 1 Second (Remaining I/O Waiting Time = {}).\n".format(
front(waiting_queue).process_id, front(waiting_queue).IO_burst_time))
if front(waiting_queue).IO_burst_time == 0:
IO_comp_report = IO_completion(waiting_queue, ready_queue)
algorithm_procedure_output_file.write(IO_comp_report)
if running_process is not None:
if running_process.CPU_burst_time_1 == 0:
if running_process.IO_burst_time == 0 and running_process.CPU_burst_time_2 != 0:
running_process.CPU_burst_time_2 -= 1
algorithm_procedure_output_file.write(
"\tProcess-{}'s Second CPU Burst Was Executed for 1 Second "
"(Remaining Second CPU Burst Time = {}).\n".format(running_process.process_id,
running_process.CPU_burst_time_2))
if running_process.CPU_burst_time_2 == 0:
terminate_report = terminate(running_process, terminated_queue, current_time)
running_process = None
algorithm_procedure_output_file.write(terminate_report)
else:
running_process.CPU_burst_time_1 -= 1
algorithm_procedure_output_file.write(
"\tProcess-{}'s First CPU Burst Was Executed for 1 Second (Remaining First CPU Burst Time = {})."
"\n".format(
running_process.process_id, running_process.CPU_burst_time_1))
if running_process.CPU_burst_time_1 == 0 and running_process.IO_burst_time != 0:
IO_req_report = IO_request(waiting_queue, running_process)
running_process = None
algorithm_procedure_output_file.write(IO_req_report)
current_time += 1
algorithm_procedure_output_file.close()
analyze_algorithm_on_the_queue(job_queue_copy, terminated_queue, algorithm_analysis_output_file)
analyze_processes_of_queue(terminated_queue, processes_analysis_output_file)
algorithm_analysis_output_file.close()
processes_analysis_output_file.close()
def SRTF_scheduling_algorithm(job_queue):
create_directory("./output/SRTF")
algorithm_procedure_output_file = create_file("./output/SRTF/", "SRTF - Scheduling Timeline Flow", ".log")
algorithm_analysis_output_file = create_file("./output/SRTF/", "SRTF - Algorithm's Analysis", ".log")
processes_analysis_output_file = create_file("./output/SRTF/", "SRTF - Job-Queue's Processes' Analysis", ".log")
job_queue_copy = copy_queue(job_queue)
ready_queue = Queue(job_queue.capacity)
running_process = None
waiting_queue = Queue(job_queue.capacity)
terminated_queue = Queue(job_queue.capacity)
current_time = 0
algorithm_procedure_output_file.write(
"*** Shortest-Remaining-Time-First (SRTF) Scheduling Algorithm in Operating System "
"***\n\n")
while not is_full(terminated_queue):
algorithm_procedure_output_file.write("Time = {}-{}:\n".format(current_time, current_time + 1))
if not is_empty(job_queue) and front(job_queue).arrival_time <= current_time:
admit_report = admit(job_queue, ready_queue)
algorithm_procedure_output_file.write(admit_report)
if not is_empty(ready_queue) and running_process is None:
running_process = remove_process_with_minimum_CPU_burst_time(ready_queue)
dispatch_report = dispatch(running_process, current_time)
algorithm_procedure_output_file.write(dispatch_report)
if not is_empty(waiting_queue):
front(waiting_queue).IO_burst_time -= 1
algorithm_procedure_output_file.write(
"\tProcess-{}'s Waited for I/O Resource for 1 Second (Remaining I/O Waiting Time = {}).\n".format(
front(waiting_queue).process_id, front(waiting_queue).IO_burst_time))
if front(waiting_queue).IO_burst_time == 0:
IO_comp_report = IO_completion(waiting_queue, ready_queue)
algorithm_procedure_output_file.write(IO_comp_report)
if running_process is not None:
if running_process.CPU_burst_time_1 == 0:
if running_process.IO_burst_time == 0 and running_process.CPU_burst_time_2 != 0:
running_process.CPU_burst_time_2 -= 1
algorithm_procedure_output_file.write(
"\tProcess-{}'s Second CPU Burst Was Executed for 1 Second (Remaining Second CPU Burst Time = "
"{}).\n".format(
running_process.process_id, running_process.CPU_burst_time_2))
if running_process.CPU_burst_time_2 == 0:
terminate_report = terminate(running_process, terminated_queue, current_time)
algorithm_procedure_output_file.write(terminate_report)
running_process = None
else:
preempt_report = preempt(ready_queue, running_process)
running_process = None
algorithm_procedure_output_file.write(preempt_report)
else:
running_process.CPU_burst_time_1 -= 1
algorithm_procedure_output_file.write(
"\tProcess-{}'s First CPU Burst Was Executed for 1 Second (Remaining First CPU Burst Time = {})."
"\n".format(
running_process.process_id, running_process.CPU_burst_time_1))
if running_process.CPU_burst_time_1 == 0 and running_process.IO_burst_time != 0:
IO_req_report = IO_request(waiting_queue, running_process)
running_process = None
algorithm_procedure_output_file.write(IO_req_report)
else:
preempt_report = preempt(ready_queue, running_process)
running_process = None
algorithm_procedure_output_file.write(preempt_report)
current_time += 1
algorithm_procedure_output_file.close()
analyze_algorithm_on_the_queue(job_queue_copy, terminated_queue, algorithm_analysis_output_file)
analyze_processes_of_queue(terminated_queue, processes_analysis_output_file)
algorithm_analysis_output_file.close()
processes_analysis_output_file.close()
def MLFQ_scheduling_algorithm(job_queue):
create_directory("./output/MLFQ")
algorithm_procedure_output_file = create_file("./output/MLFQ/", "MLFQ - Scheduling Timeline Flow", ".log")
algorithm_analysis_output_file = create_file("./output/MLFQ/", "MLFQ - Algorithm's Analysis", ".log")
processes_analysis_output_file = create_file("./output/MLFQ/", "MLFQ - Job-Queue's Processes' Analysis", ".log")
job_queue_copy = copy_queue(job_queue)
ready_queues = [] # List to hold multiple queues
running_process = None
waiting_queue = Queue(job_queue.capacity)
terminated_queue = Queue(job_queue.capacity)
current_time = 0
temp_elapsed_time = 0
process_previous_running_level = 1
algorithm_procedure_output_file.write(
"*** Multi-Level-Feedback-Queue (MLFQ) Scheduling Algorithm in Operating System ***\n\n")
# Ask user for the number of queues and quantum time for each queue
num_queues = int(input("Enter the number of queues: "))
quantum_times = []
for i in range(num_queues):
quantum_time = int(input("Enter the quantum time for Queue {}: ".format(i + 1)))
quantum_times.append(quantum_time)
ready_queues.append(Queue(job_queue.capacity))
while not is_full(terminated_queue):
algorithm_procedure_output_file.write("Time = {}-{}:\n".format(current_time, current_time + 1))
if not is_empty(job_queue) and front(job_queue).arrival_time <= current_time:
admit_report = admit(job_queue, ready_queues[0])
algorithm_procedure_output_file.write(admit_report)
if not is_empty(ready_queues[0]) and running_process is None:
running_process = dequeue(ready_queues[0])
process_previous_running_level = 1
temp_elapsed_time = 0
dispatch_report = dispatch(running_process, current_time)
algorithm_procedure_output_file.write(dispatch_report)
else:
for i in range(1, num_queues):
if not is_empty(ready_queues[i]) and running_process is None:
running_process = dequeue(ready_queues[i])
process_previous_running_level = i + 1
temp_elapsed_time = 0
if running_process.response_time == -1:
running_process.response_time = current_time
algorithm_procedure_output_file.write(
"\tProcess-{} Moved From Ready-Queue Level {} to Running-State.\n".format(
running_process.process_id, i + 1))
break
if not is_empty(waiting_queue):
front(waiting_queue).IO_burst_time -= 1
algorithm_procedure_output_file.write(
"\tProcess-{}'s Waited for I/O Resource for 1 Second (Remaining I/O Waiting Time = {}).\n".format(
front(waiting_queue).process_id, front(waiting_queue).IO_burst_time))
if front(waiting_queue).IO_burst_time == 0:
enqueue(ready_queues[0], dequeue(waiting_queue))
algorithm_procedure_output_file.write(
"\tProcess-{}'s IO's Waiting Time Was Finished and Was Moved From Waiting-Queue to Ready-Queue."
"\n".format(
rear(ready_queues[0]).process_id))
if running_process is not None:
queue_index = process_previous_running_level - 1 # Index of the queue from which process was dequeued
if running_process.CPU_burst_time_1 == 0:
if running_process.IO_burst_time == 0 and running_process.CPU_burst_time_2 != 0:
running_process.CPU_burst_time_2 -= 1
temp_elapsed_time += 1
algorithm_procedure_output_file.write(
"\tProcess-{}'s Second CPU Burst Was Executed for 1 Second "
"(Remaining Second CPU Burst Time = {}).\n".format(
running_process.process_id, running_process.CPU_burst_time_2))
if running_process.CPU_burst_time_2 == 0:
enqueue(terminated_queue, running_process)
running_process.termination_time = current_time + 1
running_process.turn_around_time = running_process.termination_time - (
running_process.arrival_time)
running_process.waiting_time = running_process.turn_around_time - (
running_process.CPU_burst_time_1 + running_process.IO_burst_time + (
running_process.CPU_burst_time_2))
running_process = None
algorithm_procedure_output_file.write(
"\tProcess-{} Was Terminated (Moved From Running-State to Terminated-Queue).\n".format(
rear(terminated_queue).process_id))
elif temp_elapsed_time == quantum_times[queue_index]:
if queue_index + 1 < num_queues:
enqueue(ready_queues[queue_index + 1], running_process)
running_process = None
algorithm_procedure_output_file.write(
"\tProcess-{} Was Preempted (Moved From Running-State to Ready-Queue Level {})."
"\n".format(
rear(ready_queues[queue_index + 1]).process_id, queue_index + 2))
else:
enqueue(ready_queues[queue_index], running_process)
running_process = None
algorithm_procedure_output_file.write(
"\tProcess-{} Time Slice Expired "
"(Moved From Running-State to the Same Ready-Queue Level).\n".format(
rear(ready_queues[queue_index]).process_id))
else:
running_process.CPU_burst_time_1 -= 1
temp_elapsed_time += 1
algorithm_procedure_output_file.write(
"\tProcess-{}'s First CPU Burst Was Executed for 1 Second (Remaining First CPU Burst Time = {})."
"\n".format(
running_process.process_id, running_process.CPU_burst_time_1))
if running_process.CPU_burst_time_1 == 0 and running_process.IO_burst_time != 0:
enqueue(waiting_queue, running_process)
running_process = None
algorithm_procedure_output_file.write(
"\tProcess-{} Moved From Running-State to Waiting-Queue to Execute Its IO Burst.\n".format(
rear(waiting_queue).process_id))
elif temp_elapsed_time == quantum_times[queue_index]:
if queue_index + 1 < num_queues:
enqueue(ready_queues[queue_index + 1], running_process)
running_process = None
algorithm_procedure_output_file.write(
"\tProcess-{} Was Preempted (Moved From Running-State to Ready-Queue Level {}).\n".format(
rear(ready_queues[queue_index + 1]).process_id, queue_index + 2))
else:
enqueue(ready_queues[queue_index], running_process)
running_process = None
algorithm_procedure_output_file.write(
"\tProcess-{} Time Slice Expired (Moved From Running-State to the Same Ready-Queue Level)."
"\n".format(
rear(ready_queues[queue_index]).process_id))
current_time += 1
algorithm_procedure_output_file.close()
analyze_algorithm_on_the_queue(job_queue_copy, terminated_queue, algorithm_analysis_output_file)
analyze_processes_of_queue(terminated_queue, processes_analysis_output_file)
algorithm_analysis_output_file.close()
processes_analysis_output_file.close()
def HRRN_scheduling_algorithm(job_queue):
create_directory("./output/HRRN")
algorithm_procedure_output_file = create_file("./output/HRRN/", "HRRN - Scheduling Timeline Flow", ".log")
algorithm_analysis_output_file = create_file("./output/HRRN/", "HRRN - Algorithm's Analysis", ".log")
processes_analysis_output_file = create_file("./output/HRRN/", "HRRN - Job-Queue's Processes' Analysis", ".log")
job_queue_copy = copy_queue(job_queue)
ready_queue = Queue(job_queue.capacity)
running_process = None
waiting_queue = Queue(job_queue.capacity)
terminated_queue = Queue(job_queue.capacity)
current_time = 0
algorithm_procedure_output_file.write(
"*** Highest-Response-Ratio-Next (HRRN) Scheduling Algorithm in Operating System ***\n\n")
while not is_full(terminated_queue):
algorithm_procedure_output_file.write("Time = {}-{}:\n".format(current_time, current_time + 1))
if not is_empty(job_queue) and front(job_queue).arrival_time <= current_time:
admit_report = admit(job_queue, ready_queue)
algorithm_procedure_output_file.write(admit_report)
if not is_empty(ready_queue) and running_process is None:
running_process = remove_process_with_highest_priority(ready_queue)
dispatch_report = dispatch(running_process, current_time)
algorithm_procedure_output_file.write(dispatch_report)
if not is_empty(waiting_queue):
front(waiting_queue).IO_burst_time -= 1
algorithm_procedure_output_file.write(
"\tProcess-{}'s Waited for I/O Resource for 1 Second (Remaining I/O Waiting Time = {}).\n".format(
front(waiting_queue).process_id, front(waiting_queue).IO_burst_time))
if front(waiting_queue).IO_burst_time == 0:
IO_comp_report = IO_completion(waiting_queue, ready_queue)
algorithm_procedure_output_file.write(IO_comp_report)
if running_process is not None:
if running_process.CPU_burst_time_1 == 0:
if running_process.IO_burst_time == 0 and running_process.CPU_burst_time_2 != 0:
running_process.CPU_burst_time_2 -= 1
algorithm_procedure_output_file.write(
"\tProcess-{}'s Second CPU Burst Was Executed for 1 Second "
"(Remaining Second CPU Burst Time = {}).\n".format(running_process.process_id,
running_process.CPU_burst_time_2))
if running_process.CPU_burst_time_2 == 0:
terminate_report = terminate(running_process, terminated_queue, current_time)
running_process = None
algorithm_procedure_output_file.write(terminate_report)
else:
running_process.CPU_burst_time_1 -= 1
algorithm_procedure_output_file.write(
"\tProcess-{}'s First CPU Burst Was Executed for 1 Second (Remaining First CPU Burst Time = {})."
"\n".format(
running_process.process_id, running_process.CPU_burst_time_1))
if running_process.CPU_burst_time_1 == 0 and running_process.IO_burst_time != 0:
IO_req_report = IO_request(waiting_queue, running_process)
running_process = None
algorithm_procedure_output_file.write(IO_req_report)
current_time += 1
algorithm_procedure_output_file.close()
analyze_algorithm_on_the_queue(job_queue_copy, terminated_queue, algorithm_analysis_output_file)
analyze_processes_of_queue(terminated_queue, processes_analysis_output_file)
algorithm_analysis_output_file.close()
processes_analysis_output_file.close()