-
Notifications
You must be signed in to change notification settings - Fork 0
/
OS_Page_Replacement_Algo.cpp
637 lines (534 loc) · 15.3 KB
/
OS_Page_Replacement_Algo.cpp
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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
#include <bits/stdc++.h>
using namespace std;
const int MISS = 0;
const int TOTAL = 1;
// Declaration of Classes
class history;
class handler;
class input;
class output;
class analyze;
class Process;
class RAM;
class algoData;
class FIFO;
class LRU;
class MRU;
// Defination of Classes
class history
{
private:
static history *currentInstance;
vector<pair<input *, output *>> hist;
public:
// deleting copy constructor
// history(const history &obj) = delete;
static history *getInstance();
history(){};
void updateHistory(input *in, output *out);
pair<input *, output *> getLastElement();
void printCurrentStats();
};
class handler
{
int RAMSize;
int noOfProcess;
int processSize;
handler(int RAMSize, int noOfProcess, int processSize);
public:
static handler *createHandler();
public:
void analyzeOnAllPageSize();
public:
void printAnalyzedData();
};
class analyze
{
int noOfProcess;
int noOfPages;
int noOfRAMPages;
vector<vector<int>> curOutput;
analyze(int noOfProcess, int RAMSize, int processSize);
public:
static analyze *createAnalyze(int noOfProcess, int RAMSize, int processSize, int pageSize);
public:
void runProcesses();
private:
void mergeOutput(vector<vector<int>> curOutput);
};
class process
{
int noOfRAMPages;
int noOfPages;
vector<int> pageID; // randomly generated, with length predefined
process(int noOfPages, int noOfRAMPages, vector<int> pageID);
public:
static process *createProcess(int noOfPages, int noOfRAMPages);
public:
vector<vector<int>> runProcess();
};
class RAM
{
int noOfRAMPages;
int noOfPages;
vector<int> pageID;
public:
RAM(int noOfRAMPages, int noOfPages, vector<int> pageID);
public:
virtual vector<int> processRAM(int noOfPages, int noOfRAMPages, vector<int> pageID) = 0;
};
class algoData
{
public:
algoData(function<RAM *(int, int, vector<int>)> createFunction, int algoID);
public:
function<RAM *(int, int, vector<int>)> createFunction;
int algoID;
};
// input
class input
{
int RAMSize;
int noOfProcess;
int processSize;
input(int RAMSize, int noOfProcess, int processSize);
private:
static input *getInput();
public:
static void createHistory();
public:
int getRAMSize();
public:
int getNoOfProcess();
public:
int getProcessSize();
};
class output
{
output();
public:
vector<vector<vector<int>>> mainOutput;
public:
void mergeOutput(vector<vector<int>> curOutput);
public:
static output *getOutput();
};
class FIFO : public RAM
{
public:
FIFO(int noOfRAMPages, int noOfPages, vector<int> pageID) : RAM(noOfRAMPages, noOfPages, pageID){};
public:
vector<int> processRAM(int noOfPages, int noOfRAMPages, vector<int> pageID) override;
};
class LRU : public RAM
{
public:
LRU(int noOfRAMPages, int noOfPages, vector<int> pageID) : RAM(noOfRAMPages, noOfPages, pageID){};
public:
vector<int> processRAM(int noOfPages, int noOfRAMPages, vector<int> pageID) override;
};
class MRU : public RAM
{
public:
MRU(int noOfRAMPages, int noOfPages, vector<int> pageID) : RAM(noOfRAMPages, noOfPages, pageID){};
public:
vector<int> processRAM(int noOfPages, int noOfRAMPages, vector<int> pageID) override;
};
class OPT : public RAM
{
public:
OPT(int noOfRAMPages, int noOfPages, vector<int> pageID) : RAM(noOfRAMPages, noOfPages, pageID){};
public:
vector<int> processRAM(int noOfPages, int noOfRAMPages, vector<int> pageID) override;
};
// Mapping of algo to its name
unordered_map<string, algoData *> mapping = {{"OPT", new algoData([&](int noOfRAMPages, int noOfPages, vector<int> pageID)
{ return new OPT(noOfRAMPages, noOfPages, pageID); }, 1)},
{"FIFO", new algoData([&](int noOfRAMPages, int noOfPages, vector<int> pageID)
{ return new FIFO(noOfRAMPages, noOfPages, pageID); }, 2)},
{"LRU", new algoData([&](int noOfRAMPages, int noOfPages, vector<int> pageID)
{ return new LRU(noOfRAMPages, noOfPages, pageID); }, 3)},
{"MRU", new algoData([&](int noOfRAMPages, int noOfPages, vector<int> pageID)
{ return new MRU(noOfRAMPages, noOfPages, pageID); }, 4)}};
// Defination of Functions
// history class
history *history::currentInstance = NULL;
void history::printCurrentStats()
{
input *currInput = hist.back().first;
output *currOutput = hist.back().second;
int noOfRows = currOutput->mainOutput.size();
int noOfColumns = mapping.size()+1;
vector<vector<string>> table(noOfRows,vector<string>(noOfColumns));
cout << "Results:" << endl;
table[0][0] = "Page Size";
for(int i=1;i<noOfRows;i++){
table[i][0] = to_string(i);
}
for(auto it:mapping){
table[0][it.second->algoID] = it.first+"(Hit Rate)";
for (int i = 1; i < noOfRows; i++)
{
int total = currOutput->mainOutput[i][it.second->algoID][TOTAL];
int miss = currOutput->mainOutput[i][it.second->algoID][MISS];
table[i][it.second->algoID] = to_string(1.0 * (total - miss) / total);
}
}
for(int j=0;j<noOfColumns;j++){
int mxLen = 0;
for(int i=0;i<noOfRows;i++){
mxLen = max(mxLen,(int)table[i][j].size());
}
for(int i=0;i<noOfRows;i++){
while(((int)table[i][j].size()) != mxLen){
table[i][j] += ' ';
}
}
}
vector<string> rowString(noOfRows+2);
for(int i=0;i<noOfRows;i++){
rowString[i+1] += "| ";
for(int j=0;j<noOfColumns;j++){
rowString[i+1] += table[i][j];
rowString[i+1] += " | ";
}
}
while(rowString[0].size() != rowString[1].size()){
rowString[0] += "-";
rowString[noOfRows+1] += "-";
}
for(auto it: rowString){
cout<<it<<endl;
}
}
history *history::getInstance()
{
if (currentInstance == NULL)
{
// We can access private members
// within the class.
currentInstance = new history();
// returning the instance pointer
return currentInstance;
}
else
{
return currentInstance;
}
}
void history::updateHistory(input *in, output *out)
{
hist.push_back({in, out});
}
pair<input *, output *> history::getLastElement()
{
return hist.back();
}
// output class
output::output()
{
}
output *output::getOutput()
{
return new output();
}
void output::mergeOutput(vector<vector<int>> curOutput)
{
this->mainOutput.push_back(curOutput);
}
// input class
input::input(int RAMSize, int noOfProcess, int processSize)
{
this->RAMSize = RAMSize;
this->noOfProcess = noOfProcess;
this->processSize = processSize;
}
input *input::getInput()
{
int RAMSize;
int noOfProcess;
int processSize;
cout << "Enter the number of processes on which you want to test the compatability of different algorithms" << endl;
cin >> noOfProcess;
cout << "Enter the RAM size for which you want to test the compatability of different algorithms" << endl;
cin >> RAMSize;
cout << "Enter the size of the process on which you want to test the compatability of different algorithms" << endl;
cin >> processSize;
return new input(RAMSize, noOfProcess, processSize);
}
void input::createHistory()
{
input *in = input::getInput();
output *out = output::getOutput();
history *instance = history::getInstance();
instance->updateHistory(in, out);
}
int input::getRAMSize()
{
return this->RAMSize;
}
int input::getNoOfProcess()
{
return this->noOfProcess;
}
int input::getProcessSize()
{
return this->processSize;
}
// Handler Class
handler::handler(int RAMSize, int noOfProcess, int processSize)
{
this->RAMSize = RAMSize;
this->noOfProcess = noOfProcess;
this->processSize = processSize;
}
handler *handler::createHandler()
{
history *instance = history::getInstance();
input::createHistory();
input *curInput = instance->getLastElement().first;
return new handler(curInput->getRAMSize(), curInput->getNoOfProcess(), curInput->getProcessSize());
}
void handler::analyzeOnAllPageSize()
{
for (int curPageSize = 0; curPageSize <= min(RAMSize,processSize); curPageSize++)
{
analyze *curAnalyze = analyze::createAnalyze(noOfProcess, RAMSize, processSize, curPageSize);
curAnalyze->runProcesses();
}
}
void handler::printAnalyzedData()
{
history *instance = history::getInstance();
instance->printCurrentStats();
}
// Analyze Class
analyze::analyze(int noOfProcess, int noOfPages, int noOfRAMPages)
{
this->noOfProcess = noOfProcess;
this->noOfPages = noOfPages;
this->noOfRAMPages = noOfRAMPages;
}
analyze *analyze::createAnalyze(int noOfProcess, int RAMSize, int processSize, int pageSize)
{
int noOfPages = (pageSize == 0 ? -1 : ((processSize + pageSize - 1) / pageSize));
int noOfRAMPages = (pageSize == 0 ? -1 : (RAMSize / pageSize));
return new analyze(noOfProcess, noOfPages, noOfRAMPages);
}
void analyze::runProcesses()
{
for (int i = 0; i < noOfProcess; i++)
{
process *curProcess = process::createProcess(noOfPages, noOfRAMPages);
vector<vector<int>> curOutput = curProcess->runProcess();
mergeOutput(curOutput);
}
history *instance = history::getInstance();
output *mainOutput = instance->getLastElement().second;
mainOutput->mergeOutput(this->curOutput);
}
void analyze::mergeOutput(vector<vector<int>> curOutput)
{
for (int i = 0; i < curOutput.size(); i++)
{
if (this->curOutput.size() > i)
{
this->curOutput[i][TOTAL] += curOutput[i][TOTAL];
this->curOutput[i][MISS] += curOutput[i][MISS];
}
else
{
this->curOutput.push_back(curOutput[i]);
}
}
}
// Process class
process::process(int noOfPages, int noOfRAMPages, vector<int> pageID)
{
this->noOfPages = noOfPages;
this->noOfRAMPages = noOfRAMPages;
this->pageID = pageID;
}
process *process::createProcess(int noOfPages, int noOfRAMPages)
{
vector<int> pageID;
int noOfBlocks = 100 * noOfPages;
for (int i = 0; i < noOfBlocks; i++)
{
int pid = (rand() % noOfPages) + 1;
pageID.push_back(pid);
}
return new process(noOfPages, noOfRAMPages, pageID);
}
vector<vector<int>> process::runProcess()
{
// Added 1 element to make it 1-based indexed
vector<vector<int>> processOutput(mapping.size()+1,vector<int>(2,-1));
for (auto it : mapping)
{
if (noOfPages == -1)
{
processOutput[it.second->algoID] = {-1, -1};
continue;
}
RAM *process = it.second->createFunction(noOfPages, noOfRAMPages, pageID);
processOutput[it.second->algoID] = process->processRAM(noOfPages, noOfRAMPages, pageID);
}
return processOutput;
}
// RAM class
RAM::RAM(int noOfRAMPages, int noOfPages, vector<int> pageID)
{
this->noOfPages = noOfPages;
this->noOfRAMPages = noOfRAMPages;
this->pageID = pageID;
}
// algoData class
algoData::algoData(function<RAM *(int, int, vector<int>)> createFunction, int algoID)
{
this->algoID = algoID;
this->createFunction = createFunction;
}
// FIFO class
vector<int> FIFO::processRAM(int noOfPages, int noOfRAMPages, vector<int> pageID)
{
int missCount = 0;
int total = pageID.size();
unordered_set<int> chachedPages;
queue<int> inOrder;
for (int i = 0; i < pageID.size(); i++)
{
if (!chachedPages.count(pageID[i]))
{
missCount++;
if (chachedPages.size() == noOfRAMPages){
chachedPages.erase(inOrder.front());
inOrder.pop();
}
chachedPages.insert(pageID[i]);
inOrder.push(pageID[i]);
}
}
return {missCount, total};
}
// LRU class
vector<int> LRU::processRAM(int noOfPages, int noOfRAMPages, vector<int> pageID)
{
int missCount = 0;
int total = pageID.size();
unordered_set<int> s;
unordered_map<int, int> indexes;
for (int i = 0; i < pageID.size(); i++)
{
if (s.size() < noOfRAMPages)
{
if (s.find(pageID[i]) == s.end())
{
s.insert(pageID[i]);
missCount++;
}
indexes[pageID[i]] = i;
}
else
{
if (s.find(pageID[i]) == s.end())
{
int lru = INT_MAX, val;
for (auto it = s.begin(); it != s.end(); it++)
{
if (indexes[*it] < lru)
{
lru = indexes[*it];
val = *it;
}
}
s.erase(val);
s.insert(pageID[i]);
missCount++;
}
indexes[pageID[i]] = i;
}
}
return {missCount, total};
}
// MRU class
vector<int> MRU::processRAM(int noOfPages, int noOfRAMPages, vector<int> pageID)
{
int missCount = 0;
int total = pageID.size();
unordered_set<int> s;
unordered_map<int, int> indexes;
for (int i = 0; i < pageID.size(); i++)
{
if (s.size() < noOfRAMPages)
{
if (s.find(pageID[i]) == s.end())
{
s.insert(pageID[i]);
missCount++;
}
indexes[pageID[i]] = i;
}
else
{
if (s.find(pageID[i]) == s.end())
{
int mru = -1, val;
for (auto it = s.begin(); it != s.end(); it++)
{
if (indexes[*it] > mru)
{
mru = indexes[*it];
val = *it;
}
}
s.erase(val);
s.insert(pageID[i]);
missCount++;
}
indexes[pageID[i]] = i;
}
}
return {missCount, total};
}
// OPT class
vector<int> OPT::processRAM(int noOfPages, int noOfRAMPages, vector<int> pageID)
{
int missCount = 0;
int total = pageID.size();
vector<int> nxtOcc(total,total);
map<int,int> nearestOcc;
for(int i=total-1;i>=0;i--){
if(nearestOcc.find(pageID[i]) != nearestOcc.end()){
nxtOcc[i] = nearestOcc[pageID[i]];
}
nearestOcc[pageID[i]] = i;
}
unordered_set<int> chachedPages;
set<int> nxtOccOfChachedPages;
for (int i = 0; i < pageID.size(); i++)
{
if(!chachedPages.count(pageID[i])){
if(chachedPages.size() == noOfRAMPages){
int pageToRemove = pageID[*nxtOccOfChachedPages.rbegin()];
nxtOccOfChachedPages.erase(*nxtOccOfChachedPages.rbegin());
chachedPages.erase(pageToRemove);
}
chachedPages.insert(pageID[i]);
nxtOccOfChachedPages.insert(nxtOcc[i]);
missCount++;
}
else{
nxtOccOfChachedPages.erase(i);
nxtOccOfChachedPages.insert(nxtOcc[i]);
}
}
return {missCount, total};
}
int main()
{
handler *run = handler::createHandler();
run->analyzeOnAllPageSize();
run->printAnalyzedData();
return 0;
}