-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.java
253 lines (218 loc) · 11.5 KB
/
Main.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
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
import java.util.*;
import java.io.*;
public class Main {
public static ArrayList<Integer> randomNumList = new ArrayList<Integer>();
public static void main (String args[]) throws IOException {
int machineSize = 0;
int pageSize = 0;
int processSize = 0;
int jobMix = 0;
int numOfReferences = 0;
String replacementAlg = null;
try {
machineSize = Integer.parseInt(args[0]);
pageSize = Integer.parseInt(args[1]);
processSize = Integer.parseInt(args[2]);
jobMix = Integer.parseInt(args[3]);
numOfReferences = Integer.parseInt(args[4]);
replacementAlg = args[5];
}
catch (Exception ex) {
System.out.println(ex);
System.exit(0);
}
randomNumbersReader();
System.out.println("The machine size is " + machineSize + ".");
System.out.println("The page size is " + pageSize + ".");
System.out.println("The process size is " + processSize + ".");
System.out.println("The job mix number is " + jobMix + ".");
System.out.println("The number of references per process is " + numOfReferences + ".");
System.out.println("The replacement algorithm is " + replacementAlg + ".");
int numOfPages = machineSize / pageSize;
FrameTableEntry[] frameTable = new FrameTableEntry[numOfPages];
List<Process> processList = new ArrayList<Process>();
switch (jobMix) {
case 1: {
processList.add(new Process(numOfReferences, processSize, pageSize, 1, 1, 0, 0));
break;
}
case 2: {
processList.add(new Process(numOfReferences, processSize, pageSize, 1, 1, 0, 0));
processList.add(new Process(numOfReferences, processSize, pageSize, 2, 1, 0, 0));
processList.add(new Process(numOfReferences, processSize, pageSize, 3, 1, 0, 0));
processList.add(new Process(numOfReferences, processSize, pageSize, 4, 1, 0, 0));
break;
}
case 3: {
processList.add(new Process(numOfReferences, processSize, pageSize, 1, 0, 0, 0));
processList.add(new Process(numOfReferences, processSize, pageSize, 2, 0, 0, 0));
processList.add(new Process(numOfReferences, processSize, pageSize, 3, 0, 0, 0));
processList.add(new Process(numOfReferences, processSize, pageSize, 4, 0, 0, 0));
break;
}
case 4: {
processList.add(new Process(numOfReferences, processSize, pageSize, 1, 0.75, 0.25, 0));
processList.add(new Process(numOfReferences, processSize, pageSize, 2, 0.75, 0, 0.25));
processList.add(new Process(numOfReferences, processSize, pageSize, 3, 0.75, 0.125, 0.125));
processList.add(new Process(numOfReferences, processSize, pageSize, 4, 0.5, 0.125, 0.125));
}
default: ;
}
simulate(frameTable, processList, replacementAlg);
}
public static void simulate(FrameTableEntry[] frameTable, List<Process> processList, String replacementAlg) {
int quantumCounter = 0;
List<Process> terminatedList = new ArrayList<Process>();
Iterator<Process> it = processList.iterator();
Queue<FrameTableEntry> LRU = new LinkedList<FrameTableEntry>();
Stack<FrameTableEntry> LIFO = new Stack<FrameTableEntry>();
int cycle = 1;
while (processList.size()!=terminatedList.size()) { // Do until processes terminate
Process p;
if (!it.hasNext()) { // Iterate through processes. If there is no next process, go back to beginning.
it = processList.iterator();
p = it.next();
}
else {
p = it.next();
}
while (quantumCounter != 3) { // Round robin with quantum = 3
if (p.isFirstTranslation()) {
// System.out.println("Random Number Used " + randomNumList.get(randomNumberCounter));
p.setFirstTranslation();
}
else {
p.setCurrentReference();
}
// System.out.print("Process " + p.getProcessNumber() + " references address " + p.getCurrentReferenceAddress() + " (Page " + p.getCurrentPageNumber() +
// ") at time " + cycle + ": ");
boolean hit = resolveAddress(frameTable, p, LRU); // Resolve Address and return true if there is a hit
if (!hit) { // If there was not a hit, aka if there was a page fault, evict
evictAndReplace(frameTable, p, cycle, replacementAlg, LRU, randomNumList, LIFO);
}
p.setNextTranslation(randomNumList); // add to random number counter
cycle++;
quantumCounter++;
if (p.incrementReferences()) { // Will return true if number of references is reached
terminatedList.add(p);
quantumCounter = 3;
}
}
quantumCounter = 0;
}
printOutput(terminatedList);
}
public static void randomNumbersReader() {
try {
Scanner randomNumReader = new Scanner(new FileInputStream("random-numbers"));
while (randomNumReader.hasNextInt()) {
randomNumList.add(randomNumReader.nextInt());
}
}
catch (Exception ex) {
System.out.println(ex);
}
}
public static boolean resolveAddress(FrameTableEntry[] frameTable, Process p, Queue<FrameTableEntry> LRU) {
// Convert referenceAddress to a page number
int processNumber = p.getProcessNumber();
int pageNumber = p.getCurrentPageNumber();
for (int i = 0; i < frameTable.length; i++) {
if (frameTable[i] != null && frameTable[i].equals(processNumber, pageNumber)) {
// System.out.println("Hit at frame " + i);
LRU.remove(frameTable[i]);
LRU.add(frameTable[i]);
return true;
}
}
return false;
}
public static void evictAndReplace(FrameTableEntry[] frameTable, Process p, int cycle, String replacementAlg, Queue<FrameTableEntry> LRU, ArrayList<Integer> randomNumList, Stack<FrameTableEntry> LIFO) {
p.incrementPageFaultCounter(); // Increment page fault counter
boolean processed = false;
for (int i = frameTable.length-1; i >= 0; i--) {
if (frameTable[i] == null) { // If frames are free
// System.out.println("Page Fault. Using free frame " + i + " for initial loading");
FrameTableEntry temp = new FrameTableEntry(p.getPageSize(), i);
temp.setEntry(p.getProcessNumber(), p.getCurrentPageNumber(), p);
temp.setCycleLoaded(cycle);
frameTable[i] = temp;
LRU.add(temp);
LIFO.add(temp);
processed = true;
break;
}
}
if (!processed) {
// Evict a frame depending on algorithm
if (replacementAlg.equalsIgnoreCase("lru")) { // Determine which frame is the least recently used
FrameTableEntry temp = LRU.poll();
// System.out.println("Page Fault. Evicting page " + temp.getPageNumber() + " of process " + temp.getProcessNumber() + " from frame " + temp.getFrameNumber());
int residency = cycle - temp.getCycleLoaded();
// Add residency to process
temp.getProcess().incrementEvictions(); // Increment evictions to calculate average residency
temp.getProcess().addResidencyTime(residency);
temp.setEntry(p.getProcessNumber(), p.getCurrentPageNumber(), p);
temp.setCycleLoaded(cycle);
LRU.add(temp);
}
else if (replacementAlg.equalsIgnoreCase("random")) {
int r = randomNumList.get(0);
randomNumList.remove(0);
int i = r % frameTable.length;
// double y = r / (Integer.MAX_VALUE + 1d);
// System.out.println(r + "ratio:" + y);
// int i = (int)(y * frameTable.length - 1);
FrameTableEntry temp = frameTable[i];
// System.out.println("Page Fault. Evicting page " + temp.getPageNumber() + " of process " + temp.getProcessNumber() + " from frame " + temp.getFrameNumber());
int residency = cycle - temp.getCycleLoaded();
// Add residency to process
temp.getProcess().incrementEvictions(); // Increment evictions to calculate average residency
temp.getProcess().addResidencyTime(residency);
temp.setEntry(p.getProcessNumber(), p.getCurrentPageNumber(), p);
temp.setCycleLoaded(cycle);
}
else if (replacementAlg.equalsIgnoreCase("lifo")) {
FrameTableEntry temp = LIFO.peek();
// System.out.println("Page Fault. Evicting page " + temp.getPageNumber() + " of process " + temp.getProcessNumber() + " from frame " + temp.getFrameNumber());
int residency = cycle - temp.getCycleLoaded();
// Add residency to process
temp.getProcess().incrementEvictions(); // Increment evictions to calculate average residency
temp.getProcess().addResidencyTime(residency);
temp.setEntry(p.getProcessNumber(), p.getCurrentPageNumber(), p);
temp.setCycleLoaded(cycle);
LIFO.add(temp);
}
}
}
public static void printOutput(List<Process> terminatedList) {
double overallAverageResidency = 0;
double totalEvictions = 0;
double totalResidency = 0;
int totalFaults = 0;
for (Process p : terminatedList) {
if (p.getEvictions() == 0) { // If evictions is 0, there is no residency.
System.out.println("Process " + p.getProcessNumber() + " had " + p.getPageFaultCounter() + " faults.");
System.out.println("\tWith no evictions, the average residency is undefined.");
totalResidency += p.getResidency();
}
else {
double averageResidency = p.getResidency() / (double)p.getEvictions();
totalEvictions += p.getEvictions();
totalResidency += p.getResidency();
System.out.println("Process " + p.getProcessNumber() + " had " + p.getPageFaultCounter() + " faults and "
+ averageResidency + " average residency.");
overallAverageResidency += averageResidency;
}
totalFaults += p.getPageFaultCounter();
}
if (totalEvictions == 0) {
System.out.println("\nThe total number of faults is " + totalFaults + ".");
System.out.println("\tWith no evictions, the overall average residency is undefined.");
}
else {
overallAverageResidency = totalResidency / (double)totalEvictions;
System.out.println("\nThe total number of faults is " + totalFaults + " and the overall average residency is " + overallAverageResidency + ".");
}
}
}