-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTables.java
More file actions
192 lines (170 loc) · 7.19 KB
/
Tables.java
File metadata and controls
192 lines (170 loc) · 7.19 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
import java.util.Hashtable;
/*********************************************************
* The Table Manager.
* Maintains one process/frame table to handle page faults.
* @author Gloire Rubambiza
* @since 11/14/2017
**********************************************************/
public class Tables {
/* The process table. */
private ProcessTable processTable;
/* The frame table. */
private FrameTable frameTable;
/* The maximum number of frames. */
private final int max = 16;
/*******************************************************
* Instantiates a unified table for frame/process tables.
* To be used as a unified data structure that initiates
* all the actions based on input passed from main.
********************************************************/
public Tables () {
this.processTable = new ProcessTable();
this.frameTable = new FrameTable();
}
/***********************************************************
* Passes a reference to the page table to the controller.
* To be passed to the GUI for display a process' page table.
* @param pPid is the PID of the process.
************************************************************/
public Hashtable<Integer, Integer> passProcessTable (int pPid) {
int i, pid;
for ( i = 0; i < 10; i++ ) {
pid = this.processTable.getPCBArray()[i].getPID();
if ( pid == pPid ) {
return this.processTable.getPCBArray()[i].getTable();
}
}
System.out.println("The input might have more than 10 processes!\n");
return null;
}
/**************************************************
* Gets a reference to the frame table i.e. current
* physical state of memory.
* Sends the table to the controller, which in
* turn passes it to the GUI.
* @return an array of frames.
*************************************************/
public Frame [] passFrameTable () {
return this.frameTable.getFrameTable();
}
/***********************************************
* Updates the references count for the process.
* @param pid is the PID of the process.
***********************************************/
public void updateProcessRefCount ( int pid ) {
this.processTable.updatePCBRefCount(pid);
}
/***********************************************
* Updates the page fault count for a process.
* @param pid is the PID of the process.
***********************************************/
public void updateProcessFaultCount ( int pid ) {
this.processTable.updatePCBFaultCount(pid);
}
/******************************************************
* Checks if the page is already in the table.
* @param pid is the process that owns the page.
* @param page the page that was referenced.
* @return the presence of the page in the frame table.
******************************************************/
public boolean checkPageInTable(int pid, int page) {
return this.frameTable.checkPage(pid, page);
}
/**********************************************************
* Updates page table of the given process with new entries.
* If the page exists, the function is used to remove the
* entry for a process whose frame has been chosen as a
* victim. Otherwise, we're inserting the frame/page pair.
* @param exists tells where the page is already in the table.
* @param pid is the PID of the process.
* @param page is the page to be added/replaced in the table.
************************************************************/
public void updatePageTable(boolean exists, int pid, int page, int frame) {
this.processTable.updatePCB(exists, pid, page, frame);
}
/****************************************************
* Checks if there are any empty frames in the table.
* @return the number of the free frame.
****************************************************/
public int checkFreeFrame() {
return this.frameTable.getFreeFrame();
}
/************************************************************
* Searches for which frame is associated with a PID/page pair.
* The function is used when the page is already in memory or
* a potential replacement.
* @return the triple of PID,page, and frame number;
/************************************************************/
public int [] searchAssociatedFrame(int pid, int page) {
return this.frameTable.searchPotentialReplacement(pid, page);
}
/***************************************
* Adds a frame to the replacement queue.
* @param frame is the frame to be added.
****************************************/
public void addCandidateFrame(int frame) {
this.frameTable.addCandidate(frame);
}
/********************************************
* Picks a victim for LRU page replacement.
* @return the frame that needs to be updated.
*********************************************/
public int [] pickVictim () {
int [] victimInfo = new int[3];
victimInfo[0] = this.frameTable.pickLRUCandidate();
// Search for the victim's pid and pair
int i;
for ( i = 0; i < max; i++ ) {
if ( i == victimInfo[0] ) { // We've found the correct frame.
victimInfo[1] = this.frameTable.getFrameTable()[i].getPID();
victimInfo[2] = this.frameTable.getFrameTable()[i].getPage();
}
}
return victimInfo;
}
/**********************************************************
* 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 victim.
***********************************************************/
public int [] searchVictimPair (int frame ) {
return this.frameTable.searchVictim(frame);
}
/*****************************************************
* Updates the frame table with a new frame as space
* allows or page faults occurs.
* @param frame is the frame table entry to be updated
*****************************************************/
public void updateFrameTable(int frame, int process, int page) {
this.frameTable.insertFrameEntry(frame, process, page);
}
/*******************************************************
* Prints the current state of the frame table to screen.
* Used in B-level logic of the program.
*******************************************************/
public void printFrameTableState() {
this.frameTable.printCurrentState();
}
/******************************************************************
* Prints the current state of the given PCB's page table to screen.
* @param pid is the PID of the process.
******************************************************************/
public void printPageTableState( int pid ) {
this.processTable.printPageTable(pid);
}
/***************************************************************
* Prints the total memory references for each process to screen.
****************************************************************/
public void printFinalStats () {
this.processTable.printStats();
}
/*************************************************
* Passes the array of PCBs to the controller.
* The array is then passed to the GUI for printing
* final statistics.
* @return an array of PCBs
**************************************************/
public PCB [] passPCBArray () {
return this.processTable.getPCBArray();
}
}