-
Notifications
You must be signed in to change notification settings - Fork 0
/
SchedulingAlgorithm.java
80 lines (76 loc) · 3.83 KB
/
SchedulingAlgorithm.java
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
// Run() is called from Scheduling.main() and is where
// the scheduling algorithm written by the user resides.
// User modification should occur within the Run() function.
import java.util.Vector;
import java.io.*;
public class SchedulingAlgorithm {
public static Results Run(int runtime, Vector processVector, Results result) {
int i = 0;
int comptime = 0;
int currentProcess = 0;
int previousProcess = 0;
int size = processVector.size();
int completed = processVector.size();
String resultsFile = "Summary-Processes";
result.schedulingType = "Batch (Nonpreemptive)";
result.schedulingName = "First-Come First-Served";
try {
//BufferedWriter out = new BufferedWriter(new FileWriter(resultsFile));
//OutputStream out = new FileOutputStream(resultsFile);
PrintStream out = new PrintStream(new FileOutputStream(resultsFile));
sProcess process = (sProcess) processVector.elementAt(currentProcess);
out.println("Process: " + currentProcess + " registered... (" + process.cputime + " " + process.ioblocking + " " + process.cpudone + " " + process.cpudone + ")");
// this is the amount of computation time alloted for the process. The runtime is how long it's allowed
// to run without throwing an error.
while (comptime < runtime) {
// out.println("comptime is: " + comptime + " and runtime is " + runtime);
if (process.cpudone == process.cputime) {
completed--;
out.println("Process: " + currentProcess + " completed... (" + process.cputime + " " + process.ioblocking + " " + process.cpudone + " " + process.cpudone + ")");
if (completed == 0) {
result.compuTime = comptime;
out.close();
return result;
}
out.println("Process " + currentProcess + " has finished successfully");
for (i = size - 1; i >= 0; i--) {
process = (sProcess) processVector.elementAt(i);
out.println("For process " + i + " cpudone is " + process.cpudone + " And cputime is " + process.cputime);
if (process.cpudone < process.cputime) {
out.println("Since .cpudone < .cputime in the completion block, setting currentProcess to " + i);
currentProcess = i;
break;
}
}
process = (sProcess) processVector.elementAt(currentProcess);
out.println("Process: " + currentProcess + " registered... (" + process.cputime + " " + process.ioblocking + " " + process.cpudone + " " + process.cpudone + ")");
}
if (process.ioblocking == process.ionext) {
out.println("Process: " + currentProcess + " I/O blocked... (" + process.cputime + " " + process.ioblocking + " " + process.cpudone + " " + process.cpudone + ")");
process.numblocked++;
process.ionext = size - 1;
out.println("Previous process used to be " + previousProcess + " and is now " + currentProcess);
previousProcess = currentProcess;
for (i = size - 1; i >= 0; i--) {
process = (sProcess) processVector.elementAt(i);
if (process.cpudone < process.cputime && previousProcess != i) {
out.println("the current process is being set to " + i);
currentProcess = i;
break;
}
}
process = (sProcess) processVector.elementAt(currentProcess);
out.println("Process: " + currentProcess + " registered... (" + process.cputime + " " + process.ioblocking + " " + process.cpudone + " " + process.cpudone + ")");
}
process.cpudone++;
if (process.ioblocking > 0) {
process.ionext++;
}
comptime++;
}
out.close();
} catch (IOException e) { /* Handle exceptions */ }
result.compuTime = comptime;
return result;
}
}