"Optimal Page Replacement Algorithm" Code Answer's

You're definitely familiar with the best coding language C that developers use to develop their projects and they get all their queries like "Optimal Page Replacement Algorithm" answered properly. Developers are finding an appropriate answer about Optimal Page Replacement Algorithm related to the C coding language. By visiting this online portal developers get answers concerning C codes question like Optimal Page Replacement Algorithm. Enter your desired code related query in the search bar and get every piece of information about C code related question on Optimal Page Replacement Algorithm. 

Optimal Page Replacement Algorithm

By Cautious CapuchinCautious Capuchin on Jun 16, 2021
#include <bits/stdc++.h>
using namespace std;
int predict(int page[], 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] == page[j]) {
            if (j > farthest) {
               farthest = j;
               res = i;
            }
            break;
         }
      }
      // Return the page which are
      // are never referenced in future,
      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;
}
bool search(int key, vector<int>& fr) {
   for (int i = 0; i < fr.size(); i++)
   if (fr[i] == key)
   return true;
   return false;
}
void opr(int page[], int pn, int fn) {
   vector<int> fr;
   int hit = 0;
   for (int i = 0; i < pn; i++) {
      // Page found in a frame : HIT
      if (search(page[i], fr)) {
         hit++;
         continue;
      }
      //If a page not found in a frame : MISS  
      // check if there is space available in frames.
      if (fr.size() < fn)
      fr.push_back(page[i]);
      // Find the page to be replaced.
      else {
         int j = predict(page, fr, pn, i + 1);
         fr[j] = page[i];
      }
   }
   cout << "Hits = " << hit << endl;
   cout << "Misses = " << pn - hit << endl;
}
// main Function
int main() {
   int page[] = { 1, 7, 8, 3, 0, 2, 0, 3, 5, 4, 0, 6, 1 };
   int pn = sizeof(page) / sizeof(page[0]);
   int fn = 3;
   opr(page, pn, fn);
   return 0;
}

Source: www.tutorialspoint.com

Add Comment

0

Optimal Page Replacement Algorithm

By Crazy CottonmouthCrazy Cottonmouth on May 02, 2021
Input : Number of frames, fn = 3
        Reference String, pg[] = {1,2,3,4,2,1,5,6,2,1,2,3,7,6,3,2,1,2,3,6};
Output : No. of hits = 11 
         No. of misses = 9

Input : Number of frames, fn = 4 
        Reference String, pg[] = {7, 0, 1, 2, 
                  0, 3, 0, 4, 2, 3, 0, 3, 2};
Output : No. of hits = 7
         No. of misses = 6

Source: www.geeksforgeeks.org

Add Comment

0

All those coders who are working on the C based application and are stuck on Optimal Page Replacement Algorithm can get a collection of related answers to their query. Programmers need to enter their query on Optimal Page Replacement Algorithm related to C code and they'll get their ambiguities clear immediately. On our webpage, there are tutorials about Optimal Page Replacement Algorithm for the programmers working on C code while coding their module. Coders are also allowed to rectify already present answers of Optimal Page Replacement Algorithm while working on the C language code. Developers can add up suggestions if they deem fit any other answer relating to "Optimal Page Replacement Algorithm". Visit this developer's friendly online web community, CodeProZone, and get your queries like Optimal Page Replacement Algorithm resolved professionally and stay updated to the latest C updates. 

C answers related to "Optimal Page Replacement Algorithm"

View All C queries

C queries related to "Optimal Page Replacement Algorithm"

Browse Other Code Languages

CodeProZone