-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path09.01.LRU_Cache.java
More file actions
127 lines (106 loc) · 3.08 KB
/
09.01.LRU_Cache.java
File metadata and controls
127 lines (106 loc) · 3.08 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
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
9. Design, develop and implement a C/C++/Java program to implement page
replacement algorithms LRU and FIFO. Assume suitable input required to
demonstrate the results.
LRU Part
*/
import java.util.Scanner;
class Frame {
int pageNumber = -1;
int lastAccessTime = -1;
void replaceFrame(int pageNumber, int lastAccessTime) {
this.pageNumber = pageNumber;
this.lastAccessTime = lastAccessTime;
}
void refreshFrame(int lastAccessTime) {
this.lastAccessTime = lastAccessTime;
}
}
class CacheLRU {
private static Frame[] cache;
private static int nF;
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter number of page requests");
int nR = scanner.nextInt();
int[] pageNumbers = new int[nR];
System.out.println("Enter page requests");
for (int i = 0; i < nR; ++i) {
pageNumbers[i] = scanner.nextInt();
}
System.out.println("Enter number of frames");
nF = scanner.nextInt();
cache = new Frame[nF];
for (int i = 0; i < nF; ++i) {
cache[i] = new Frame();
}
int totalHits = 0, totalMisses = 0;
for (int i = 0; i < nR; ++i) {
int index = findPageNumber(pageNumbers[i]);
if (index != -1) {
totalHits += 1;
cache[index].refreshFrame(i);
} else {
totalMisses += 1;
int temp = getLruFrameIndex();
cache[temp].replaceFrame(pageNumbers[i], i);
}
printCache();
}
System.out.println("Total Hits " + totalHits);
System.out.println("Total Misses " + totalMisses);
float hitRatio = ((float) totalHits) / (totalHits + totalMisses);
System.out.println("Hit Ratio " + hitRatio);
}
public static int findPageNumber(int pageNumber) {
for (int i = 0; i < nF; ++i) {
if (cache[i].pageNumber == pageNumber) {
return i;
}
}
return -1;
}
public static int getLruFrameIndex() {
int min = cache[0].lastAccessTime;
int index = 0;
for (int i = 1; i < nF; ++i) {
if (cache[i].lastAccessTime < min) {
min = cache[i].lastAccessTime;
index = i;
}
}
return index;
}
public static void printCache() {
System.out.print("Cache Content: ");
for (int i = 0; i < nF; ++i) {
System.out.print(cache[i].pageNumber + " ");
}
System.out.println();
}
}
/*
Output:
Enter number of page requests
13
Enter page requests
7 0 1 2 0 3 0 4 2 3 0 3 2
Enter number of frames
4
Cache Content: 7 -1 -1 -1
Cache Content: 7 0 -1 -1
Cache Content: 7 0 1 -1
Cache Content: 7 0 1 2
Cache Content: 7 0 1 2
Cache Content: 3 0 1 2
Cache Content: 3 0 1 2
Cache Content: 3 0 4 2
Cache Content: 3 0 4 2
Cache Content: 3 0 4 2
Cache Content: 3 0 4 2
Cache Content: 3 0 4 2
Cache Content: 3 0 4 2
Total Hits 7
Total Misses 6
Hit Ratio 0.53846157
*/