先决条件–最近最少使用(LRU)页面替换算法
最近使用最少的页面替换算法将替换最近不使用的页面。
执行:
在本文中,LRU是使用计数器实现的,一个ctime(即计数器)变量用于表示当前时间,它对于参考数组的每一页都会递增。一对向量用于表示页面帧,该对的第一个变量是页码,第二个变量是当前时间。当要插入一个新页面且帧已满时,将删除ctime最小的页面(因为它是最近最少使用的页面)。如果再次访问该页面,则ctime的值将更新。
笔记:
最小ctime(计数器/当前时间)值表示最近最少使用的页面。
例子:
Reference array is : 0, 0, 0, 2, 3, 0, 5, 7, 1, 2, 0, 8
Output :
When the number of frames is : 3
The number of page faults are : 9
Reference array is : 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 0, 0
Output :
When the number of frames is : 3
The number of page faults are : 15
代码:
#include
using namespace std;
// To calculate the number of page faults
void pageFaults(int frame_size, int* ref, int len, int ctime)
{
// Count variable to count the
// number of page faults
int cnt = 0;
// Arr to simulate frames
vector > arr;
// To initialise the array
for (int i = 0; i < frame_size; i++) {
arr.push_back(make_pair(-1, ctime));
}
int page;
for (int i = 0; i < len; i++) {
page = ref[i];
auto it = arr.begin();
for (it = arr.begin(); it != arr.end(); it++) {
if (it->first == page) {
break;
}
}
// If page is found
if (it != arr.end()) {
// update the value of
// current time
it->second = ctime;
}
// If page is not found
else {
// Find the page with min value of ctime,
// as it will be the least recently used
vector >::iterator pos;
pos = arr.begin();
int min = pos->second;
for (auto itr = arr.begin(); itr != arr.end(); itr++) {
if (itr->second < min) {
pos = itr;
min = pos->second;
}
}
// Erase this page from the frame vector
arr.erase(pos);
// Insert the new page
arr.push_back(make_pair(page, ctime));
cnt++;
}
ctime++;
}
cout << "The number of page faults is : " << cnt << endl;
}
int main()
{
// This is the reference array
int ref[] = { 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5 };
int len = sizeof(ref) / sizeof(ref[0]);
int frame_size = 3;
// Ctime represents current time,
// it is incremented for every page
int ctime = 0;
pageFaults(frame_size, ref, len, ctime);
}
输出:
The number of page faults is : 10