Question

Need this in C++. Thanks. OBJECTIVE The purpose of this programming project is to explore page...

Need this in C++. Thanks.

OBJECTIVE

The purpose of this programming project is to explore page replacement algorithms. This can be achieved by implementing the LRU and Optimal algorithms as described in our textbook and discussed in class.

This project is worth 100 points.

PROJECT SPECIFICATIONS

Review the LRU and Optimal algorithms discussed in class and Chapter 9 from the book. Your program must be named "paging" and will read a list of page numbers from a file. The name of the file will be passed to your program as a command line argument. The example input file contains three lists of page numbers:

2, 6, 9, 2, 4, 2, 1, 7, 3, 0, 5, 2, 1, 2, 9, 5, 7, 3, 8, 5

0, 6, 3, 0, 2, 6, 3, 5, 2, 4, 1, 3, 0, 6, 1, 4, 2, 3, 5, 7

                        3, 1, 4, 2, 5, 4, 1, 3, 5, 2, 0, 1, 1, 0, 2, 3, 4, 5, 0, 1

Your program will output the page number in each frame for each page fault with the given frames by following in a standardized format.

  1. Echo the list of page numbers.
  2. Echo the algorithm used (LRU or Optimal).
  3. Echo the total given frames.
  4. For each page fault, output the page number in each frame
  5. Output the total number of page faults.

Here is how the program should be run (3 is the number of frames).

% paging input.txt LRU 3

Input page numbers: 2,6,9,2,4,2,1,7,3,0,5,2,1,2,9,5,7,3,8,5

Page replace algorithm: LRU

Given frame number: 3

Processing page 2, page fault 1: 2, -1, -1

Processing page 6, page fault 2: 2, 6, -1

Processing page 9, page fault 3: 2, 6, 9

Processing page 2, no page fault

                 .

                 .

                 .

Total page faults is: 17                

Sample input is provided however you should test your program with different list of page numbers. Output may be different if you use stack.

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

#include<iostream>
#include<bits/stdc++.h>
#include <fstream>
using namespace std;


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

int predict(int pages[], vector<int>& vec, int n, int index)
{
    int result = -1, farthest = index;
    for (int i = 0; i < vec.size(); i++) {
        int j;
        for (j = index; j < n; j++) {
            if (vec[i] == pages[j]) {
                if (j > farthest) {
                    farthest = j;
                    result = i;
                }
                break;
            }
        }

        if (j == n)
            return i;
    }
    return (result == -1) ? 0 : result;
}

int optimalPage(int pages[], int n, int capacity)
{
       cout<<"Given frame number: "<<capacity<<endl;
    vector<int> vec;
       vector<int>::iterator it;
       int page_faults=0;
    for (int i = 0; i < n; i++)
       {
               cout<<"Processing page "<<pages[i]<<",";
        if (search(pages[i], vec)) {
                       cout<<"no page fault: "<<endl;
            continue;
        }
        if (vec.size() < capacity)
        {
                   vec.push_back(pages[i]);
                   page_faults++;
                   cout<<"page fault "<<page_faults<<": ";
               }
        else
               {
            int j = predict(pages, vec, n, i + 1);
            vec[j] = pages[i];
                       page_faults++;
                       cout<<"page fault "<<page_faults<<": ";
        }
        for(int j=0; j<capacity; j++)
           {
               cout<<vec[j]<<", ";
           }
           cout<<endl;
    }
       return page_faults;

}

int modstack(int pages[], int n, int capacity)
{
   cout<<"Given frame number: "<<capacity<<endl;
   vector<int> vec;
   vector<int>::iterator it;
   int page_faults=0;
   for(int i=0;i<n;i++)
   {
       cout<<"Processing page "<<pages[i]<<",";
       if(vec.capacity()<capacity)
       {
           if(find(vec.begin(),vec.end(),pages[i])==vec.end())
           {
               vec.insert(vec.begin(),pages[i]);
               page_faults++;
               cout<<"page fault "<<page_faults<<": ";
           }
           else
           {
               vec.erase(find(vec.begin(),vec.end(),pages[i]));
               vec.insert(vec.begin(),pages[i]);
               cout<<"no page fault: "<<endl;
        continue;
           }
       }
       else
       {
           if(find(vec.begin(),vec.end(),pages[i])==vec.end())
           {
               vec.pop_back();
               vec.insert(vec.begin(),pages[i]);
               page_faults++;
               cout<<"page fault "<<page_faults<<": ";
           }
           else
           {
               vec.erase(find(vec.begin(),vec.end(),pages[i]));
               vec.insert(vec.begin(),pages[i]);
               cout<<"no page fault: "<<endl;
        continue;
           }
       }
       for(it=vec.begin();it!=vec.end();++it)
       {
           cout<<*it<<",";
       }
       cout<<endl;
   }
   return page_faults;
}


void getArray(string str)
{
    int l = str.length();
    int arr[l];
    for(int i=0;i<l;i++) arr[i]=0;
    int j = 0, i;
    l=0;
    for (i = 0; str[i] != '\0'; i++)
       {
           if (str[i] == ',') continue;
      else if (str[i] == ' ') continue;
      else {arr[l] = arr[l] * 10 + (str[i] - 48); l++;}
    }
       int capacity, n, page_faults,page_faults1;
       cout<<"Page replace algorithm: LRU"<<endl;
       capacity = 3;
       page_faults = modstack(arr,l,capacity);
       cout<<"Total page faults is:"<<page_faults<<endl;
    cout<<"\n\n";
       cout<<"Page replace algorithm: Optimal"<<endl;
       capacity = 3;
       page_faults1 = optimalPage(arr,l,capacity);
       cout<<"Total page faults is:"<<page_faults1<<"\n"<<endl;
}

int main()
{
   string pages[3],line;
int count=0;
   ifstream inFile ("paging.txt");
   if (inFile.is_open())
   {
       while (! inFile.eof() )
       {
           getline (inFile,line);

           if(count<3)
      {
        cout<<"Input page numbers: "<<line<<endl;
        getArray(line);
        count++;
      }
       }
       inFile.close();
   }

   else
   {
           cout << "Unable to open file";
           exit(1);
   }
}

Add a comment
Know the answer?
Add Answer to:
Need this in C++. Thanks. OBJECTIVE The purpose of this programming project is to explore page...
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
  • This is JAVA programing problem. Please give me all necessary screenshot and comments. (I use Ecl...

    This is JAVA programing problem. Please give me all necessary screenshot and comments. (I use Eclipse) Thanks Write a program that implements the FIFO, LRU, and Optimal page replacement algorithms presented in chapter 8 of your text. First generate a random page-reference string (this should be 20 entries long) where page numbers range from 0 to 9. Apply the random page-reference string to each algorithm, and record the number of page faults incurred by each algorithm. Implement the replacement algorithms...

  • Java Looking for some help on getting this started. Directions below. Operating systems designers have long...

    Java Looking for some help on getting this started. Directions below. Operating systems designers have long used simulation as an inexpensive way to test new algorithms. In this assignment you write a Java program that prompts the user for simulation data then runs a selected page replacement algorithm on that system, reporting frame assignments and total page faults. Instructions Your program will prompt the user first for the number of frames to be used in the simulation, then for a...

  • 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:...

  • Question 7 30 pts Consider the following page reference string: {1,2,3,4,1,5,6,2,1,2,3,7,6,3} Assume that the system has...

    Question 7 30 pts Consider the following page reference string: {1,2,3,4,1,5,6,2,1,2,3,7,6,3} Assume that the system has 4 page frames allocated to these 7 pages. Follow the page placement and replacement using the following three replacement algorithms: • LRU replacement • FIFO replacement • Optimal replacement How many page faults will occur for these three algorithms? Assume that all frames are initially empty, so your first unique pages will all cost one fault each USE ENCLOSED TABLES! Show all calculations in...

  • Consider the following virtual page reference sequence: page 1, 2, 3, 4, 2, 1, 5, 6,...

    Consider the following virtual page reference sequence: page 1, 2, 3, 4, 2, 1, 5, 6, 2, 1, 2, 3. This indicates that these particular pages need to be accessed by the computer in the order shown. Consider each of the following 4 algorithm-frame combinations: LRU with 3 frames FIFO with 3 frames LRU with 4 frames FIFO with 4 frames . For each of the 4 combinations, below, move from left to right as the virtual page numbers are...

  • Consider the following page reference string for a specific process: 7, 2, 3, 1, 2, 5,...

    Consider the following page reference string for a specific process: 7, 2, 3, 1, 2, 5, 3, 4, 6, 7, 7, 1, 0, 5, 4, 6, 2, 3, 0, 1 Assuming demand paging with 3 frames, determine the page fault rate for each of the following page replacement algorithms: a. LRU replacement b. FIFO replacement c. Optimal replacement

  • Consider the following page reference string: 1, 0, 3, 2, 6, 4, 5, 0, 1, 7,...

    Consider the following page reference string: 1, 0, 3, 2, 6, 4, 5, 0, 1, 7, 7, 6, 4, 3, 5, 2, 1, 3, 2, 7 Assuming demand paging with three frames, how many page faults would occur for the following replacement algorithms? Show your work. (a) LRU replacement (b) FIFO replacement (c) Optimal replacement

  • 9.3 (3 points for correct answer, 7 points for steps) Consider the following page reference string:...

    9.3 (3 points for correct answer, 7 points for steps) Consider the following page reference string: 6, 1, 5, 5, 4, 6, 2, 6, 0 , 1, 7, 2, 3, 1, 4, 6, 7, 2, 5, 2,. Assuming demand paging with three frames, how many page faults would occur for the following replacement algorithms? • LRU replacement • FIFO replacement • Optimal replacement

  • Consider the following page reference string: 5, 3,7,5, 3, 6, 1, 0,4,6,2,0,1,2,7,3,1,4,7,2 Assuming demand paging with...

    Consider the following page reference string: 5, 3,7,5, 3, 6, 1, 0,4,6,2,0,1,2,7,3,1,4,7,2 Assuming demand paging with three frames, how many page faults would occur for the following replacement algorithms!? LRU replacement FIFO replacement Optimal replacement

  • Assume demand paging with three frames. Consider the following page reference string: 7, 2, 3, 1,...

    Assume demand paging with three frames. Consider the following page reference string: 7, 2, 3, 1, 2, 5, 3, 4, 6, 7, 7, 1 If OPT replacement is used, the number of page faults will be a. 8 b. 9 c. 10 d. 7 e.None of the above If LRU replacement is used, the number of page fault will be a. 8 b. 9 c. 10 d. 11 e. None of the above

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