-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRandom.cpp
More file actions
64 lines (53 loc) · 1.68 KB
/
Random.cpp
File metadata and controls
64 lines (53 loc) · 1.68 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
// FILE: Random.cpp
// A Bautista, B Franco, E Mora
// OS, Fall 2023, Transy U
//
// Implementation for Random that contains all functions needed to page a process using the Random algorithm
//
#include "Random.h"
#include "Table.h"
#include <iostream>
using namespace std;
Random::Random(){
pageFaults = 0;
srand(time(NULL));
}
Random::~Random(){}
int Random::randomPager(queue<int>& addresses,Table& table){
int freeFrame, address, victimPage, victimFrame, page;
int iteration = 0;
while(!addresses.empty()){
//print out page table before manipulation
cout << "iteration: " << iteration++ << endl;
table.print();
//push all addresses to its specific page
address = addresses.front();
addresses.pop();
page = table.addressPage(address);
//if valid bit is not set increase page faults and find a free frame
if(!table.valid(page)){
pageFaults++;
freeFrame = table.freeFrame();
//if we have a free frame put page there
if(freeFrame != -1){
cout << "\t\tfreeFrame: " << freeFrame << endl;
table.load(page,freeFrame);
table.setValid(page);
}
// page replacement must be conducted
//victim is selected and removed randomly
else{
victimPage = selectVictim(table);
table.setInvalid(victimPage);
cout << "\t\tvictimPage: " << victimPage << endl;
table.load(page,table.getFrame(victimPage));
table.setValid(page);
}
}
cout << "pageFaults: " << pageFaults << endl << endl;
}
return pageFaults;
}
int Random::selectVictim(Table& table){
return table.findPage(rand()%table.totalFrames());
}