Program for Page Replacement Algorithms (LRU)

// LRU.cpp : This file contains the 'main' function. Program execution begins and ends there.
//

#include "pch.h"
#include <iostream>
#include <unordered_map>
#include <unordered_set>
using namespace std;


int pageFaults(int pages[], int n, int capacity)
{

unordered_set<int> s;

unordered_map<int, int> indexes;

// Start from initial page
int page_faults = 0;
for (int i = 0; i < n; i++)
{
// Check if the set can hold more pages
if (s.size() < capacity)
{
// Insert it into set if not present
// already which represents page fault
if (s.find(pages[i]) == s.end())
{
s.insert(pages[i]);

// increment page fault
page_faults++;
}

// Store the recently used index of
// each page
indexes[pages[i]] = i;
}

// If the set is full then need to perform lru
// i.e. remove the least recently used page
// and insert the current page
else
{
// Check if current page is not already
// present in the set
if (s.find(pages[i]) == s.end())
{
// Find the least recently used pages
// that is present in the set
int lru = INT_MAX, val;
for (auto it = s.begin(); it != s.end(); it++)
{
if (indexes[*it] < lru)
{
lru = indexes[*it];
val = *it;
}
}

// Remove the indexes page
s.erase(val);

// insert the current page
s.insert(pages[i]);

// Increment page faults
page_faults++;
}

// Update the current page index
indexes[pages[i]] = i;
}
}

return page_faults;
}

// Main Function
int main()
{
int pages[] = { 7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2 };
int n = sizeof(pages) / sizeof(pages[0]);
int capacity = 4;
cout << pageFaults(pages, n, capacity);
return 0;
}

Comments

Popular posts from this blog

Dynamic Memory allocation in C++ for 1d, 2d and 3d array

Write Your Own String Class:

Printing the odd and even no using Pthread