This repository has been archived by the owner on Mar 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathround_robin.py
86 lines (76 loc) · 3.61 KB
/
round_robin.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from escalonador import Escalonador
from termcolor import colored
class Round_Robin(Escalonador):
__tempo_executando = 0
def __init__(self, file, quantum):
self.quantum = quantum
self.descricao = 'Quantum: {}'.format(self.quantum)
super().__init__(file)
self.cabecalho = 'Tempo\tProcessos (P_# / tempo_total ms / tempo_restante ms)'
self.cabecalho_efeitos = (colored(self.cabecalho,'cyan', None, ['bold']))
self.timeline_linhas.append(self.cabecalho_efeitos)
def escalonar(self):
if self.quantidade_estado('Pronto') == 0:
return None
key = ((self.processo_executando+1) % len(self.processando))
while self.processando[key].estado != 'Pronto':
key = ((key+1) % len(self.processando))
if key != self.processo_executando:
self.processo_executando = key
return self.processando[key]
def executar(self, processo):
self.__tempo_executando = 0
if processo.tipo == 'user':
processo.tempo_de_entrada = self.timeline
processo.estado = 1
quant_processos = len(self.processando)
atualizar_saida = True
while self.__tempo_executando != self.quantum and processo.estado == 'Executando':
if atualizar_saida:
atualizar_saida = False
self.mostrar_timeline()
self.__tempo_executando += 1
self.timeline += 1
self.verificar_entrada_de_processos()
if (len(self.processando)) > quant_processos:
quant_processos = len(self.processando)
atualizar_saida = True
if processo.tipo == 'user':
processo.tempo_executado = 1
if ((processo.eventos_I_O > 0) and (processo.tempo_executado % processo.eventos_I_O) == 0) and (processo.tempo_restante > 0):
processo.estado = 2
self.GER_I_O.estado = 0
if processo.tempo_restante == 0:
processo.estado = 3
elif processo.tipo == 'system':
if self.quantidade_estado('Bloqueado para I/O') == 0:
processo.estado = 4
else:
self.simular_I_O()
self.atraso_ms()
processo.tempo_de_saida = (self.timeline+1)
if processo.estado == 'Executando':
processo.estado = 0
def montar_linhas(self):
linha = '{:02d}\t'.format(self.timeline)
for i in range(len(self.processando)):
if self.processando[i].estado:
if self.processando[i].tipo == 'user':
aux = 'P_{0.id} / {0.tempo_total:02d}ms / {0.tempo_restante:02d}ms'.format(self.processando[i])
else:
aux = 'P_{0.id} / Tratamento de I/O'.format(self.processando[i])
if self.processando[i].estado == 'Pronto':
aux = colored(aux, 'cyan', None, ['bold'])
elif self.processando[i].estado == 'Executando':
aux = colored(aux, 'yellow', None, ['underline', 'bold'])
elif self.processando[i].estado == 'Bloqueado para I/O':
aux = colored(aux, 'red', None, ['bold'])
elif self.processando[i].estado == 'Concluído':
aux = colored(aux, 'green', None, ['bold'])
elif self.processando[i].estado == 'Suspenso':
aux = colored(aux, 'cyan', None, ['dark'])
linha += aux
linha += ' | '
self.timeline_linhas.append(linha)