-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmru.java
69 lines (59 loc) · 2.1 KB
/
mru.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
package PageReplacement;
import java.util.*;
public class mru {
public static void main(String[] args) {
Scanner myinp = new Scanner(System.in);
int references, frames, hit=0, fault_count=0, page_get, cap=0, repeat=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 mru[] = 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 < references; i++) {
hit = 0;
for (int j = 0; j < frames; j++) {
if (page[j] == reference_string[i]) {
hit = 1;
fault_count++;
break;
}
}
if (hit == 0) {
for (int j = 0; j < frames; j++) {
page_get = page[j];
for (int j2 = i; j2 < references; j2++) {
if (page_get == reference_string[j2]) {
mru[j] = j2;
cap = 1;
break;
} else {
cap = 0;
}
}
if (cap == 0) {
mru[j] = 9999;
}
}
int maximum = -9999;
for (int j = 0; j < frames; j++) {
if (mru[j] > maximum) {
maximum = mru[j];
repeat = j;
}
}
page[repeat] = reference_string[i];
}
}
System.out.print("\n===============================");
System.out.println("\nFault: "+fault_count);
}
}