-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmfu.java
78 lines (64 loc) · 2.1 KB
/
mfu.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
package PageReplacement;
import java.util.*;
public class mfu {
public static void main(String[] args) {
Scanner myinp = new Scanner(System.in);
int references, frames, fault_count=0, checker=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 pno[] = new int[references];
int frequency[] = 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++) {
pno[i] = -1;
frequency[i] = 0;
}
for (int i = 0; i < reference_string.length; i++) {
checker = capture_page(reference_string[i], frames, pno);
if (checker == -1) {
checker = fframe(frames, pno);
if (checker == -1) {
checker = mframe(frames, frequency);
}
fault_count++;
pno[checker] = reference_string[i];
frequency[checker] = 1;
} else {
frequency[checker]++;
}
}
System.out.print("\n===============================");
System.out.println("\nFault: "+fault_count);
}
static int capture_page(int page, int frames, int pno[]){
for (int i = 0; i < frames; i++) {
if (pno[i] == page) {
return i;
}
}
return -1;
}
static int fframe(int frames, int pno[]){
for (int i = 0; i <= frames; i++) {
if (pno[i] == -1) {
return i;
}
}
return -1;
}
static int mframe(int frames, int frequency[]){
int short1 = 0;
for (int i = 1; i < frames; i++) {
if (frequency[i] > frequency[short1]) {
short1 = i;
}
}
return short1;
}
}