/
MemoryandPaging.c
480 lines (401 loc) · 13.4 KB
/
MemoryandPaging.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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
//Matthew DiLandro
//Assignment 4
//Server
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/queue.h>
#include <time.h>
#include <limits.h>
#define AllPageTablesSize 1024
#define numFrames 32
#define frameSize 1024
#define numPages 4
#define maxData 50000
struct PageTable
{
char * Name;
int * PageNumber; //array of page number
int * MemoryLocation; //indices stored parallel to PageNumber
int offset; //where we start
int bytesincache; //Says the amount of bytes in cache
clock_t PageTableUseTime;
};
struct Memory{
char ** frames;
int * frameBytes;
struct PageTable ** AllPageTables;
int freeFrames;
int numPageTables;
};
struct PageTable * CreatePageTable(){
struct PageTable * newPageTable = malloc(sizeof(struct PageTable));
newPageTable->PageNumber = calloc(numPages, sizeof(int));
int i;
for(i=0;i<numPages;i++){
newPageTable->PageNumber[i] = -1;
}
newPageTable->MemoryLocation = calloc(numPages, sizeof(int));
for(i=0;i<numPages;i++){
newPageTable->MemoryLocation[i] = -1;
}
newPageTable->offset = 0;
newPageTable->PageTableUseTime = clock(); //remind miguel to make clock();
return newPageTable;
}
struct Memory * CreateMemory(){
struct Memory * MemoryBank = malloc(sizeof(struct Memory));
MemoryBank->frames = calloc(numFrames, frameSize);
int i;
for(i=0;i<numFrames;i++){
MemoryBank->frames[i] = "-1";
}
MemoryBank->AllPageTables = malloc(AllPageTablesSize);
MemoryBank->freeFrames = numFrames;
MemoryBank->numPageTables = 0;
MemoryBank->frameBytes = malloc(AllPageTablesSize*sizeof(int));
return MemoryBank;
}
void Merge(struct PageTable **pt, struct PageTable **Lpt, int leftCount, struct PageTable **Rpt, int rightCount) {
int i,j,k;
i = 0; j = 0; k =0;
while(i<leftCount && j< rightCount) {
if(Lpt[i]->PageTableUseTime > Rpt[j]->PageTableUseTime) pt[k++] = Lpt[i++];
else pt[k++] = Rpt[j++];
}
while(i < leftCount) pt[k++] = Lpt[i++];
while(j < rightCount) pt[k++] = Rpt[j++];
}
void MergeSort(struct PageTable **pt, int n) {
int mid,i;
struct PageTable **L = malloc(sizeof(struct PageTable*)*((n/2) + 1));
struct PageTable **R = malloc(sizeof(struct PageTable*)*((n/2) + 1));
if(n < 2) return;
mid = n/2;
L = malloc(mid*sizeof(struct PageTable));
R = malloc((n- mid)*sizeof(struct PageTable));
for(i = 0;i<mid;i++) L[i] = pt[i];
for(i = mid;i<n;i++) R[i-mid] = pt[i];
MergeSort(L,mid);
MergeSort(R,n-mid);
Merge(pt,L,mid,R,n-mid);
free(L);
free(R);
}
void DeleteOldest(struct Memory* MemoryBank, int deletions){
int removedframes = 0;
struct PageTable ** Temp = calloc(MemoryBank->numPageTables,sizeof(struct PageTable));
int i;
while(removedframes < deletions){
for (i = 0; i < MemoryBank->numPageTables; i++){
Temp[i] = MemoryBank->AllPageTables[i]; //THIS IS rearranging actual memorybank FIX THIS*****
}
MergeSort(Temp, MemoryBank->numPageTables);
for(i=0;i<numPages;i++){
if(Temp[0]->PageNumber[i] == -1){
continue;
}
else{
//Delete it from cache
MemoryBank->frames[Temp[0]->MemoryLocation[i]] = "-1"; //THERE SHOULD BE FREE'S HERE
Temp[0]->PageNumber[i] = -1;
Temp[0]->MemoryLocation[i] = -1;
MemoryBank->freeFrames++;
removedframes = removedframes + 1;
}
if (removedframes < deletions) { break; }
}
if (removedframes < deletions) { break; }
for (i = 0; i < MemoryBank->numPageTables; i++){//looking for position of the thing we are deleting in original frames
if(strcmp(Temp[0]->Name,MemoryBank->AllPageTables[i]->Name) == 0){
int j;
//check if all the pageNums and CacheLocs == -1, if so delete the page table
//****NEED TO ADD IF STATEMENT******
for (j = 0; j < MemoryBank->numPageTables; j++){ //move all page tables
MemoryBank->AllPageTables[j] = MemoryBank->AllPageTables[j+1];
}
MemoryBank->numPageTables = MemoryBank->numPageTables-1;
}
}
}
}
char ** GetDirDat(int offset, int length, char* fileName);
//given a page table, same data, and a pagenum, store the data in a CacheLoc and link the PageNum to the CacheLoc
void AddToPageTable(struct PageTable* pt, struct Memory* MemoryBank, int pageNum, char* data, char* name, int offset, int length)
{
int min = INT_MIN;
int index = 0;
int* memLocs;// = malloc(sizeof(int));
int i;
for (i = 0; i < numPages; i++)
{
//look for the first -1 you can find.
if (pt->PageNumber[i] == -1)
{
//if the pagenum is -1 and the cacheLoc is -1, find a frame in CacheMem to store stuff
index = i;
memLocs = findEmpty(MemoryBank, 1);
break;
}
else if(pt->PageNumber[i] > min) //Otherwise just overwrite the smallest indexed thing
{
index = i;
min = pt->PageNumber[i];
}
}
//reassign pt->pageNumber[index] to pageNum(provided)
/*DO I NEED TO SET NAME HERE? SHOULD I HAVE THAT PASSED AS AN ARGUMENT?********/
pt->Name = name; //set the name too just in case
pt->PageNumber[index] = pageNum;
pt->MemoryLocation[index] = memLocs[0]; //store actual data in the frame we have reserved
pt->offset = offset;
pt->bytesincache = length;
pt->PageTableUseTime = clock();
}
int* findEmpty(struct Memory* MemoryBank, int framesNecessary){
if (framesNecessary < MemoryBank->freeFrames){
//Find the first ones in memory
}
else if (framesNecessary > MemoryBank->freeFrames){
//delete that many files
DeleteOldest(MemoryBank, framesNecessary - MemoryBank->freeFrames);
}
int * framesreturned = calloc(framesNecessary,sizeof(int));
int size = 0;
int i;
for(i=0;i<numFrames;i++){
if(strcmp(MemoryBank->frames[i],"-1")==0)
{
framesreturned[size] = i;
size++;// = size + 1;
}
if(size == framesNecessary){
break;
}
}
return framesreturned;
}
//function for returning full information from cache
char* returnFull(struct PageTable* pt, struct Memory* MemoryBank, int offset, int length, int miguel)
{
char * ret = malloc(maxData*sizeof(char));
//find start point
int index = 0;
int max = INT_MAX;
int i;
for (i = 0; i < numPages; i++)
{
if (pt->PageNumber[i] < max)
{
index = i;
max = pt->PageNumber[i];
}
}
int sentBytes = 0;
while (sentBytes < length-offset)
{
//send message SENT ACK frameBytes to client
// ************************send some more crap************************
strcat(ret, MemoryBank->frames[pt->MemoryLocation[index]]);
sentBytes += MemoryBank->frameBytes[pt->MemoryLocation[index]];
index++;
}
pt->PageTableUseTime = clock();
return ret;
}
//start of the find is missing
char* returnPartial(struct PageTable* pt, struct Memory* MemoryBank, int offset, int length, int miguel)
{
char * ret = malloc(maxData*sizeof(char));
//find start point
int index = 0;
int max = INT_MAX;
int i;
for (i = 0; i < numPages; i++)
{
if (pt->PageNumber[i] < max)
{
index = i;
max = pt->PageNumber[i];
}
}
//figure out how many frames are necessary to add to the start
int startDifference = pt->offset - offset;
if (startDifference % frameSize == 0) // if it divides evenly don't add one
{
startDifference = startDifference/frameSize;
}
else
{
startDifference = (startDifference/frameSize)+1;
}
//figure out how many frames are necessary to add to the end
int endDifference = offset + length - pt->offset + pt->bytesincache;
if (endDifference % frameSize == 0) // if it divides evenly don't add one
{
endDifference = startDifference/frameSize;
}
else
{
endDifference = (startDifference/frameSize)+1;
}
int startTemp = startDifference;
int endTemp = (startDifference + numPages)-1;
char * temp[(endTemp-startTemp)+1];// = malloc((endTemp - startTemp)*sizeof(char*));
//go through and save all the information we have cached on the desired data and store it in a temp
for (i = 0; i < (endTemp-startTemp)+1; i++)
{
temp[i] = MemoryBank->frames[pt->PageNumber[index + i]]; //char* =
}
//go get the start information from dir
char** startStuff = GetDirDat(offset, startDifference, pt->Name);
//go get the end information from dir
char** endStuff = GetDirDat(endTemp, endDifference, pt->Name);
int startBytesWritten = 0;
int cacheBytesWritten = 0;
int endBytesWritten = 0;
i = 0;
int j = 0;
int k = 0;
int l = index;
//now go through all the stuff we don't have at the start, then the stuff we do have, then the stuff we don't have at the end, and cache them/return them
while (cacheBytesWritten + startBytesWritten + endBytesWritten < length-offset)
{
if (startBytesWritten < startDifference)
{
strcat(ret, startStuff[i]);
startBytesWritten += 999999;
AddToPageTable(pt, MemoryBank, l, startStuff[i]); //given pt, MemoryBank, index, dataToAdd *****IF DOESN"T HAVE PHYSICAL MEMORY LOCATION, MAKE ONE
i++;
l++;
}
else if(cacheBytesWritten < length-offset)
{
strcat(ret, temp[j]);
cacheBytesWritten += 999999;
AddToPageTable(pt, MemoryBank, l, temp[i]); //given pt, MemoryBank, index, dataToAdd *****IF DOESN"T HAVE PHYSICAL MEMORY LOCATION, MAKE ONE
j++;
l++;
}
else if(endBytesWritten < endDifference)
{
strcat(ret, endStuff[k]);
endBytesWritten += 9999999;
AddToPageTable(pt, MemoryBank, l, endStuff[i]); //given pt, MemoryBank, index, dataToAdd *****IF DOESN"T HAVE PHYSICAL MEMORY LOCATION, MAKE ONE
k++;
l++;
}
}
pt->PageTableUseTime = clock();
return ret;
}
//end of the find is missing
/*char* returnPartialEnd(struct PageTable* pt, struct Memory* MemoryBank, int offset, int length, int miguel)
{
char * ret = malloc(maxData*sizeof(char));
//find start point
int index = 0;
int max = INT_MAX;
int i;
for (i = 0; i < numPages; i++)
{
if (pt->PageNumber[i] < max)
{
index = i;
max = pt->PageNumber[i];
}
}
int sentBytes = 0;
while (sentBytes < length-offset)
{
//send message SENT ACK frameBytes to client
// ************************send some more crap************************
strcat(ret, MemoryBank->frames[pt->MemoryLocation[index]]);
sentBytes += MemoryBank->framesBytes[pt->[index]];
}
pt->PageTableUseTime = clock();
return ret;
}
//Both start and end are missing
char* returnPartialStartEnd(struct PageTable* pt, struct Memory* MemoryBank, int offset, int length, int miguel)
{
char * ret = malloc(maxData*sizeof(char));
//find start point
int index = 0;
int max = INT_MAX;
int i;
for (i = 0; i < numPages; i++)
{
if (pt->PageNumber[i] < max)
{
index = i;
max = pt->PageNumber[i];
}
}
int sentBytes = 0;
while (sentBytes < length-offset)
{
//send message SENT ACK frameBytes to client
// ************************send some more crap************************
strcat(ret, MemoryBank->frames[pt->[index]]);
sentBytes += MemoryBank->framesBytes[pt->[index]];
}
pt->PageTableUseTime = clock();
return ret;
}*/
char* FindInCache(struct Memory* MemoryBank, char* fileName, int offset, int length, int miguel)
{
//find pagetable for fileName
struct PageTable *pt;
short inPageTable = 0;
//find pagetable start
int i;
for (i = 0; i < MemoryBank->numPageTables; i++)
{
if(strcmp(MemoryBank->AllPageTables[i]->Name, fileName) == 0)
{
pt = MemoryBank->AllPageTables[i];
inPageTable = 1;
break;
}
}
return returnPartial(pt, MemoryBank, offset, length, 0);//LAST NUMBER IS MIGUEL
//if found it's either in partial or full
/*if (inPageTable)
{
short startGap = 0;
short endGap = 0;
//check if the start is before or after requested start
if (pt->offset > offset) //(if start in cache is after start asked for)
{
startGap = 1;
}
//check if the end is before or after the requested end
if (pt->offset + pt->bytesincache < offset + length)
{
endGap = 1;
}
//full find
if (!startGap && !endGap)
{
}
//if partial
//if we have only a start gap
if(startGap && !endGap)
{
}
else if(!startGap && endGap) //if we only have an end gap
{
}
else //if we have both a start and end gap
{
}
}
else //else it's not in there at all
{
}*/
}
int main( int argc, char *argv[] ){
struct Memory * MemoryBank = CreateMemory();
return EXIT_SUCCESS;
}