-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathframeTable.c
286 lines (219 loc) · 6.1 KB
/
frameTable.c
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
#include<stdio.h>
#include "dataTypes.h"
#include "pageTable.h"
#include "frameTable.h"
extern FrameTable frameTable;
extern FILE *outputFile;
/*
Returns a replacable frame for allocation or -1 if no frame is available
*/
int getReplacableEmptyFrame()
{
int i;
for(i=0;i<NUM_FRAMES;i++)
{
if(frameTable.entries[i].emptyBit==0 && frameTable.entries[i].considerInLfu == 1&& frameTable.entries[i].lock==0)
return i;
}
return -1; // returns -1 if no empty frame available
}
/*
Returns an irreplacable frame for allocation or -1 if no frame is available
*/
int getNonReplaceableFrame()
{
int i;
for(i=0;i<NUM_FRAMES;i++)
{
if(frameTable.entries[i].emptyBit==0 && frameTable.entries[i].considerInLfu == 0 )
return i;
}
return -1; // returns -1 if no empty frame available
}
/*
Updates the frame access count which is used in LFU page replacement
in the frame table
*/
int updateLfuCount(int frameNo)
{
fprintf(outputFile, "Frame Table: Updating count for frame# %d\n",frameNo);
if(frameNo>=NUM_FRAMES)
return -1;
frameTable.entries[frameNo].LfuCount.value++;
if(frameTable.entries[frameNo].lock==1)
frameTable.entries[frameNo].lock = 0;
//fprintf(outputFile, "count = %d\n",frameTable.entries[frameNo].LfuCount.value);
return 1;
}
/*
Returns the LFU frame to use for replacement
*/
int getLfuFrameNum()
{
int minCount = frameTable.entries[0].LfuCount.value, indexFrame=0;
int i;
for(i=0;i<NUM_FRAMES;i++)
{
if(frameTable.entries[i].considerInLfu==1 && frameTable.entries[i].LfuCount.value<minCount && frameTable.entries[i].lock==0)
{
minCount = frameTable.entries[i].LfuCount.value;
indexFrame = i;
}
}
return indexFrame;
}
/*
Returns the value of the dirty bit of the given frame number
*/
short unsigned getDirtyBitFrameTable(int index)
{
return frameTable.entries[index].dirtyBit;
}
/*
Sets the value of the dirty bit of the given frame number
to the value given as input by user
*/
short unsigned setDirtyBitFrameTable(int index, int value)
{
frameTable.entries[index].dirtyBit = value;
}
/*
Returns the value of the empty bit of the given frame number
*/
short unsigned getEmptyBitFrameTable(int index)
{
return frameTable.entries[index].emptyBit;
}
/*
Sets the value of the empty bit of the given frame number
to the value given as input by user
*/
short unsigned setEmptyBitFrameTable(int index, int value)
{
frameTable.entries[index].emptyBit=value;
}
/*
Runs the LFU frame ageing alogrithm
*/
int frameAgeing()
{
int i;
for(i=0;i<NUM_FRAMES;i++)
{
frameTable.entries[i].LfuCount.value>>=1;
}
return 1;
}
/*
Finds an empty frame and allocates it to a process.
Takes segment number, page table level in which page fault occured and page index in the pafge table as input
*/
int allocateFrame(int pid,int segno,pageTable *pT,int pageNum,int level)
{
int frameNo = getReplacableEmptyFrame();
if(frameNo!=-1)
{ //placement
fprintf(outputFile, "Frame Table: Frame allocated:%d(placement)\n",frameNo);
frameTable.entries[frameNo].pageNum.value=pageNum;
frameTable.entries[frameNo].pid=pid;
frameTable.entries[frameNo].LfuCount.value=0;
frameTable.entries[frameNo].dirtyBit=0;
frameTable.entries[frameNo].emptyBit=1;
frameTable.entries[frameNo].considerInLfu=1;
frameTable.entries[frameNo].level = level + 1;
frameTable.entries[frameNo].segNum = segno; //Confirm once if this is correct
}
else
{
//replacement
fprintf(outputFile, "Frame Table: No free frame available in memory. Going for replacement\n");
frameNo = getLfuFrameNum();
//swap the page
if(frameTable.entries[frameNo].dirtyBit==1)
{
//func to swap page out
}
//get the reference to the page table of the swapped out page
pageTable* pT2 = getPageTableFromPid(frameTable.entries[frameNo].pid,frameTable.entries[frameNo].segNum,frameTable.entries[frameNo].level);
//chane present bit to 0 in the pagetable
updatePageTablePresentBit(pT2,frameTable.entries[frameNo].pageNum.value,0);
//allocate the frame
fprintf(outputFile, "Frame Table: Frame allocated:%d(replacement)",frameNo);
frameTable.entries[frameNo].pageNum.value=pageNum;
frameTable.entries[frameNo].pid=pid;
frameTable.entries[frameNo].LfuCount.value=0;
frameTable.entries[frameNo].dirtyBit=0;
frameTable.entries[frameNo].emptyBit=1;
frameTable.entries[frameNo].considerInLfu=1;
frameTable.entries[frameNo].level = level + 1;
frameTable.entries[frameNo].segNum = segno;
}
//update page table
fprintf(outputFile, "Frame Table: Updating page table entry of page# %d\n",pageNum);
updatePageTablePresentBit(pT,pageNum,1);
//Lock the frame until accessed first
setLock(frameNo);
//update frame number in page Table
setFrameNo(pT, pageNum, frameNo);
return frameNo;
}
/*setLock
Writes a dirty frame back to disk and sets its empty bit to zero.
*/
int invalidateFrame(int frameNo)
{
if(frameTable.entries[frameNo].dirtyBit==1)
{
//fprintf(outputFile, "Frame Table: Frame# %d is dirty. Writing it back to disk\n",frameNo);
//write back to disk
}
frameTable.entries[frameNo].emptyBit=0;
return 1;
}
/*
Initializes the ADT of the frame table
*/
int initFrameTable()
{
int i;
for(i=0;i<NUM_FRAMES;i++)
{
frameTable.entries[i].emptyBit=0;
frameTable.entries[i].considerInLfu = 1;
frameTable.entries[i].lock = 0;
}
//mark some frames as non-replaceable
for(i=NUM_FRAMES-NUM_NON_REPLACABLE_FRAMES;i<NUM_FRAMES;i++){
frameTable.entries[i].considerInLfu = 0;
}
//Allocate irreplacable frames for Frame Table
for(i = NUM_FRAMES-NUM_NON_REPLACABLE_FRAMES; i < NUM_FRAMES-NUM_NON_REPLACABLE_FRAMES + 512; i++){
frameTable.entries[i].emptyBit = 1;
}
}
/*
Locks the Frame
*/
int setLock(int frameNo)
{
frameTable.entries[frameNo].lock=1;
}
/*
Function to read from memory and update the lfu count
*/
int readFromMemory(int frameNo)
{
if(frameTable.entries[frameNo].lock==1)
frameTable.entries[frameNo].lock = 0;
updateLfuCount(frameNo);
}
/*
Function to write to memory, update the lfu count and set dirty bit of the frame
*/
int writeToMemory(int frameNo)
{
if(frameTable.entries[frameNo].lock==1)
frameTable.entries[frameNo].lock = 0;
setDirtyBitFrameTable(frameNo, 1);
updateLfuCount(frameNo);
}