-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFrameTable.java
More file actions
209 lines (190 loc) · 6.48 KB
/
FrameTable.java
File metadata and controls
209 lines (190 loc) · 6.48 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import java.util.Queue;
import java.util.LinkedList;
import java.util.Random;
/************************************************************
* A frame table data structure
* Keeps track of process and pages associated with frames.
* Physical memory is specified to be 16 KB with 1KB frames.
* There are 16 total frames in physical memory.
* The frame table also keeps track of its free frames in a
* frame.
* @author Gloire Rubambiza
* @since 11/12/2017
************************************************************/
public class FrameTable {
/** The array of frames in physical memory. */
private Frame [] frameTable;
/** The max amount of frames to be created. */
private int max = 16;
/** The list of free frames in the system. */
private boolean [] freeFrameList;
/** The Queue for deciding which frame to vacate for a new page reference. */
private Queue<Integer> kicker;
/**********************************************************
* Instantiates an array of 16 frames and a free frame list.
***********************************************************/
public FrameTable () {
this.frameTable = new Frame [max];
this.freeFrameList = new boolean [max];
kicker = new LinkedList<Integer>();
int i;
// Creates each new frame
for ( i = 0; i < max; i++ ) {
this.frameTable[i] = new Frame ();
this.freeFrameList[i] = true;
}
}
/******************************************************
* Checks if the page is already in the table.
* @param pPid is the process that owns the page.
* @param pPage is the page that was referenced.
* @return the presence of the page in the frame table.
******************************************************/
public boolean checkPage(int pPid, int pPage){
int i, pid, page;
for ( i = 0; i < max; i++ ) {
pid = this.frameTable[i].getPID();
page = this.frameTable[i].getPage();
if ( (pid == pPid) && (page == pPage) ) {
return true;
}
}
return false;
}
/**************************************************
* Checks for empty spots in the frame table.
* @return a free frame number if one is available,
* -1 if the frame table is empty.
*************************************************/
public int getFreeFrame() {
int i, j;
// Simulate pseudorandomness
Random rnd = new Random();
i = 0;
while ( i < max ) {
int freeFrame = rnd.nextInt(max);
if ( isFree(freeFrame) ) {
return freeFrame;
}
i++;
}
// Try sequentially after 16 unsuccessful "random" picks.
for ( j = 0; j < max; j++) {
if ( isFree(j) ) {
System.out.println("Free frame returned is " + j);
return j;
}
}
return -1;
}
/*****************************************
* Checks if a given frame is free.
* Sets the frame to false if it's free.
* @param frame is the frame to be checked.
* @return true if it is, false otherwise.
******************************************/
private boolean isFree( int frame) {
if( this.freeFrameList[frame] ) {
this.freeFrameList[frame] = false;
return true;
}
return false;
}
/***************************************************
* Inserts a new frame table entry.
* @param frame is the frame that will be associated
* with the process/page pair
* @param process is the process that owns the page.
* @param page is the page to be inserted.
**************************************************/
public void insertFrameEntry(int frame, int process, int page) {
int i;
for ( i = 0; i < max; i++ ) {
if ( i == frame ) {
this.frameTable[i].updateProcess(process);
this.frameTable[i].updatePage(page);
}
}
}
/**********************************
* Prints the frame table to screen.
***********************************/
public void printCurrentState() {
int i;
for ( i = 0; i < max; i++ ) {
int pid = this.frameTable[i].getPID();
int page = this.frameTable[i].getPage();
System.out.println(i + "\t" + pid + "\t" + page + "\n");
}
}
/***************************************
* Adds a frame to the replacement queue.
* @param frame is the frame to be added.
****************************************/
public void addCandidate(int frame) {
this.kicker.remove(frame);
this.kicker.add(frame);
}
/*****************************************************************
* Searches for which frame is associated with a PID/page pair.
* @param pid is the PID of the process owning the frame.
* @param page is the page located at the frame.
* @return the frame number associated with the PID/page pair.
/****************************************************************/
public int [] searchPotentialReplacement(int pPid, int pPage) {
int i;
int [] interestArray = new int [3];
for ( i = 0; i < max; i++ ) {
int pid = this.frameTable[i].getPID();
int page = this.frameTable[i].getPage();
if ( (pid == pPid) && (page == pPage) ) {
interestArray[0] = i;
interestArray[1] = pid;
interestArray[2] = page;
return interestArray;
}
}
return interestArray;
}
/*****************************************************
* Reports the PID/page pair associated with
* a victim frame.
* @param frame is the frame associated with the pair.
* @return the PID and page of the victims.
*****************************************************/
public int [] searchVictim (int frame ) {
int i;
int [] pair = new int [2];
for ( i = 0; i < max ; i++ ) {
if ( i == frame ) {
int pid = this.frameTable[i].getPID();
int page = this.frameTable[i].getPage();
pair[0] = pid;
pair[1] = page;
return pair;
}
}
return pair;
}
/********************************************
* Picks a candidate for page replacement.
* Removes it from the head of the queue.
* @return the frame that needs to be updated.
*********************************************/
public int pickLRUCandidate () {
int vic = this.kicker.remove();
return vic;
}
/*********************************************
* Returns a reference to the frame table for
* the GUI to display.
* The reference is passed to the Tables class
* which sends it to the controller, which in
* turn passes it to the GUI.
* This ensures the view never calls the model.
* @return an array of frames.
**********************************************/
public Frame [] getFrameTable () {
return this.frameTable;
}
}