-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFrameTable.java
More file actions
206 lines (174 loc) · 5.14 KB
/
FrameTable.java
File metadata and controls
206 lines (174 loc) · 5.14 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
import java.util.ArrayList;
public class FrameTable{
private int bufferSpace = 512;
private int numFrames;
private Frame[] frames;
private String algorithm, paging;
private ArrayList<Process> programs;
private int swapCount = 0;
//Age is used for FIFO.
private long maxAge = 0;
//Access time is used for LRU
private long maxAccessTime = 0;
//Set current index for SCLRU
private int currIdx = 0;
FrameTable(int bufferSpace, int pageSize, String algorithm,
String paging, ArrayList<Process> programs){
this.bufferSpace = bufferSpace;
numFrames = bufferSpace / pageSize;
frames = new Frame[numFrames];
this.algorithm = algorithm;
this.paging = paging;
this.programs = programs;
}
public void loadMem(int program, int relMemAddress){
boolean found = false;
//Try to directly load page
Process process = programs.get(program);
if(process.get_pID() != program){
for(Process proc : programs){
if(proc.get_pID() == program){
process = proc;
break;
}
}
}
if(process.get_pID() != program){
System.out.println("----CRITICAL ERROR: CANNOT FIND PROGRAM IN loadMem----");
return;
}
int pageNum = process.getPageNumber(relMemAddress);
//Check if memory is already loaded
for(Frame frame: frames){
if(frame != null){
if(frame.getPage() == pageNum){
found = true;
//Track access for lru
frame.setLastAccess(maxAccessTime);
maxAccessTime++;
//Track access for sclru
frame.setRefBit(true);
return;
}
}
}
//If not, then load it.
if(!found){
if(paging.equalsIgnoreCase("d")){
switch (algorithm){
case "lru":
lru(process, relMemAddress);
break;
case "sclru":
sclru(process, relMemAddress);
break;
case "fifo":
fifo(process, relMemAddress);
break;
default:
System.out.println("Broken algorithm call.");
break;
}
}else{
switch (algorithm){
case "lru":
lruP(process, relMemAddress);
break;
case "sclru":
sclruP(process, relMemAddress);
break;
case "fifo":
fifoP(process, relMemAddress);
break;
default:
System.out.println("Broken algorithm call.");
break;
}
}
swapCount++;
}
}
private void fifo(Process process, int relMemAddress){
long frameAge = Long.MAX_VALUE;
int frameIndex = -1;
for(int i = 0; i < numFrames; i++){
if(frames[i] == null){
frameIndex = i;
break;
}else if(frames[i].getAge() < frameAge){
frameAge = frames[i].getAge();
frameIndex = i;
}
}
swap(frameIndex, process, relMemAddress);
}
private void lru(Process process, int relMemAddress){
long frameAccess = Long.MAX_VALUE;
int frameIndex = -1;
for(int i = 0; i < numFrames; i++){
if(frames[i] == null){
frameIndex = i;
break;
}else if(frames[i].getLastAccess() < frameAccess){
frameAccess = frames[i].getLastAccess();
frameIndex = i;
}
}
swap(frameIndex, process, relMemAddress);
}
private void sclru(Process process, int relMemAddress){
boolean found = false;
int replIdx = -1;
while(!found){
if(frames[currIdx] == null){
replIdx = currIdx;
found = true;
}else if(!frames[currIdx].getRefBit()){
replIdx = currIdx;
found = true;
}else if(frames[currIdx].getRefBit()){
frames[currIdx].setRefBit(false);
}
currIdx++;
if(currIdx >= numFrames){
currIdx = 0;
}
}
swap(replIdx, process, relMemAddress);
}
private void fifoP(Process process, int relMemAddress){
fifo(process, relMemAddress);
fifo(process, process.getNextPageAddress(relMemAddress));
}
private void lruP(Process process, int relMemAddress){
lru(process, relMemAddress);
lru(process, process.getNextPageAddress(relMemAddress));
}
private void sclruP(Process process, int relMemAddress){
sclru(process, relMemAddress);
sclru(process, process.getNextPageAddress(relMemAddress));
}
//Performs a swap given a frame index and a page number.
//Allows us to not rewrite swap for each algorithm.
//Also will handle prepaging vs demand paging.
private void swap(int frameIndex, Process process, int relMemAddress){
if(process != null && relMemAddress !=-1){
int pageNum = process.getPageNumber(relMemAddress);
frames[frameIndex] = new Frame(pageNum);
//Set age for fifo
frames[frameIndex].setAge(maxAge);
maxAge++;
//Set access time for LRU
frames[frameIndex].setLastAccess(maxAccessTime);
maxAccessTime++;
//Set reference bit for SCLRU
frames[frameIndex].setRefBit(false);
}else{
frames[frameIndex] = null;
currIdx = frameIndex;
}
}
public int getSwapCount(){
return swapCount;
}
}