-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsheduler.py
851 lines (778 loc) · 34.2 KB
/
sheduler.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
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
import random
from queue import Queue
from time import sleep
import sys
def log(text="\nCREATE BY MARCUS"):
with open('sheduler.log', 'a') as file:
file.writelines(text)
if sys.version_info.major < 3:
print("Não compatível com essa versão do python")
print("Atualize sua versão para 3+")
sys.exit(0)
# NOTE: The printing methods are old, because I don't know which version of python runs on that poha
# The real times defined by the user to execute were not placed, because,
# if the person sets a high time, it could run for a long time, making the presentation unfeasible.
INTERRUPTION_TIME = 1 # Set the overall system interrupt time to 1
SIZE = 0 # user-defined number of processes
class Process:
"""
This class represents the process itself.
That is, when instantiating it, the
object of the process, each having its own
attributes.
The use will be by composition and not inheritance or anything like that.
Yeah, it's simpler and performative.
"""
def __init__(self, id, burst, preemtive=False, wait_time=0, priority=random.randint(0, 10)):
"""
Process attributes
:param id: filled in automatically, according to the number of processes that will enter.
:param burst: burst time, that is, how long it will run.
:param preemtive: variable that identifies whether it is preemptive, that is, if there is an interruption,
should call the following process.
"""
self.id = id
self.burst = burst
self.preemptive = preemtive
self.quantum = 1
self.priority = priority
self.wait_time = wait_time
def __repr__(self):
return "Process {0}:\nBurst: {1}\nPreemptive: {2}\nPriority: {3}\nQuantum: {4}\n".format(
self.id,
self.burst,
self.preemptive,
self.priority,
self.quantum
)
WAIT_QUEUE = Queue()
READY_QUEUE = Queue()
class Sheduler:
"""
This class represents the process scheduler
from the CPU.
Its methods are for, in addition to interacting with the user, initially,
contain the algorithms, preemptive or not.
The main methods will be:
FCFS => FIRST COME FIRST SERVED
SJF => SHORTEST JOB FIRST
SRT => SHORTEST REMAINS TIME
DULING => SHORTEST PRIORITY
RR => ROUND ROUBIN
"""
def __init__(self, ready_queue):
"""
Takes the queue as an argument
Once this is done, it is traversed and treated in the methods below
:param ready_queue:
"""
self.ready_queue = ready_queue
def __repr__(self):
return "READY QUEUE FIFO {0}".format(self.ready_queue)
def __fcfs(self, fscfs_queue, final_queue):
"""
Method that handles the fcfs algorithm
If there is no interruption, the processes will be
executed in the order they arrive. Otherwise,
as shown in the teacher's image, the processes
will go to the end of the queue, those who are interrupted
:param fscfs_queue:
:param final_queue:
:return:
"""
global INTERRUPTION_TIME, SIZE
total_time = 0
fscfs_queue_view = self.ready_queue
history = list()
# time running
execute_time = 0
# capture start time of process execution.
data = dict()
# rest that remained to be executed, that is, processes that went to the queue
rest = dict()
print('-' * 23, end='')
print('[FCFS]', end='')
print('-' * 21)
print("[+] Processos na fila de espera, por ordem de chegada, aguardando a execucao: \n", flush=True)
print("*" * 10, flush=True)
while fscfs_queue_view.qsize() > 0:
# remove process from the queue to display its details
proc = fscfs_queue_view.get()
print(proc)
# add to the total time, the execution time of each process.
total_time += proc.burst
if proc.preemptive:
proc.wait_time = INTERRUPTION_TIME
else:
proc.wait_time = proc.burst
# store processes to know the execution time of each
history.append(proc.wait_time)
print("*" * 10)
print("[+] Executando escalonamento...")
print("[+] Duracao estimada de {0}s".format(total_time))
# traverse the history array
index = 0
# while there is a process, run the queue
while fscfs_queue.qsize() > 0:
# remove process from queue to run
proc = fscfs_queue.get()
# check if there is an interrupt
if proc.preemptive:
# if it exists, the process will be sent to the end of the queue, but not before executing for INTERRUPTION_TIME
print('!' * 10, end='')
print('INTERROMPIDO P{0}'.format(proc.id), end='')
print('!' * 10)
execute_time += INTERRUPTION_TIME
data[proc.id] = execute_time
print("[*] Houve interrupcao do processo {0}".format(proc.id))
print("[+] Esperando por {0}".format(INTERRUPTION_TIME))
sleep(INTERRUPTION_TIME)
# remove the history process
del history[index]
# reduces the value of INTERRUPTION_TIME in the process burst.
proc.burst -= INTERRUPTION_TIME
print("[+] Movendo processo {0} para o final da fila".format(proc.id))
print("[+] O processo {0} aguardara por {1} s".format(proc.id, sum(history)))
final_queue.put(proc)
# skip to the next exit
continue
else:
print("+" * 30)
print("[*] Interrupcao nao encontrada para o processo {0}".format(proc.id))
print("[*] Executando P{0} por {1}s".format(proc.id, proc.burst))
execute_time += proc.burst
data[proc.id] = execute_time
index += 1
print("-" * 50)
print("[WAIT QUEUE]")
# running the processes that went to the end of the queue:
print("[+] Executando os processos que foram para o final da fila")
# time left to run
remains = sum(history)
while final_queue.qsize() > 0:
proces = final_queue.get()
print("[+] Retomando o processo {0} aos {1}s".format(proces.id, remains))
remains += process.burst
print("[+] Processo {0} executando por {1}s".format(proces.id, proces.burst))
rest[proces.id] = proces.burst
print("[+] Concluido")
print("\n\nRESULTADO [FCFS]:")
log("\n\nRESULTADO [FCFS]:")
print("-" * SIZE * 3)
log("-" * SIZE * 3)
save = 0
count = 0
# average of times (ALLOWS VERIFYING ALGORITHM PERFORMANCE)
average = 0
for index, (key, value) in enumerate(data.items()):
print("P{0} ".format(key), end='')
if index == 0:
print("t={0}s ".format(0))
log("P{0} t={1}s ".format(key, 0))
else:
print("t={0}s ".format(save))
log("P{0} t={1}s ".format(key, save))
average += save
count += 1
save = value
for index, (key, value) in enumerate(rest.items()):
print("P{0} ".format(key), end='')
print("t={0}s ".format(save))
log("P{0} t={1}s ".format(key, save))
average += save
count += 1
save += value
print("-" * SIZE * 3)
log("-" * SIZE * 3)
print("AVERAGE: {0}".format(average/count))
log("AVERAGE: {0}".format(average / count))
def __sjf(self, sjf_queue, sjf_final_queue):
global sjf_queue_view
"""
Method that handles the sjf algorithm
If there is no interruption, the processes will be
executed so that the one with the shortest time (burst) will execute
first.Otherwise, as shown in the teacher's image, the processes
will go to the end of the queue, those who are interrupted
:param sjf_queue:
:param sjf_final_queue:
:return:
"""
global INTERRUPTION_TIME, SIZE
total_time = 0
# sjf_queue_view = self.ready_queue
history = list()
# time running
execute_time = 0
# capture start time of process execution.
data = dict()
# rest that remained to be executed, that is, processes that went to the queue
rest = dict()
print('-' * 23, end='')
print('[SJF]', end='')
print('-' * 21)
print("[+] Processos na fila de espera, pelo menor tempo de execucao, aguardando a execucao: \n", flush=True)
print("*" * 10, flush=True)
ordered_by_burst_queue = []
while sjf_queue_view.qsize() > 0:
# remove process from the queue to display its details
proc = sjf_queue_view.get()
# sjf_queue.put(proc)
ordered_by_burst_queue.append(proc)
# add to the total time, the execution time of each process.
total_time += proc.burst
if proc.preemptive:
proc.wait_time = INTERRUPTION_TIME
else:
proc.wait_time = proc.burst
# store processes to know the execution time of each
history.append(proc.wait_time)
# Here's the difference to the other methods. Below, the sorting by time (burst)
for n in range(len(ordered_by_burst_queue) - 1):
for m in range(len(ordered_by_burst_queue) - 1):
if ordered_by_burst_queue[m].burst > ordered_by_burst_queue[m + 1].burst:
aux = ordered_by_burst_queue[m]
ordered_by_burst_queue[m] = ordered_by_burst_queue[m + 1]
ordered_by_burst_queue[m + 1] = aux
# display only
for proc in ordered_by_burst_queue:
print(proc)
sjf_queue.put(proc)
print("*" * 10)
print("[+] Executando escalonamento...")
print("[+] Duracao estimada de {0}s".format(total_time))
# traverse the history array
index = 0
# traverse the history array
while sjf_queue.qsize() > 0:
# remove process from queue to run
proc = sjf_queue.get()
# check if there is an interrupt
if proc.preemptive:
# if it exists, the process will be sent to the end of the queue, but not before executing for
# INTERRUPTION_TIME
print('!' * 10, end='')
print('INTERROMPIDO P{0}'.format(proc.id), end='')
print('!' * 10)
execute_time += INTERRUPTION_TIME
data[proc.id] = execute_time
print("[*] Houve interrupcao do processo {0}".format(proc.id))
print("[+] Esperando por {0}".format(INTERRUPTION_TIME))
sleep(INTERRUPTION_TIME)
# remove the history process
del history[index]
# reduces the value of INTERRUPTION_TIME in the process burst.
proc.burst -= INTERRUPTION_TIME
print("[+] Movendo processo {0} para o final da fila".format(proc.id))
print("[+] O processo {0} aguardara por {1} s".format(proc.id, sum(history)))
sjf_final_queue.put(proc)
# skip to the next exit
continue
else:
print("+" * 30)
print("[*] Interrupcao nao encontrada para o processo {0}".format(proc.id))
print("[*] Executando P{0} por {1}s".format(proc.id, proc.burst))
execute_time += proc.burst
data[proc.id] = execute_time
index += 1
print("-" * 50)
print("[WAIT QUEUE]")
# running the processes that went to the end of the queue:
print("[+] Executando os processos que foram para o final da fila")
# time left to run
remains = sum(history)
while sjf_final_queue.qsize() > 0:
proces = sjf_final_queue.get()
print("[+] Retomando o processo {0} aos {1}s".format(proces.id, remains))
remains += process.burst
print("[+] Processo {0} executando por {1}s".format(proces.id, proces.burst))
rest[proces.id] = proces.burst
print("[+] Concluido")
print("\n\nRESULTADO [SJF]:")
log("\n\nRESULTADO [SJF]:")
print("-" * SIZE * 3)
log("-" * SIZE * 3)
save = 0
count = 0
# average of times (ALLOWS VERIFYING ALGORITHM PERFORMANCE)
average = 0
for index, (key, value) in enumerate(data.items()):
print("P{0} ".format(key), end='')
if index == 0:
print("t={0}s ".format(0))
log("P{0} t={1}s ".format(key, save))
else:
print("t={0}s ".format(save))
log("P{0} t={1}s ".format(key, save))
average += save
count += 1
save = value
for index, (key, value) in enumerate(rest.items()):
print("P{0} ".format(key), end='')
print("t={0}s ".format(save))
log("P{0} t={1}s ".format(key, save))
average += save
count += 1
save += value
print("-" * SIZE * 3)
log("-" * SIZE * 3)
print("AVERAGE: {0}".format(average/count))
log("AVERAGE: {0}".format(average / count))
def __duling(self, duling_queue, duling_final_queue):
"""
Method that handles the duling algorithm
If there is no interruption, the processes will be
executed so that the one with the lowest priority will execute
first.Otherwise, as shown in the teacher's image, the processes
will go to the end of the queue, those who are interrupted
:param duling_queue:
:param duling_final_queue:
:return:
"""
global INTERRUPTION_TIME, SIZE, duling_queue_view
total_time = 0
history = list()
# time running
execute_time = 0
# capture start time of process execution.
data = dict()
# rest that remained to be executed, that is, processes that went to the queue
rest = dict()
print('-' * 23, end='')
print('[DULING]', end='')
print('-' * 21)
print("[+] Processos na fila de espera, pela menor prioridade, aguardando a execucao: \n", flush=True)
print("*" * 10, flush=True)
ordered_by_priority_queue = []
while duling_queue_view.qsize() > 0:
# remove process from the queue to display its details
proc = duling_queue_view.get()
# sjf_queue.put(proc)
ordered_by_priority_queue.append(proc)
# add to the total time, the execution time of each process.
total_time += proc.burst
if proc.preemptive:
proc.wait_time = INTERRUPTION_TIME
else:
proc.wait_time = proc.burst
# store processes to know the execution time of each
history.append(proc.wait_time)
# Here's the difference to the other methods. Below, the sorting by time (burst)
for n in range(len(ordered_by_priority_queue) - 1):
for m in range(len(ordered_by_priority_queue) - 1):
if ordered_by_priority_queue[m].priority > ordered_by_priority_queue[m + 1].priority:
aux = ordered_by_priority_queue[m]
ordered_by_priority_queue[m] = ordered_by_priority_queue[m + 1]
ordered_by_priority_queue[m + 1] = aux
# display only
for proc in ordered_by_priority_queue:
print(proc)
duling_queue.put(proc)
print("*" * 10)
print("[+] Executando escalonamento...")
print("[+] Duracao estimada de {0}s".format(total_time))
# traverse the history array
index = 0
# while there is a process, run the queue
while duling_queue.qsize() > 0:
# remove processo da fila para executar
proc = duling_queue.get()
# check if there is an interrupt
if proc.preemptive:
# if it exists, the process will be sent to the end of the queue, but not before executing for INTERRUPTION_TIME
print('!' * 10, end='')
print('INTERROMPIDO P{0}'.format(proc.id), end='')
print('!' * 10)
execute_time += INTERRUPTION_TIME
data[proc.id] = execute_time
print("[*] Houve interrupcao do processo {0}".format(proc.id))
print("[+] Esperando por {0}".format(INTERRUPTION_TIME))
sleep(INTERRUPTION_TIME)
# remove the history process
del history[index]
# reduces the value of INTERRUPTION_TIME in the process burst.
proc.burst -= INTERRUPTION_TIME
print("[+] Movendo processo {0} para o final da fila".format(proc.id))
print("[+] O processo {0} aguardara por {1} s".format(proc.id, sum(history)))
duling_final_queue.put(proc)
# skip to the next exit
continue
else:
print("+" * 30)
print("[*] Interrupcao nao encontrada para o processo {0}".format(proc.id))
print("[*] Executando P{0} por {1}s".format(proc.id, proc.burst))
execute_time += proc.burst
data[proc.id] = execute_time
index += 1
print("-" * 50)
print("[WAIT QUEUE]")
# running the processes that went to the end of the queue:
print("[+] Executando os processos que foram para o final da fila")
# time left to run
remains = sum(history)
while duling_final_queue.qsize() > 0:
proces = duling_final_queue.get()
print("[+] Retomando o processo {0} aos {1}s".format(proces.id, remains))
remains += process.burst
print("[+] Processo {0} executando por {1}s".format(proces.id, proces.burst))
rest[proces.id] = proces.burst
print("[+] Concluido")
print("\n\nRESULTADO [DULING]:")
log("\n\nRESULTADO [DULING]:")
print("-" * SIZE * 3)
log("-" * SIZE * 3)
save = 0
count = 0
# average of times (ALLOWS VERIFYING ALGORITHM PERFORMANCE):
average = 0
for index, (key, value) in enumerate(data.items()):
print("P{0} ".format(key), end='')
if index == 0:
print("t={0}s ".format(0))
log("P{0} t={1}s ".format(key, save))
else:
print("t={0}s ".format(save))
log("P{0} t={1}s ".format(key, save))
average += save
count += 1
save = value
for index, (key, value) in enumerate(rest.items()):
print("P{0} ".format(key), end='')
print("t={0}s ".format(save))
log("P{0} t={1}s ".format(key, save))
average += save
count += 1
save += value
print("-" * SIZE * 3)
log("-" * SIZE * 3)
# media is relative to priority and number of processes.
print("AVERAGE: {0}".format(average/count))
log("AVERAGE: {0}".format(average / count))
def finished(self, proc):
"""
Checks if the process in srt has finished
the execution, that is, if your working time
ended.
:param proc:
:return:
"""
if proc.burst == 0:
print("[+] Processo {0} terminou a execucao".format(proc.id))
return True
return False
def __srt(self, srt_queue, final_srt_queue):
"""
Method that handles the srt algorithm
If there is no interruption, the processes will be
executed so that if there is a process with less work,
then, the current process is interrupted to execute the other one, until it finishes. Otherwise,
as shown in the teacher's image, the processes
will go to the end of the queue, those who are interrupted
:param srt_queue:
:param final_srt_queue:
:return:
"""
pre_ordered = list()
ex_time = 0 # tempo estimado
print('-' * 23, end='')
print('[SRT]', end='')
print('-' * 21)
print("[+] Processos na fila de espera aguardando a execucao: \n", flush=True)
# feed the vector, to facilitate pre-sorting
while srt_queue.qsize() > 0:
proc = srt_queue.get()
print("*" * 10, flush=True)
print(proc)
pre_ordered.append(proc)
final_srt_queue.put(proc)
ex_time += proc.burst
print("[+] Duracao estimada de {0}s".format(ex_time))
for proc in range(len(pre_ordered) - 1):
for m in range(len(pre_ordered) - 1):
if pre_ordered[m].burst < pre_ordered[m + 1].burst:
aux = pre_ordered[m]
pre_ordered[m] = pre_ordered[m + 1]
pre_ordered[m + 1] = aux
data = dict()
storange = list()
amount = 0
space = 0
# with the sorted vector, can now make the comparisons
while final_srt_queue.qsize() > 0:
_process = final_srt_queue.get()
if self.finished(_process):
continue
space += 1
if _process.preemptive:
print("[+] Executando o processo {0} por {1}s".format(_process.id, INTERRUPTION_TIME))
sleep(INTERRUPTION_TIME)
_process.burst -= INTERRUPTION_TIME
# go back to the end of the queue
print("[+] Movendo o processo {0} para o final da fila".format(_process.id))
final_srt_queue.put(_process)
# remove preemption from the process, since it has already been executed
_process.preemptive = False
amount += INTERRUPTION_TIME
data[str(_process.id) + ' ' * space] = amount
storange.append(data)
# skip to the next
continue
else:
if pre_ordered[len(pre_ordered) - 1].id == _process.id:
print("[+] Executando processo {0} por {1}s".format(_process.id, _process.burst))
print("[+] Processo {0} terminou a execucao!".format(_process.id))
# if equal, remove from vector
pre_ordered.pop()
amount += _process.burst
data[str(_process.id) + ' ' * space] = amount
storange.append(data)
else:
print("[!] Encontrado processo com menor trabalho.")
print("[+] Executando processo {0} por {1}s".format(_process.id, INTERRUPTION_TIME))
_process.burst -= INTERRUPTION_TIME
print("[+] Buscando processo de menor espaço de tempo na CPU...")
print("[+] Encontrado.")
print("[+] Movendo o processo {0} para o final da fila".format(_process.id))
final_srt_queue.put(_process)
amount += INTERRUPTION_TIME
data[str(_process.id) + ' ' * space] = amount
storange.append(data)
# result display
# avoid repetition
show = set()
print("[+] Concluido")
print("\n\nRESULTADO [SRT]:")
log("\n\nRESULTADO [SRT]:")
print("-" * SIZE * 3)
log("-" * SIZE * 3)
save = 0
count = 0
# average of times (ALLOWS VERIFYING ALGORITHM PERFORMANCE):
average = 0
for proc in storange:
for index, (key, value) in enumerate(proc.items()):
# if it has repeated, it will be disregarded
if key in show:
continue
show.add(key)
print("P{0} ".format(key.strip()), end='')
if index == 0:
print("t={0}s ".format(0))
log("P{0} t={1}s ".format(key, save))
else:
print("t={0}s ".format(save))
log("P{0} t={1}s ".format(key, save))
average += save
count += 1
save = value
print("-" * SIZE * 3)
log("-" * SIZE * 3)
# media is relative to priority and number of processes.
print("AVERAGE: {0}".format(average / count))
log("AVERAGE: {0}".format(average / count))
@staticmethod
def _aux(process_rr, proc):
"""
[PROTECTED]Helper function to avoid massive repetition in the same code
calculates the estimated time for the process to return to the processor
:param process:
:return:
"""
temp = 0
for procc in process_rr:
if procc.id == proc.id:
continue
elif procc.preemptive:
temp += 1
else:
temp += procc.burst
print("[+] O processo {0} poderá aguardar por {1} s".format(proc.id, temp))
def __robin_round(self, rr_queue, final_rr_queue):
global INTERRUPTION_TIME, SIZE
"""
Method that handles the robin round algorithm
If there is no interruption, the processes will be
executed based on the quantum. Otherwise,
as shown in the teacher's image, the processes
will go to the end of the queue, those who are interrupted
:return:
"""
total_time = 0
rr_queue_view = self.ready_queue
# time running
execute_time = 0
print('-' * 23, end='')
print('[RR]', end='')
print('-' * 21)
print("[+] Processos na fila de espera, pelo quantum, aguardando a execucao: \n", flush=True)
print("*" * 10, flush=True)
# store , exclusively, to measure the return time of the interrupted process
process_rr = set()
while rr_queue_view.qsize() > 0:
# remove process from the queue to display its details
proc = rr_queue_view.get()
print(proc)
process_rr.add(proc)
# add to the total time, the execution time of each process.
total_time += proc.burst
if proc.preemptive:
proc.wait_time = INTERRUPTION_TIME
else:
proc.wait_time = proc.burst
print("*" * 10)
# list to store processes in order and display them later
results = []
print("[+] Executando escalonamento...")
print("[+] Duracao estimada de {0}s".format(total_time))
# traverse the history array
index = 0
# while there is a process, run the queue
while rr_queue.qsize() > 0:
# remove process from queue to run
proc = rr_queue.get()
# capture start time of process execution.
data = dict()
# check if there is still runtime for the process in question
if proc.burst > 0:
# check if there is an interrupt
if proc.preemptive:
# if it exists, the process will be sent to the end of the queue, but not before executing for
# INTERRUPTION_TIME
print('!' * 10, end='')
print('INTERROMPIDO P{0}'.format(proc.id), end='')
print('!' * 10)
execute_time += INTERRUPTION_TIME
data[proc.id] = execute_time
print("[*] Houve interrupcao do processo {0}".format(proc.id))
print("[+] Esperando por {0}s".format(INTERRUPTION_TIME))
sleep(INTERRUPTION_TIME)
# reduces the value of INTERRUPTION_TIME in the process burst.
proc.burst -= INTERRUPTION_TIME
print("[+] Movendo processo {0} para o final da fila".format(proc.id))
results.append(data)
# removing need for forced interrupt, after one has already been performed
proc.preemptive = False
# analyzing the possible waiting time of this interrupted process in the queue,
# before the next run
self._aux(process_rr, proc)
# return to the end of the queue
rr_queue.put(proc)
# skip to the next exit
continue
else:
print("+" * 30)
print("[*] Interrupcao forçada nao encontrada para o processo {0}".format(proc.id))
print("[!] Porem, como este algoritmo e preemptivo, sera interrompido por {0}s".
format(INTERRUPTION_TIME))
proc.burst -= INTERRUPTION_TIME
print("[*] Executando P{0} por {1}s".format(proc.id, INTERRUPTION_TIME))
print("[*] Processo {0} retornando ao final da fila...".format(proc.id))
# analyzing the possible waiting time of this interrupted process in the queue,
# before the next run
self._aux(process_rr, proc)
execute_time += proc.quantum
data[proc.id] = 1
results.append(data)
# return to the end of the queue
rr_queue.put(proc)
index += 1
else:
print("[+] O processo {0} terminou a sua execucao".format(proc.id))
# display the result
print("\n[+] Resultado [RR]")
log("\n\nRESULTADO [RR]:")
print("-" * SIZE * 3)
log("-" * SIZE * 3)
save = 0
# average of times (ALLOWS VERIFYING ALGORITHM PERFORMANCE)
average = 0
index = 0
# will save the processes, without repetition, to calculate the time precisely.
# as said in class, though, it doesn't make much sense
different_p = set()
# corresponding time
different_t = set()
for result in results:
for (key, value) in result.items():
print("P{0} ".format(key), end='')
different_p.add(key)
different_t.add(save)
if index == 0:
print("t={0}s ".format(0))
log("P{0} t={1}s ".format(key, save))
else:
print("t={0}s ".format(save))
log("P{0} t={1}s ".format(key, save))
save += INTERRUPTION_TIME
index += 1
print("-" * SIZE * 3)
log("-" * SIZE * 3)
for n in range(len(different_p)):
# add the matches, before starting to repeat
average += different_t.pop()
print("AVERAGE: {0}".format(average / SIZE))
log("AVERAGE: {0}".format(average / SIZE))
def start(self):
"""
Calls the private methods of the M algorithms
and start operations
:return:
"""
global fscfs_queue, final_queue, sjf_queue, final_sjf_queue, duling_queue, final_duling_queue_queue, srt_queue,\
final_srt_queue, rr_queue, final_rr_queue
# --------------- FCFS ----------------
self.__fcfs(fscfs_queue, final_queue)
print('-' * 50)
# --------------- SJF -----------------
self.__sjf(sjf_queue, final_sjf_queue)
print('-' * 50)
# -------------- Dulling --------------
self.__duling(duling_queue, final_duling_queue_queue)
print('-' * 50)
# -------------- SRT --------------
self.__srt(srt_queue, final_srt_queue)
print('-' * 50)
# -------------- RR --------------
self.__robin_round(rr_queue, final_rr_queue)
print('-' * 50)
if __name__ == '__main__':
sjf_queue_view = Queue()
duling_queue_view = Queue()
fscfs_queue = Queue()
final_queue = Queue()
sjf_queue = Queue()
final_sjf_queue = Queue()
duling_queue = Queue()
final_duling_queue_queue = Queue()
srt_queue = Queue()
final_srt_queue = Queue()
rr_queue = Queue()
final_rr_queue = Queue()
# user interaction V
log("SHEDULER\n")
try:
how_many_process = int(input("[+] Quantos processos serao executados? "))
# R
SIZE = how_many_process
for amount in range(how_many_process):
time = int(input("[+] Tempo para o processo {0}: ".format(amount)))
interruption = bool(input("[+] O processo {0} sera interrompido? [sim/nao] ".format(amount)).lower()
.replace('sim', 'True')\
.replace('nao', ''))
process = Process(id=amount,
burst=time,
preemtive=interruption,
priority=random.randint(0, 10))
READY_QUEUE.put(process)
fscfs_queue.put(process)
sjf_queue_view.put(process)
srt_queue.put(process)
rr_queue.put(process)
duling_queue_view.put(process)
print('-' * 50)
# save the queue in scheduler A
sheduler = Sheduler(READY_QUEUE)
sheduler.start()
log()
except ValueError as e:
print("[-] Entrada invalida! {0}".format(e))