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

Develop an algorithm and implement a Preemptive Priority scheduling algorithm using C++ and using bubble sorting...
Develop an algorithm and implement a Preemptive Priority scheduling algorithm using C++ and using bubble sorting .Arrival time, burst time and priority values.The code should also display: (i) Gantt chart and determine the following: (ii) Determine the Turnaround time(TAT), waiting time(WT) of each process (iii) Determine the Average Waiting Time (AWT) and Average Turnaround Time (ATAT) of all processes. please write the comments
Develop an algorithm and implement Multilevel Queuing scheduling using C++.and using bubble sorting. Consider a set...
Develop an algorithm and implement Multilevel Queuing scheduling using C++.and using bubble sorting. Consider a set of user and system processes and determine (i) turnaround time (ii) waiting time of each process (iii) Average Waiting Time (AWT) and (iv) Average Turnaround Time (ATAT) of all processes. please write comment
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
In this lab, you will implement Heap Sort algorithm in C++ and Report the number of...
In this lab, you will implement Heap Sort algorithm in C++ and Report the number of steps and the CPU running time in a table, please show the code and output Approximation the constant c in the complexity of heap sort (cnlgn) by inspecting the results For each algorithm, and for each n = 100, 200, 300, 400, 500, 1000, 4000, 10000, measure its running time and number of steps when the input is (1) already sort, i.e. n, n-1,...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT