-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPageReqlacement.java
More file actions
132 lines (111 loc) · 2.6 KB
/
Copy pathPageReqlacement.java
File metadata and controls
132 lines (111 loc) · 2.6 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
128
129
130
131
132
package OS;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Random;
public class PageReqlacement {
public static final int MEMORYSIZE = 3;
/**
* random sequence.
*/
private int[] accessSeries = new int[12];
private int lru;
private int fifo;
private int totalInstruction = accessSeries.length;
/**
* disaffectLRU.
*/
private int disaffectLRU;
/**
* disaffectfifo;
*/
private int disaffectfifo;
ArrayList<Frame> frames = new ArrayList<Frame>(MEMORYSIZE);
HashMap<Integer, Frame> framMap = new HashMap<Integer, Frame>();
/**
* initial the random sequence.
*/
public void initSequence() {
Random rd = new Random();
for (int i=0; i<accessSeries.length; i++) {
accessSeries[i] = rd.nextInt(5) + 1;
System.out.print(accessSeries[i]);
}
}
/**
* initial FramMap.
*/
public void initFramMap() {
for (int i=1; i<6; i++) {
framMap.put(i, new Frame(i));
}
System.out.println(framMap.toString());
}
/**
* imitate stack by LRU.
* @param pageNo pageNo.
*/
public void imitateStackByLRU(int pageNo) {
if (!frames.contains(framMap.get(pageNo))) {
if (lru < MEMORYSIZE) {
frames.add(framMap.get(pageNo));
lru ++;
} else {
frames.remove(0);
frames.add(framMap.get(pageNo));
}
disaffectLRU ++;
} else {
if (lru < MEMORYSIZE) {
return;
} else {
Frame frame = frames.get(0);
frames.remove(0);
frames.add(frame);
}
}
}
/**
* imitate stack by FIFO.
* @param pageNO pageNo.
*/
public void imitateStackByFIFO(int pageNo) {
if (!frames.contains(framMap.get(pageNo))) {
if (fifo < MEMORYSIZE) {
frames.add(framMap.get(pageNo));
fifo ++;
} else {
frames.remove(0);
frames.add(framMap.get(pageNo));
}
disaffectfifo ++;
}
}
/**
* page replacement by lru.
* @return
*/
public float lru() {
for (int pageNo : accessSeries) {
imitateStackByLRU(pageNo);
System.out.println(frames);
}
return (float)disaffectLRU / totalInstruction;
}
/**
* page replacement by fifo.
* @return
*/
public float fifo() {
for (int pageNo : accessSeries) {
imitateStackByFIFO(pageNo);
}
return (float)disaffectfifo / totalInstruction;
}
public static void main(String[] args) {
PageReqlacement pageReqlacement = new PageReqlacement();
pageReqlacement.initSequence();
pageReqlacement.initFramMap();
System.out.println(pageReqlacement.lru());
System.out.println(pageReqlacement.fifo());
}
}