-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlru_clock.cpp
92 lines (77 loc) · 1.67 KB
/
lru_clock.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
#include <iostream>
#include <vector>
#include <climits>
int find_page(int page_table[], int size, int req){
int ind = -1;
for (int i = 0; i < size; ++i)
{
if(page_table[i]==req){
ind = i;
return ind;
}
}
return ind;
}
void print_page_tabe(int page_table[], int use[], int no_of_frames){
for (int i = 0; i < no_of_frames; ++i)
{
std::cout << page_table[i] << "\t";
}
std::cout << std::endl;
for (int i = 0; i < no_of_frames; ++i)
{
std::cout << use[i] << "\t";
}
std::cout << std::endl;
}
int lru_clock(std::vector<int> requests, int no_of_frames){
int page_table[no_of_frames];
int use[no_of_frames];
for (int i = 0; i < no_of_frames; ++i)
{
use[i] = 0;
page_table[i] = INT_MIN;
}
int size = requests.size();
int pointer = 0;
int ind_found = -1;
int faults = 0;
for (int i = 0; i < size; ++i)
{
ind_found = find_page(page_table, no_of_frames, requests[i]);
if(ind_found != -1){
use[ind_found] = 1;
}else{
while(use[pointer]!=0){
use[pointer] = 0;
pointer = (pointer+1)%no_of_frames;
}
page_table[pointer] = requests[i];
use[pointer] = 1;
pointer = (pointer+1)%no_of_frames;
faults += 1;
}
// print_page_tabe(page_table, use, no_of_frames);
}
return faults - no_of_frames;
}
// int main(int argc, char const *argv[])
// {
// std::vector<int> v;
// int frames;
// v.push_back(2);
// v.push_back(3);
// v.push_back(2);
// v.push_back(1);
// v.push_back(5);
// v.push_back(2);
// v.push_back(4);
// v.push_back(5);
// v.push_back(3);
// v.push_back(2);
// v.push_back(5);
// v.push_back(2);
// int faults = lru_clock(v, 3);
// std::cout<< "No. of faults = " << faults << std::endl;
// return 0;
// }