-
Notifications
You must be signed in to change notification settings - Fork 0
/
lru.java
102 lines (93 loc) · 2.86 KB
/
lru.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
98
99
100
101
102
package SPOS;
import java.util.*;
public class lru {
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int nframe,pointer = 0, h = 0, f = 0,str_len;
Boolean isFull = false;
int buffer[];
ArrayList<Integer> stack = new ArrayList<Integer>();
int str[];
int mem_layout[][];
System.out.println("Please enter the number of Frames: ");
nframe = sc.nextInt();
System.out.println("Please enter the length of the Reference string: ");
str_len = sc.nextInt();
str = new int[str_len];
mem_layout = new int[str_len][nframe];
buffer = new int[nframe];
for(int j = 0; j < nframe; j++)
{
buffer[j] = -1;
}
System.out.println("Please enter the reference string: ");
for(int i = 0; i < str_len; i++)
{
str[i] = sc.nextInt();
}
System.out.println();
for(int i = 0; i < str_len; i++)
{
if(stack.contains(str[i]))
{
stack.remove(stack.indexOf(str[i]));
}
stack.add(str[i]);
int search = -1;
for(int j = 0; j < nframe; j++)
{
if(buffer[j] == str[i])
{
search = j;
h++;
break;
}
}
if(search == -1)
{
if(isFull)
{
int min_loc = str_len;
for(int j = 0; j < nframe; j++)
{
if(stack.contains(buffer[j]))
{
int temp = stack.indexOf(buffer[j]);
if(temp < min_loc)
{
min_loc = temp;
pointer = j;
}
}
}
}
buffer[pointer] = str[i];
f++;
pointer++;
if(pointer == nframe)
{
pointer = 0;
isFull = true;
}
}
for(int j = 0; j < nframe; j++)
{
mem_layout[i][j] = buffer[j];
}
}
for(int i = 0; i < nframe; i++)
{
System.out.print("Frame "+(i+1)+":\t\t");
for(int j = 0; j < str_len; j++)
{
System.out.print(mem_layout[j][i]+"\t\t");
}
System.out.println();
}
System.out.println("The number of Hits: " + h);
System.out.println("The number of Faults: " + f);
System.out.println("Hit Ratio: " + (float)((float)h/str_len));
sc.close();
}
}