-
Notifications
You must be signed in to change notification settings - Fork 1
/
PreemptiveScheduler.java
133 lines (108 loc) · 3.45 KB
/
PreemptiveScheduler.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
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
/**
* (c) 2015 Maróti Ádám
*
* */
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.util.Queue;
import java.util.Vector;
import java.util.concurrent.LinkedBlockingQueue;
public class PreemptiveScheduler extends SchedulingAlgorithm {
private Queue<sProcess> line = new LinkedBlockingQueue<sProcess>();
public static final int sliceSize = 25;
public Results Run(int runtime, Vector processVector, Results result) {
int i = 0;
int comptime = 0;
int size = processVector.size();
int completed = 0;
String resultsFile = "Summary-Processes";
result.schedulingType = "Round-Robin (Preemptive)";
result.schedulingName = "First-Come First-Served with respect to max time slice.";
try {
// BufferedWriter out = new BufferedWriter(new
// FileWriter(resultsFile));
// OutputStream out = new FileOutputStream(resultsFile);
PrintStream out = new PrintStream(new FileOutputStream(resultsFile));
initQueue(processVector);
sProcess process = getNextProcess();
process.remainFromSlice = sliceSize;
out.println("Process registered: " + process);
System.out.println("Process registered: " + process + " init");
while (comptime < runtime) {
if (process.cpudone == process.cputime) {
completed++;
out.println("Process completed: " + process);
System.out.println("Process completed: " + process);
if (completed == size) {
result.compuTime = comptime;
out.close();
return result;
}
process = getNextProcess();
out.println("Process registered: " + process);
System.out.println("Process registered: " + process
+ " done");
continue;
}
if (process.remainFromSlice == 0) {
out.println("Process interrupted (slice is over)...: "
+ process);
System.out
.println("Process interrupted (slice is over)...: "
+ process);
process.remainFromSlice = PreemptiveScheduler.sliceSize;
line.add(process);
process = getNextProcess();
out.println("Process registered: " + process);
System.out.println("Process registered: " + process
+ " sliceisover");
continue;
}
if (process.ioblocking == process.ionext) {
out.println("Process I/O blocked...: " + process);
System.out.println("Process I/O blocked...: " + process);
process.numblocked++;
process.ionext = 0;
process.remainFromSlice = PreemptiveScheduler.sliceSize;
line.add(process);
process = getNextProcess();
out.println("Process registered: " + process);
System.out.println("Process registered: " + process
+ " blocking");
continue;
}
process.cpudone++;
process.remainFromSlice--;
if (process.ioblocking > 0) {
process.ionext++;
}
comptime++;
}
out.close();
} catch (IOException e) { /* Handle exceptions */
}
result.compuTime = comptime;
return result;
}
private void initQueue(Vector processVector) {
for (int i = 0; i < processVector.size(); ++i) {
sProcess p = (sProcess) processVector.elementAt(i);
p.remainFromSlice = PreemptiveScheduler.sliceSize;
line.add(p);
}
}
private sProcess getNextProcess() {
if (line.isEmpty())
return null;
sProcess first = null;
for (sProcess p = line.remove();; line.add(p), p = line.remove()) {
if (p == first)
return null;
if (p.cpudone < p.cputime)
return p;
if (first == null)
first = p;
}
}
}