Question

Write a program that implements the FIFO, Optimal, MFU, and LRU page-replacement algorithms. Given a page-reference...

Write a program that implements the FIFO, Optimal, MFU, and LRU page-replacement algorithms. Given a page-reference string, where page numbers range from 0 to 9, apply the page-reference string to each algorithm, and output the number of page faults incurred by each algorithm. Write your code so that the number of page frames in the page table can vary from 1 to 10. 1.0 Functional Requirements 1.1: Your program shall be run with the following: ./a.out Example: ./a.out datafile.txt 1.2: Your output shall be the number of page faults for each of the four algorithms on a separate line: Page faults of FIFO: 23 Page faults of LRU: 13 Page faults of MRU: 17 Page faults of Optimal: 10 1.2: The datafile will consist of lines of integers. Each line shall start with an integer denoting the size of the working set. The rest of the line shall be the pages requested. Given a data file with the single line below: 3 5 2 2 0 4 6 4 4 1 3 4 5 0 4 5 6 2 0 0 1 This will be parsed as: Page Table Size = 3 Page Reference String = 5 2 2 0 4 6 4 4 1 3 4 5 0 4 5 6 2 0 0 1 1.3 There shall be no limit to the number of lines in the input data file.

The datafile.txt should contain: 3 1 2 3 4 5 6 7 8 9 8 7 6 5 4 3 2 1 2 3 4 56 7
4 2 3 5 6 8 9 7 5 3 1 3 6 3 2 5 2 8 5 4 3 1 1

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <bits/stdc++.h>
# define HG 2
using namespace std;

bool search(int key, vector<int>& fr)
{
for (int i = 0; i < fr.size(); i++)
if (fr[i] == key)
return true;
return false;
}

// Function to find the frame that will not be used
// recently in future after given index in pg[0..pn-1]
int predict(vector<int> pg, vector<int>& fr, int pn, int index)
{
// Store the index of pages which are going
// to be used recently in future
int res = -1, farthest = index;
for (int i = 0; i < fr.size(); i++) {
int j;
for (j = index; j < pn; j++) {
if (fr[i] == pg[j]) {
if (j > farthest) {
farthest = j;
res = i;
}
break;
}
}

// If a page is never referenced in future,
// return it.
if (j == pn)
return i;
}

// If all of the frames were not in future,
// return any of them, we return 0. Otherwise
// we return res.
return (res == -1) ? 0 : res;
}

void optimalPage(vector<int> pg, int pn, int fn)
{
// Create an array for given number of
// frames and initialize it as empty.
vector<int> fr;

// Traverse through page reference array
// and check for miss and hit.
int hit = 0;
for (int i = 0; i < pn; i++) {

// Page found in a frame : HIT
if (search(pg[i], fr)) {
hit++;
continue;
}

// Page not found in a frame : MISS

// If there is space available in frames.
if (fr.size() < fn)
fr.push_back(pg[i]);

// Find the page to be replaced.
else {
int j = predict(pg, fr, pn, i + 1);
fr[j] = pg[i];
}
}
cout << "No. of hits = " << hit << endl;
cout << "No. of misses = " << pn - hit << endl;
}

int Fifo(vector<int> pages, int n, int capacity)
{
// To represent set of current pages. We use
// an unordered_set so that we quickly check
// if a page is present in set or not
unordered_set<int> s;

// To store the pages in FIFO manner
queue<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())
{
// Insert the current page into the set
s.insert(pages[i]);

// increment page fault
page_faults++;

// Push the current page into the queue
indexes.push(pages[i]);
}
}

// If the set is full then need to perform FIFO
// i.e. remove the first page of the queue from
// set and queue both and insert the current page
else
{
// Check if current page is not already
// present in the set
if (s.find(pages[i]) == s.end())
{
// Store the first page in the
// queue to be used to find and
// erase the page from the set
int val = indexes.front();

// Pop the first page from the queue
indexes.pop();

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

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

// push the current page into
// the queue
indexes.push(pages[i]);

// Increment page faults
page_faults++;
}
}
}

return page_faults;
}

int Lru(vector<int> pages, int n, int capacity)
{
// To represent set of current pages. We use
// an unordered_set so that we quickly check
// if a page is present in set or not
unordered_set<int> s;

// To store least recently used indexes
// of pages.
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;
}

int main(int argc, char** argv)
{
   ifstream file;
   string line , filename = argv[1];
   file.open(filename.c_str());
   while(getline(file , line)){
       int capacity = (int)(line[0]-'0') , n = line.size();
       vector<int> pages;
       for(int i = 2 ; i<n ; i+=2)
           pages.push_back((int)(line[i]-'0'));
       cout<<capacity<<endl;
       for(int i = 0 ; i < pages.size() ; ++i)
           cout<<pages[i]<<" ";
       cout<<endl;
       cout<<Fifo(pages , n , capacity)<<endl;
       optimalPage(pages , n , capacity);
       cout<<endl;
       cout<<Lru(pages , n , capacity)<<endl;
   }
   return 0;
}

Add a comment
Know the answer?
Add Answer to:
Write a program that implements the FIFO, Optimal, MFU, and LRU page-replacement algorithms. Given a page-reference...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT