-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRR.java
More file actions
218 lines (189 loc) · 6.11 KB
/
RR.java
File metadata and controls
218 lines (189 loc) · 6.11 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
/*
* Evan Thompson, Tausif Ahmed
*/
import java.util.ArrayList;
public class RR extends CPU_Algorithm {
private int slice_time;
private int time;
/**
* @param processes: current time
* @param num_cpus: # of cpus
* @param s: time slice value
* @effect initialize RR (Round Robin) object
* @modifies slice_time, in_procs
*/
public RR(ArrayList<Process> processes, int num_cpus, int s) {
copy_in_procs(processes);
super.NUM_PROCESSES = procs.size();
super.NUM_CPUS = num_cpus;
slice_time = s;
}
/**
* @param t: current time
* @effect load the next processes from procs into the curr_proc buffer
* @modifies curr_proc, procs
*/
@Override
protected void get_next_procs(int t){
// If this is the initial first processes
if(curr_procs.size() == 0) {
for(int i = 0; i < super.NUM_CPUS; i++) {
Process tmp = null;
curr_procs.add(tmp);
}
}
// Get the first n processes, fill in the null slots
for(int i = 0; i < curr_procs.size(); i++) {
if(procs.size() > 0 && curr_procs.get(i) == null) {
curr_procs.set(i, procs.remove(0));
curr_procs.get(i).set_wait(time);
}
}
}
/**
* @param cpu_slice: list of cpu slices
* @effect add another slice to cpu_slice
* @modifies cpu_slice
*/
public void setup_slice(ArrayList<Integer> cpu_slice) {
for(int i = 0; i < super.NUM_CPUS; i++) {
cpu_slice.add(new Integer(slice_time));
}
}
/**
* @param i: context index
* @param context_cpu: list of context times
* @param tmp: Process of interest
* @effect print the context time of tmp
*/
private void print_context_switch(int i, ArrayList<Integer> context_cpu, Process tmp) {
curr_procs.set(i, null);
get_next_procs(time);
if(curr_procs.get(i) != null) {
System.out.println("[time " + time + "ms] Context switch (swapping out process ID " + tmp.get_pid() + " for process ID " + curr_procs.get(i).get_pid() + ")");
context_cpu.set(i, 4);
curr_procs.get(i).set_wait(time);
curr_procs.get(i).activate_burst();
} else {
System.out.println("[time " + time + "ms] Context switch (swapping out process ID " + tmp.get_pid() + " but no process to replace it)");
context_cpu.set(i, 2);
}
}
/**
* @param context_cpu: list of context times
* @param cpu_slice: list of cpu_slices
* @effect handle the context and cpu_slices
*/
public void context_handle_processes(ArrayList<Integer> context_cpu, ArrayList<Integer> cpu_slice) {
for(int i = 0; i < curr_procs.size(); i++) {
if(context_cpu.get(i) == 0 && cpu_slice.get(i) != 0 && curr_procs.get(i) != null) {
Process tmp = curr_procs.get(i);
if(!tmp.is_active()) {
tmp.activate_burst();
tmp.set_wait(time);
}
tmp.dec_curr_burst();
cpu_slice.set(i, cpu_slice.get(i) - 1);
// If the burst process is completed
if(!tmp.is_active()) {
// Set turnaround
if(tmp.get_wait() == 0) {
tmp.set_turnaround(time + 1);
} else {
tmp.set_turnaround(time + 1);
}
// Setup for I/O blocking
print_proc_end(tmp, time);
// Add to blocked procs
if(!tmp.finished()) {
int val = gen_num(IO_BLOCK_RANGE, IO_BLOCK_OFF);
int burst_val;
if(tmp.is_interactive()) {
burst_val = gen_num(BURST_RANGE, BURST_OFF);
} else {
burst_val = gen_num(CPU_BURST_RANGE, CPU_BURST_OFF);
}
tmp.set_burst(burst_val);
tmp.set_blocked_time(val);
blocked_procs.add(tmp);
}
// Remove from curr_procs
curr_procs.set(i, null);
// Set slice back to slice time
cpu_slice.set(i, slice_time);
// Context switch
get_next_procs(time);
if(curr_procs.get(i) != null) {
System.out.println("[time " + time + "ms] Context switch (swapping out process ID " + tmp.get_pid() + " for process ID " + curr_procs.get(i).get_pid() + ")");
context_cpu.set(i, new Integer(4));
} else {
System.out.println("[time " + time + "ms] Context switch (swapping out process ID " + tmp.get_pid() + " but no process to replace it)");
context_cpu.set(i, new Integer(2));
}
} else if(cpu_slice.get(i) == 0) {
// Bump tmp into the ready queue
procs.add(tmp);
// Add context switch
curr_procs.set(i, null);
get_next_procs(time);
cpu_slice.set(i, slice_time);
if(curr_procs.get(i) != null) {
System.out.println("[time " + time + "ms] Context switch (swapping out process ID " + tmp.get_pid() + " for process ID " + curr_procs.get(i).get_pid() + ")");
context_cpu.set(i, new Integer(4));
} else {
System.out.println("[time " + time + "ms] Context switch (swapping out process ID " + tmp.get_pid() + " but no process to replace it)");
context_cpu.set(i, new Integer(2));
}
}
} else {
if(curr_procs.get(i) == null) {
get_next_procs(time);
if(curr_procs.get(i) != null) {
context_cpu.set(i, context_cpu.get(i) + 2);
if(curr_procs.get(i).is_interactive()) {
System.out.println("[time " + time + "ms] " + curr_procs.get(i).get_type() + " process ID " + curr_procs.get(i).get_pid() + " has taken unused cpu, " +
(i + 1));
}
}
}
}
}
}
/**
* @effect activate the processes within the cpus
*/
private void activate_curr_procs() {
for(int i = 0; i < curr_procs.size(); i++) {
if(curr_procs.get(i) != null) {
curr_procs.get(i).activate_burst();
}
}
}
/**
* @effect execute the algorithm
*/
@Override
public void exec() {
System.out.println("---- Executing RR ----");
time = 0;
ArrayList<Process> all_procs = new ArrayList<Process>(procs);
set_start_ready();
print_ready_entry();
ArrayList<Integer> context_cpu = new ArrayList<Integer>();
ArrayList<Integer> cpu_slice = new ArrayList<Integer>();
blocked_procs = new ArrayList<Process>();
curr_procs = new ArrayList<Process>();
get_next_procs(time);
activate_curr_procs();
setup_context_cpu(context_cpu);
setup_slice(cpu_slice);
while(!should_stop()) {
context_handle_processes(context_cpu, cpu_slice);
handle_blocked_processes(time);
dec_context_switch(context_cpu, time);
remove_finished_procs(time);
time++;
}
display_data(all_procs, time);
}
}