-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPG2TSB.java
More file actions
106 lines (86 loc) · 2.83 KB
/
PG2TSB.java
File metadata and controls
106 lines (86 loc) · 2.83 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
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
public class PG2TSB {
public static void main(String[] args) throws IOException {
String pageReference = "";
BufferedReader buffer = new BufferedReader(new FileReader("pageRefString.txt"));
String line = "";
while ((line = buffer.readLine()) != null) {
pageReference += line;
System.out.println(line);
}
buffer.close();
String[] pageReferenceArray = pageReference.split(" ");
paging(3, pageReferenceArray);
paging(4, pageReferenceArray);
}
private static void paging(int numberOfFrames, String[] pages) throws IOException{
String dataToDisplay = "";
int numberOfPageFaults = 0;
String[] frames = new String[numberOfFrames];
int[] lastUseOfPage = new int[numberOfFrames];
for(int i = 0; i < lastUseOfPage.length; i++){
lastUseOfPage[i] = 0;
}
for(int i = 0; i < frames.length; i++){
frames[i] = "-";
}
System.out.println("\n\nRunning LRU simulation with " + numberOfFrames + " Frames. \n");
boolean pageExist;
for(int i = 0; i < pages.length; i++){
pageExist = false;
for(int j = 0; j < frames.length; ++j){;
if(frames[j].equals(pages[i])){
pageExist = true;
}
}
if(pageExist != true){
int pageToReplace = getLongestTimeSinceUse(lastUseOfPage);
frames[pageToReplace] = pages[i];
lastUseOfPage[pageToReplace] = -1;
numberOfPageFaults += 1;
}
dataToDisplay += framesToString(frames) + "\n";
lastUseOfPage = incLastUseOfPage(lastUseOfPage);
}
dataToDisplay += "\nTotal Number Of Page Faults: " + numberOfPageFaults;
System.out.println(dataToDisplay);
writeDataToOutput(dataToDisplay, numberOfFrames);
}
private static void writeDataToOutput(String dataToDisplay, int numberOfFrames) throws IOException{
String[] dataPieces = dataToDisplay.split("\n");
String file = "LRU output With " + numberOfFrames + " Frame.txt";
PrintWriter writer = new PrintWriter(file, "UTF-8");
for(int i = 0; i < dataPieces.length; i++){
writer.println(dataPieces[i]);
}
writer.close();
}
private static String framesToString(String[] frames) {
String text = "";
for(int i = 0; i < frames.length; i++){
text += frames[i] + " ";
}
text += "\n";
return text;
}
private static int[] incLastUseOfPage(int[] lastUseOfPage){
for(int i = 0; i < lastUseOfPage.length; i++){
lastUseOfPage[i] = lastUseOfPage[i] + 1;
}
return lastUseOfPage;
}
private static int getLongestTimeSinceUse(int[] lastUseOfPage){
int currentPage = lastUseOfPage[0];
int position = 0;
for(int i = 1; i < lastUseOfPage.length; i++) {
if(currentPage < lastUseOfPage[i]){
position = i;
currentPage = lastUseOfPage[i];
}
}
return position;
}
}