-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlru_aging.cpp
100 lines (88 loc) · 2.77 KB
/
lru_aging.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
#include <iostream>
#include <vector>
#include <stdio.h>
#include <math.h>
int find_page_in_frames(int frames[], int aging_regs[], int page_num, int num_frames, int history) {
for(int i = 0; i < num_frames; i++) {
aging_regs[i] = aging_regs[i]>>1;
}
for(int i = 0; i < num_frames; i++) {
// If found = HIT
if(frames[i] == page_num) {
aging_regs[i] += pow(2, history);
return i;
}
}
// If not found = MISS
return -1;
}
// Called in case of a MISS
int replace(int frames[], int aging_regs[], int new_page, int num_frames, int history) {
int min_idx = 0;
int min = aging_regs[min_idx];
for(int i = 0; i < num_frames; i++) {
if(aging_regs[i] < min){
min_idx = i;
min = aging_regs[min_idx];
}
}
frames[min_idx] = new_page;
aging_regs[min_idx] += pow(2, history);
return min_idx;
}
void print_regs(int aging_regs[], int num_frames) {
printf("Registers values now :\n");
for(int i = 0; i < num_frames; i++) {
printf(" %d\n", aging_regs[i]);
}
}
void print_frame_state(int frames[], int num_frames) {
printf("Current values in frames are :\n");
for(int i = 0; i < num_frames; i++) {
printf(" %d\n", frames[i]);
}
}
// Takes vector of requests as input and outputs number of page faults
int lru_aging(int requests[], // array containing requests
int num_frames_in_system, // to define the page frames in system
int history_to_look, // how much bits would the register have
int num_requests)
{
int aging_regs[num_frames_in_system] = {0};
int page_faults = 0;
int index = 0;
int frames[num_frames_in_system] = {0};
for (int j = 0; j < num_requests; j++) {
index = find_page_in_frames(frames, aging_regs, requests[j], num_frames_in_system, history_to_look);
if (index == -1) {
page_faults++;
int replaced_frame_idx = replace(frames, aging_regs, requests[j], num_frames_in_system, history_to_look);
// printf("We replaced %d\n", replaced_frame_idx);
} else {
// printf("We replaced nothing !\n");
}
// print_frame_state(frames, num_frames_in_system);
// print_regs(aging_regs, num_frames_in_system);
}
printf("%d\n", page_faults);
return page_faults;
}
// int main() {
// int num_frames = 6;
// int history = 10;
// int requests_length = 15;
//
// // pages in memory :
// // a: 1
// // b: 2
// // c: 3
// // ....
// // and so on...
// // request array consists of page indexes referenced
// int requests[requests_length] = {3, 5, 1, 6, 3, 6, 8, 1, 4, 9, 2, 7, 7, 9, 5};
// int faults = 0;
//
// faults = lru_aging_policy(requests, num_frames, history, requests_length);
//
// return 0;
// }