-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlru.cpp
More file actions
56 lines (49 loc) · 1.52 KB
/
lru.cpp
File metadata and controls
56 lines (49 loc) · 1.52 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
#include "lru.h"
namespace coen383
{
//CONSTRUCTOR
lru::lru()
{
pageRequest = 0;
numElements = 0;
pageNumber = 0;
numCount = 0;
//*used for debugging*//
// myfile.open ("output");
}
void lru::pageReplaceAlgo()
{
tableitr = table.find(pageRequest);
if(tableitr == table.end())
{
//page does not already exist in the cache
std::cout<<"Page number "<<pageRequest<<" caused a page fault."<<std::endl;
myfile<<"Page number "<<pageRequest<<" caused a page fault.\n";
llistitr = llist.insert(llist.begin(),pageRequest);
table.insert(std::pair <int, std::list <int> :: iterator> (pageRequest,llistitr));
if(table.size() > tableSize)
{
table.erase(llist.back());
llist.pop_back();
}
numElements++;
numCount++;
}
else
{
///page already exists in the cache
std::cerr<<"Page number "<<pageRequest<<" caused a page hit."<<std::endl;
//remove node from list and remove pair element in table
llist.erase(table[pageRequest]);
table.erase(tableitr);
//make new node and pair with value and insert at the beginning of the list
llistitr = llist.insert(llist.begin(),pageRequest);
table.insert(std::pair <int, std::list <int> :: iterator> (pageRequest,llistitr));
}
//*used for debugging*//
// std::cerr<<"llist: ";
// for (std::list<int>::const_iterator it = llist.begin(); it != llist.end(); ++it)
// std::cerr<<*it<<" ";
// std::cerr<<std::endl;
}
}