-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProcess.java
More file actions
382 lines (341 loc) · 8.59 KB
/
Process.java
File metadata and controls
382 lines (341 loc) · 8.59 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
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
/*
* Evan Thompson, Tausif Ahmed
*/
import java.util.ArrayList;
import java.util.Comparator;
public class Process implements Comparable<Process>, Comparator<Process>{
// Constant
private Integer INV_CPU = new Integer(-1);
private Integer MAX_BURST = new Integer(5); // Six in total when starting from 0
private boolean interactive;
private String pid;
private Integer num_bursts;
private Integer burst; // Between 20-200ms
private Integer wait;
private Integer turnaround;
private Integer num_times_blocked;
private Integer blocked_time;
private Integer ready_start;
private Integer curr_burst_time;
private ArrayList<Integer> all_turnarounds;
private ArrayList<Integer> all_waits;
/**
* @param p the process id
* @param t_req the required time for completion of the process
* @param b the burst time in ms
* @param inter whether or not it is interactive
* @param c_time the time required for a cpu_burst. If inter = true, cpu_time = -1
*/
public Process(String p, int b, boolean inter) {
pid = new String(p);
num_times_blocked = new Integer(0);
burst = new Integer(b);
all_turnarounds = new ArrayList<Integer>();
all_waits = new ArrayList<Integer>();
interactive = inter;
curr_burst_time = new Integer(0);
num_bursts = new Integer(0);
wait = new Integer(0);
turnaround = new Integer(0);
blocked_time = new Integer(0);
ready_start = new Integer(0);
}
/**
* @param other: Process to be copied
* @effect copies all data from other to this Process
* @modifies all variables
*/
public Process(Process other) {
num_times_blocked = new Integer(other.num_times_blocked);
all_turnarounds = new ArrayList<Integer>();
all_waits = new ArrayList<Integer>();
curr_burst_time = new Integer(other.curr_burst_time);
blocked_time = new Integer(other.blocked_time);
pid = other.get_pid();
burst = new Integer(other.burst);
interactive = other.interactive;
num_bursts = new Integer(other.num_bursts);
ready_start = new Integer(other.ready_start);
wait = new Integer(other.wait);
turnaround = new Integer(other.turnaround);
}
/**
* @param p: Process to be compared to
* @effect checks which Process has a smaller CPU burst time
* @return int that tells which Process has a smaller burst time
*/
@Override
public int compareTo(Process p) {
if(this.get_burst() > p.get_burst()) {
return 1;
}
if(this.get_burst() < p.get_burst()) {
return -1;
}
return 0;
}
/**
* @param p1: first Process
* @param p2: second Process
* @effect gets the difference between the burst times
* @return difference of burst times
*/
@Override
public int compare(Process p1, Process p2) {
return p1.get_burst() - p2.get_burst();
}
/**
* @return pid of the process
*/
public String get_pid() {
return new String(pid);
}
/**
* @return the length of a burst
*/
public Integer get_burst() {
return new Integer(burst);
}
/**
* @return remaining cpu burst time
*/
public Integer get_remaining_burst() {
return new Integer(curr_burst_time);
}
/**
* @param val: new burst time
* @effect changes burst time of the process
* @modifies burst
*/
public void set_burst(int val) {
burst = new Integer(val);
}
/**
* @return string indicating what type of process
*/
public String get_type() {
if(!interactive) { return "CPU-Bound"; }
return "Interactive";
}
/**
* @return true if the process is currently blocked on i/o
*/
public boolean is_blocked() {
if(blocked_time > 0) { return true; }
return false;
}
/**
* @return true if the process is currently in its burst, false otherwise
*/
public boolean is_active() {
if(curr_burst_time > 0 && blocked_time == 0) { return true; }
return false;
}
/**
* @return true if the process is interactive with the user. False if cpu bound
*/
public boolean is_interactive() {
return interactive;
}
/**
*
* @return the number of burst that have occured
*/
public int get_num_bursts() {
return num_bursts;
}
/**
* @effect starts the burst for this process
*/
public void activate_burst() {
if(curr_burst_time == 0) {
curr_burst_time = burst;
num_bursts++;
}
}
/**
* @effect decrements the current burst time if it is greator than 0
*/
public void dec_curr_burst() {
if(curr_burst_time > 0) {
curr_burst_time--;
}
}
/**
* @return the total turnaround time for the process (start to finish)
*/
public Integer get_turnaround() {
return new Integer(turnaround);
}
/**
* @param time: the time that the process enters the ready queue
*/
public void set_ready_entry(int time) {
ready_start = time;
}
/**
* @param time: the time that the process completes its burst
*/
public void set_turnaround(int time) {
turnaround = new Integer(time - ready_start);
if(wait != 0) { turnaround--; }
all_turnarounds.add(new Integer(turnaround));
if(turnaround < all_waits.get((all_turnarounds.size()-1))) {
all_waits.set((all_turnarounds.size()-1), wait);
}
}
/**
* @param wait: new wait time
* @effect adds new wait time to Process
* @modifies all_waits
*/
public void set_wait(int time) {
wait = new Integer(time - ready_start);
if(ready_start != 0){ wait--; }
all_waits.add(new Integer(wait));
}
/**
* @return wait time
*/
public Integer get_wait() {
return new Integer(wait);
}
/**
* @return the maximum number of burst allowed for the process
*/
public Integer get_max_burst() {
return new Integer(MAX_BURST);
}
/**
* @return true if the maximum number of bursts have been used (cpu bound) or the
* @return computation is done. false otherwise
*/
public boolean finished() {
if(!interactive && MAX_BURST <= num_bursts) {
if(curr_burst_time == 0){
return true;
}
}
return false;
}
/**
* @param time: the time to be blocked for
* @effect blocks the process
*/
public void set_blocked_time(int time) {
blocked_time = time;
num_times_blocked++;
}
/**
* @effect decrements the blocked timer
*/
public void dec_blocked_time() {
if(blocked_time > 0) { blocked_time--; }
}
/**
* @return average turnaround time
*/
public Double get_avg_turnaround() {
Double sum = new Double(0);
for(int i = 0; i < all_turnarounds.size(); i++) {
sum += new Double(all_turnarounds.get(i));
}
return sum / all_turnarounds.size();
}
/**
* @return average wait time
*/
public Double get_avg_wait() {
Double sum = new Double(0);
for(int i = 0; i < all_waits.size(); i++) {
sum += new Double(all_waits.get(i));
}
return sum / all_waits.size();
}
/**
* @return minimum turnaround time
*/
public Integer get_min_turnaround(){
Integer min = all_turnarounds.get(0);
for(int i = 0; i < all_turnarounds.size(); i++) {
if(min > all_turnarounds.get(i)) {
min = all_turnarounds.get(i);
}
}
return new Integer(min);
}
/**
* @return maximum turnaround time
*/
public Integer get_max_turnaround() {
Integer max = all_turnarounds.get(0);
for(int i = 0; i < all_turnarounds.size(); i++) {
if(max < all_turnarounds.get(i)) {
max = all_turnarounds.get(i);
}
}
return new Integer(max);
}
/**
* @return maximum wait time
*/
public Integer get_max_wait() {
Integer max = all_waits.get(0);
for(int i = 0; i < all_waits.size(); i++) {
if(max < all_waits.get(i)) {
max = all_waits.get(i);
}
}
return new Integer(max);
}
/**
* @return minimum wait time
*/
public Integer get_min_wait() {
Integer min = all_waits.get(0);
for(int i = 0; i < all_waits.size(); i++) {
if(min > all_waits.get(i)) {
min = all_waits.get(i);
}
}
return new Integer(min);
}
/**
* @return total cpu use time
*/
public int get_cpu_use_time() {
int sum = 0;
for(int i = 0; i < all_turnarounds.size(); i++) {
sum += (all_turnarounds.get(i) - all_waits.get(i));
}
return sum;
}
/**
* @return average cpu use time
*/
public double get_avg_cpu_time() {
double sum = 0;
double all_wait_time = 0;
for(int i = 0; i < all_turnarounds.size(); i++) {
sum += (all_turnarounds.get(i) - all_waits.get(i));
all_wait_time += all_turnarounds.get(i);
}
if(sum < 0) { sum *= -1; }
return sum / all_wait_time * 100;
}
/**
* @return string representation of process
*/
public String toString() {
return "(" + pid + ")\n" +
"burst: " + burst + "ms\n" +
"interactive? " + interactive + "\n" +
"num burst: " + num_bursts + "\n" +
"total_wait " + wait + "ms\n" +
"turnaround " + turnaround + "ms\n" +
"curr burst: " + curr_burst_time + "ms\n" +
"curr blocked time: " + blocked_time + "ms\n" +
"num times blocked: " + num_times_blocked + "\n" +
"finished? " + finished();
}
}