-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMostUsed.java
More file actions
57 lines (46 loc) · 1.19 KB
/
Copy pathMostUsed.java
File metadata and controls
57 lines (46 loc) · 1.19 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
import java.util.Arrays;
import java.util.Stack;
public class MostUsed {
Pages pages;
ReferenceString str;
int count[];
public MostUsed(Pages pages) {
this.pages = pages;
}
public int simulate(ReferenceString str) {
this.str = str;
for (int i = 0; i < str.size(); i++)
if (!pages.hasSpace() && !pages.contains(str.get(i))) {
pages.remove(count(str, i));
pages.add(str.get(i));
}
else if (pages.hasSpace() && !pages.contains(str.get(i))) {
pages.add(str.get(i));
}
return pages.pageFaults;
}
public int count(ReferenceString str, int start) {
count = new int[pages.getRSS()];
int lru = -1;
for (int page = 0; page < count.length; page++)
for (int pos = start; pos > -1; pos--)
if (str.get(pos) == pages.get(page)) {
count[page]++;
}
for (int i = 0, min = Integer.MAX_VALUE; i < count.length; i++)
if (min > count[i]) {
min = count[i];
lru = i;
}
return pages.get(lru);
}
public void print() {
pages.print();
}
public static void main(String[]args) {
MostUsed cbe = new MostUsed(new Pages(3));
ReferenceString str = new ReferenceString(.9, 10000);
str.fillRandomly();
System.out.println(cbe.simulate(str));
}
}