Question

In: Computer Science

Develop an algorithm and implement Optimal Page Replacement algorithm using C++. Determine the number of page...

Develop an algorithm and implement Optimal Page Replacement algorithm using C++. Determine the number of page faults and page hits by considering the Frame size=4, ReferenceString:2 4 6 7 8 2 4 9 13 9 2 7 2 6 1 4 9 2

Solutions

Expert Solution

  • Below is the detailed implementation of the above problem in C++ with code and output shown.
  • For better understanding please read the comments mentioned in the code.
  • In optimal page replacement algorithm we do following steps for every page in reference string:

If current page is present in frame then we increase page hit and move to next page in reference string,

Otherwise we insert that page in the frame if frame size is less than it's capacity, if not then find if any page in the frame is not referenced in future if so then replace that page with the current one, and if this also is not the case then find the farthest referenced page and replace that with current one.

This algorithm is not practical as you can see we are taking action on the basis of future and in real life we can't predict future.

  • CODE:

//include headers
#include <iostream>
#include <unordered_map>
#include <vector>
using namespace std;

//function to find page which is not referred in future or is referred farthest in future
int find(int reference_str[], vector<int>&frame, int tot, int index){
//to save index of resulting page
   int save=-1;
   //initialize farthest page to be index
   int farthest=index;
   //for all page in frame
   for(int i=0;i<frame.size();i++){
   //set found to false
   bool found=false;
   //for all pages in reference string starting from index
   for(int j=index;j<tot;j++){
//if current is matched then
           if(frame[i]==reference_str[j]){
//set found true
found=true;
//update farthest if possible
               if(j>farthest){
                   farthest=j;
                   save=i;
               }
               //and break
               break;
           }
       }
       //if the current page is not found then return index as it is not reference in future
       if (found==false)
           return i;
   }

   //if no farthest found return 0
   if(save==-1){
return 0;
   }
   //otherwise return farthest
   return save;
}

//function which uses optimal page replacement algorithm and prints page hits and page faults
void OptimalPageReplacement(int reference_str[], int tot, int frame_size){
//current frame
vector<int>frame;
//unordered map to store which pages are present in frame
unordered_map<int,bool>present;

//to store page hits
   int page_hits=0;
   //for all pages in reference string
   for(int i=0;i<tot;i++){
//if current page is in frame then it is a page hit
       if (present.find(reference_str[i])!=present.end()){
           page_hits++;
           continue;
       }
//otherwise
//if frame has some space then add this page to frame
       if(frame.size()<frame_size){
           frame.push_back(reference_str[i]);
           //and mark it in map also
           present[reference_str[i]]=true;
       }
       //otherwise
       else{
//find the farthest or not reference in future page
           int index=find(reference_str, frame, tot, i+1);
//erase this from map
           present.erase(frame[index]);
           //update frame
           frame[index] = reference_str[i];
           //mark this page in map
           present[frame[index]]=true;
       }
   }
   //print page hit and page faults
   cout<<"Number of page hits are: "<<page_hits<< endl;
   cout<<"Number of page faults: "<<tot-page_hits<< endl;
   return;
}

//driver function
int main(){
//reference string
   int reference_str[] = {2, 4, 6, 7, 8, 2, 4, 9, 13, 9, 2, 7, 2, 6, 1, 4, 9, 2};
   //size of reference string
   int sz=sizeof(reference_str)/sizeof(reference_str[0]);
   //frame size
   int frame_size=4;
   //call function
   OptimalPageReplacement(reference_str, sz, frame_size);
   return 0;
}

  • OUTPUT:

Number of page hits are: 8
Number of page faults: 10

  • Below are the screenshot attached for the code and output for better clarity and understanding.

CODE

OUTPUT

So if you still have any doubt regarding this solution please feel free to ask it in the comment section below and if it is helpful then please upvote this solution, THANK YOU.


Related Solutions

implementing linked list using c++ Develop an algorithm to implement an employee list with employee ID,...
implementing linked list using c++ Develop an algorithm to implement an employee list with employee ID, name, designation and department using linked list and perform the following operations on the list. Add employee details based on department Remove employee details based on ID if found, otherwise display appropriate message Display employee details Count the number of employees in each department
Implement a C++ program to implement the Banker’s algorithm for deadlock avoidance. Number of process 5,...
Implement a C++ program to implement the Banker’s algorithm for deadlock avoidance. Number of process 5, number of resources 3 and the number of instances of each given resource is in available. You should complete the functionalities for safe state check and resource request processing. To Do 1. Complete the definition of isSafe function. The function take, the process array, 1D array of available resources, 2D array storing current allocation, and 2D array of current need. The function does not...
Write algorithm for LIFO (Last In First Out) Page Replacement Algorithm
Write algorithm for LIFO (Last In First Out) Page Replacement Algorithm
develop an algorithm and then a C program that accomplishes the following. determines the minimum number...
develop an algorithm and then a C program that accomplishes the following. determines the minimum number of quarters, dimes, nickels, and pennies to make change for any amount of cents from 1 cent to 99 cents inclusive; produces an error message if 0 or more than 99 is entered as input, but the program will keep running and ask for another input; terminate if 0 or a negative number is entered. Here is possible example of the program running (remember...
develop an algorithm and then a C program that accomplishes the following. determines the minimum number...
develop an algorithm and then a C program that accomplishes the following. determines the minimum number of quarters, dimes, nickels, and pennies to make change for any amount of cents from 1 cent to 99 cents inclusive; produces an error message if 0 or more than 99 is entered as input, but the program will keep running and ask for another input; terminate if 0 or a negative number is entered. Here is possible example of the program running (remember...
Develop an algorithm to implement an employee list with employee ID ,name designation and department using...
Develop an algorithm to implement an employee list with employee ID ,name designation and department using link list and perform the following operation on the list i)add employee details based on department ii)remove employee details iv)count the number of employee in each department
Optimal and near-optimal process scheduling and page replacement algorithms. a) What is the theoretically best process...
Optimal and near-optimal process scheduling and page replacement algorithms. a) What is the theoretically best process scheduling algorithm? b) Do shortest job first and shortest remaining time first amount to the same thing or not? c) Can they be directly implemented? d) What do you consider the next best scheduling option, and how does it use the recent past to approximate the ideal case? e) Consider the analogous case in page replacement. What is the best possible choice of page...
(Write a C# program DO NOT USE CLASS)Implement the merge sort algorithm using a linked list...
(Write a C# program DO NOT USE CLASS)Implement the merge sort algorithm using a linked list instead of arrays. You can use any kind of a linked structure, such as single, double, circular lists, stacks and/or queues. You can populate your list from an explicitly defined array in your program. HINT: You will not be using low, middle and high anymore. For finding the middle point, traverse through the linked list while keeping count of the number of nodes. Break...
Write a program that implements the FIFO, LRU, and Optimal page replacement algorithms presented in chapter...
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 so that the number of page frames goes from 1 to 7 and you must compute the...
Using Java implement a searching algorithm to solve the following problem (please specify the searching algorithm...
Using Java implement a searching algorithm to solve the following problem (please specify the searching algorithm being used) Requirements Choose one problem with an algorithm and implement it. You should show and explain the result whatever you got. I recommend using N-Queen problem (at least N=8 or more) or any simple perfect games. For example, - N-Queen problem with hill climbing - N-Queen problem with simulated annealing - N-Queen problem with genetic algorithm - Tic-Tac-Toe with Minimax
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT