-
Notifications
You must be signed in to change notification settings - Fork 0
/
FIFO.java
142 lines (119 loc) · 3.2 KB
/
FIFO.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
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
133
134
135
136
137
138
139
140
141
142
/* File FIFO.java */
package bufmgr;
import java.util.ArrayList;
import java.util.Collections;
/**
* class FIFO is a subclass of class Replacer using LRU
* algorithm for page replacement
*/
class FIFO extends Replacer {
/**
* private field
* An array to hold number of frames in the buffer pool. First indexes are the first frames which came.
*/
private ArrayList<Integer> frames;
/**
* private field
* number of frames used
*/
private int nframes;
/**
* This put the given frame to the end of the list.
* @param frameNo the frame number
*/
private void update(int frameNo)
{
frames.add((Integer) frameNo);
}
/**
* Calling super class the same method
* Initializing frames with number of buffer allocated
* by buffer manager
* set number of frame used to zero
*
* @param mgr a BufMgr object
* @see BufMgr
* @see Replacer
*/
public void setBufferManager( BufMgr mgr )
{
super.setBufferManager(mgr);
int numBuffers = mgr.getNumBuffers();
frames = new ArrayList<Integer>(Collections.nCopies(numBuffers, 0));
nframes = 0;
}
/* public methods */
/**
* Class constructor
* Initializing frames pinter = null.
*/
public FIFO(BufMgr mgrArg)
{
super(mgrArg);
frames = null;
}
/**
* call super class the same method
* pin the page in the given frame number
*
* @param frameNo the frame number to pin
* @exception InvalidFrameNumberException
*/
public void pin(int frameNo) throws InvalidFrameNumberException
{
super.pin(frameNo);
}
/**
* Finding a free frame in the buffer pool
* or choosing a page to replace using FIFO policy
*
* @return return the frame number
* @exception BufferPoolExceededException if the buffer has no more room
*/
public int pick_victim()
throws BufferPoolExceededException
{
int numBuffers = mgr.getNumBuffers();
int frame;
// Finding a free frame
if ( nframes < numBuffers ) {
frame = nframes++;
frames.set(frame,frame);
state_bit[frame].state = Pinned;
(mgr.frameTable())[frame].pin();
return frame;
}
// Choosing a page to replace using FIFO policy. First In First Out. The first in is frames[0].
for ( int i = 0; i < numBuffers; ++i ) {
frame = frames.get(i);
if ( state_bit[frame].state != Pinned ) {
state_bit[frame].state = Pinned;
(mgr.frameTable())[frame].pin();
update(frame);
return frame;
}
}
// If no page available (every pages are pinned). Return an error BufferPoolExceededException
throw new BufferPoolExceededException(null, "BUFMGR: BUFFER_EXCEEDED.");
}
/**
* get the page replacement policy name
*
* @return return the name of replacement policy used
*/
public String name() { return "FIFO"; }
/**
* print out the information of frame usage
*/
public void info()
{
super.info();
System.out.print( "FIFO REPLACEMENT");
for (int i = 0; i < nframes; i++) {
if (i % 5 == 0)
System.out.println( );
System.out.print( "\t" + frames.get(i));
}
System.out.println();
}
}