-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRoundRobin.java
More file actions
212 lines (190 loc) · 8.64 KB
/
RoundRobin.java
File metadata and controls
212 lines (190 loc) · 8.64 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
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
public class RoundRobin {
public static void main(String[] args) throws IOException {
// pede ao usuário para inserir o quantum
Scanner input = new Scanner(System.in);
System.out.print("Insira o Quantum: ");
int quantum = input.nextInt();
input.close();
// lê o arquivo de entrada
File file = new File("entrada.txt");
Scanner scanner;
try {
scanner = new Scanner(file);
} catch (FileNotFoundException e) {
System.out.println("Arquivo não encontrado!");
return;
}
// cria lista de processos
ArrayList<Processo> processos = new ArrayList<>();
FileWriter escrever = new FileWriter("gantt.txt");
// lê cada linha do arquivo e cria um processo correspondente
while (scanner.hasNextLine()) {
String[] line = scanner.nextLine().split(" ");
String pid = line[0];
int duracao = Integer.parseInt(line[1]);
int chegada = Integer.parseInt(line[2]);
boolean io = false;
ArrayList<Integer> ioInstantes = new ArrayList<>();
if (line.length > 3) {
io = true;
String[] ioString = line[3].split(",");
for (String ioInstante : ioString) {
ioInstantes.add(Integer.parseInt(ioInstante));
}
}
processos.add(new Processo(pid, duracao, chegada, io, ioInstantes));
}
scanner.close();
// executa o escalonamento Round-Robin
System.out.println("***********************************");
System.out.println("***** ESCALONADOR ROUND ROBIN *****");
System.out.println("-----------------------------------");
System.out.println("------- INICIANDO SIMULAÇÃO -------");
System.out.println("-----------------------------------");
int tempo = 0;
ArrayList<Processo> fila = new ArrayList<>();
Processo cpu = null;
System.out.println("********** TEMPO " + tempo + " **************");
StringBuilder graficoGantt = new StringBuilder();
StringBuilder tempos = new StringBuilder();
StringBuilder eventos = new StringBuilder(); // Armazena os eventos
while (!processos.isEmpty() || !fila.isEmpty()) {
for (Processo processo : processos) {
if (processo.chegada == tempo && tempo > 0) {
fila.add(processo);
System.out.println("#[evento] CHEGADA <" + processo.pid + ">");
if (cpu == null) {
cpu = fila.remove(0);
}
} else if (processo.chegada == tempo) {
fila.add(processo);
if (cpu == null) {
cpu = fila.remove(0);
System.out.println("FILA: " + imprimirFila(fila, cpu));
System.out.println("CPU: " + cpu.toString());
} else {
System.out.println("CPU: " + cpu.toString());
}
}
}
if (cpu != null) {
cpu.duracao--;
graficoGantt.append(cpu.pid);
cpu.tempoCPU++; // Atualiza o tempo que o processo ficou na CPU
cpu.cpuTempo++; // Atualiza o tempo de CPU específico do processo
if (cpu.duracao == 0) {
eventos.append("#[evento] ENCERRANDO <").append(cpu.pid).append(">");
cpu.tempoEspera = (tempo - cpu.chegada) - cpu.tempoCPU + 1;
cpu = null;
if (!fila.isEmpty()) {
cpu = fila.remove(0);
cpu.cpuTempo = 0; // Reseta o tempo de CPU para o novo processo
}
} else {
if (cpu.io && !cpu.ioInstantes.isEmpty() && cpu.tempoCPU == cpu.ioInstantes.get(0)) {
cpu.ioInstantes.remove(0);
fila.add(cpu);
eventos.append("#[evento] OPERAÇÃO I/O <").append(cpu.pid).append(">");
cpu = null;
if (!fila.isEmpty()) {
cpu = fila.remove(0);
cpu.cpuTempo = 0; // Reseta o tempo de CPU para o novo processo
}
} else if (cpu.cpuTempo == quantum) {
fila.add(cpu);
eventos.append("#[evento] FIM QUANTUM <").append(cpu.pid).append(">");
cpu = null;
if (!fila.isEmpty()) {
cpu = fila.remove(0);
cpu.cpuTempo = 0; // Reseta o tempo de CPU para o novo processo
}
}
}
} else {
System.out.println("ACABARAM OS PROCESSOS!!");
System.out.println("-----------------------------------");
System.out.println("------- Encerrando simulação -------");
System.out.println("-----------------------------------");
break;
}
tempo++;
System.out.println("********** TEMPO " + tempo + " **************");
if (eventos.length() > 0) {
System.out.println(eventos); // Imprime os eventos
eventos.setLength(0); // Limpa a variável de eventos
}
System.out.println("FILA: " + imprimirFila(fila, cpu));
if (cpu != null) {
System.out.println("CPU: " + cpu.toString());
}
// Atualiza o gráfico de Gantt e as marcações de tempo
graficoGantt.append("|");
tempos.append(tempo);
if (tempo < 10) {
tempos.append(" |");
} else {
tempos.append("|");
}
}
// Imprime o gráfico de Gantt e as marcações de tempo
escrever.write("\nGráfico de Gantt:\n");
escrever.write("|" + graficoGantt.toString() + "\n");
escrever.write("|" + tempos.toString() + "\n");
escrever.write("\nTempo de espera dos processos: \n");
for (Processo processo : processos) {
escrever.write(processo.pid + ": " + processo.tempoEspera + "\n");
}
// Calcula o tempo médio de espera
int totalTempoEspera = 0;
for (Processo processo : processos) {
totalTempoEspera += processo.tempoEspera;
}
double tempoMedioEspera = (double) totalTempoEspera / processos.size();
escrever.write("Tempo Médio de Espera: " + tempoMedioEspera);
escrever.close();
}
// método auxiliar para imprimir a fila de processos
public static String imprimirFila(ArrayList<Processo> fila, Processo cpu) {
if (fila.isEmpty() || (fila.size() == 0 && cpu != null && fila.get(0).pid.equals(cpu.pid))) {
return "Não há processos na fila";
}
StringBuilder filaString = new StringBuilder();
for (int i = 0; i < fila.size(); i++) {
Processo processo = fila.get(i);
if (cpu == null || !processo.pid.equals(cpu.pid)) {
filaString.append(processo.toString()).append(" ");
}
}
return filaString.toString();
}
}
class Processo {
String pid; // Nome do processo
int duracao;// Duração do processo
int chegada; // Tempo que o processo chegou
boolean io; // Verifica se o processo tem i\o
ArrayList<Integer> ioInstantes; // Guarda os momentos de I\O dos processos
int tempoCPU; // Tempo que o processo ficou na CPU
int cpuTempo; // Tempo de CPU específico do processo
int tempoEspera; // Tempo de espera do processo
public Processo(String pid, int duracao, int chegada, boolean io, ArrayList<Integer> ioInstantes) {
this.pid = pid;
this.duracao = duracao;
this.chegada = chegada;
this.io = io;
this.ioInstantes = ioInstantes;
this.tempoCPU = 0;
this.cpuTempo = 0;
this.tempoEspera = 0;
}
@Override
public String toString() {
return pid + "(" + duracao + ")";
}
}