-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlfu.java
97 lines (79 loc) · 2.77 KB
/
lfu.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
package PageReplacement;
import java.util.*;
public class lfu {
public static void main(String[] args) {
Scanner myinp = new Scanner(System.in);
int references, frames, hit=0, fault_count=0, least, rp=0, cnt2=0, bn=0;
System.out.print("Number of Frames: ");
frames = myinp.nextInt();
System.out.print("Number of Reference: ");
references = myinp.nextInt();
int reference_string[] = new int [references];
int page[] = new int[references];
int cnt[] = new int[references];
for (int i = 0; i < references; i++) {
System.out.print("Reference String "+i+": ");
reference_string[i] = myinp.nextInt();
}
for (int i = 0; i < frames; i++) {
page[i] = 9999;
}
for (int i = 0; i < frames; i++) {
cnt[i] = 0;
}
for (int i = 0; i < references; i++) {
System.out.println();
hit = 0;
for (int j = 0; j < frames; j++) {
if (page[j] == reference_string[i]) {
hit = 1;
break;
}
}
if (hit != 0) {
int temp=0, index_hit;
for (int j = 0; j < frames; j++) {
if (page[j] == reference_string[i]) {
temp = j;
break;
}
}
index_hit = temp;
cnt[index_hit]++;
for (int j = 0; j < frames; j++) {
System.out.print("-\t");
}
} else {
fault_count++;
if (bn < frames) {
page[bn] = reference_string[i];
cnt[bn] = cnt[bn]++;
bn++;
} else {
least = 9999;
for (int j = 0; j < frames; j++) {
if (cnt[j] < least) {
least = cnt[j];
rp = j;
}
}
page[rp] = reference_string[i];
cnt2 = 0;
for (int j = 0; j <= i; j++) {
if (reference_string[i] == reference_string[j]) {
cnt2 = cnt2++;
}
}
cnt[rp] = cnt2;
}
for (int j = 0; j < frames; j++) {
if (page[j] != 9999) {
System.out.print(page[j]+"\t");
}
}
}
}
System.out.print("\n===============================");
System.out.println("\nFault: "+fault_count);
}
}