-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPageReplacementLRU.java
81 lines (72 loc) · 2.82 KB
/
PageReplacementLRU.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
import java.io.*;
import java.util.*;
public class PageReplacementLRU {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int frames, pointer = 0, hit = 0, fault = 0, ref_len;
Boolean isFull = false;
int buffer[];
ArrayList<Integer> stack = new ArrayList<Integer>();
int reference[];
int mem_layout[][];
System.out.println("Please enter the number of Frames: ");
frames = Integer.parseInt(br.readLine());
System.out.println("Please enter the length of the Reference string: ");
ref_len = Integer.parseInt(br.readLine());
reference = new int[ref_len];
mem_layout = new int[ref_len][frames];
buffer = new int[frames];
for (int j = 0; j < frames; j++)
buffer[j] = -1;
System.out.println("Please enter the reference string: ");
for (int i = 0; i < ref_len; i++) {
reference[i] = Integer.parseInt(br.readLine());
}
System.out.println();
for (int i = 0; i < ref_len; i++) {
if (stack.contains(reference[i])) {
stack.remove(stack.indexOf(reference[i]));
}
stack.add(reference[i]);
int search = -1;
for (int j = 0; j < frames; j++) {
if (buffer[j] == reference[i]) {
search = j;
hit++;
break;
}
}
if (search == -1) {
if (isFull) {
int min_loc = ref_len;
for (int j = 0; j < frames; j++) {
if (stack.contains(buffer[j])) {
int temp = stack.indexOf(buffer[j]);
if (temp < min_loc) {
min_loc = temp;
pointer = j;
}
}
}
}
buffer[pointer] = reference[i];
fault++;
pointer++;
if (pointer == frames) {
pointer = 0;
isFull = true;
}
}
for (int j = 0; j < frames; j++)
mem_layout[i][j] = buffer[j];
}
for (int i = 0; i < frames; i++) {
for (int j = 0; j < ref_len; j++)
System.out.printf("%3d ", mem_layout[j][i]);
System.out.println();
}
System.out.println("The number of Hits: " + hit);
System.out.println("Hit Ratio: " + (float) ((float) hit / ref_len));
System.out.println("The number of Faults: " + fault);
}
}