Question
HI, I was writing codes for calculation of the page faults when using LRU. My input file starts with the case number, number of frames,
HI, I was writing codes for calculation of the page faults when using LRU. My input file starts with the case number, number of frames, and the sequence of page request. Sample input file //input file Case 1, 3, 7,0,1,2,0,3,0,4,2,3,0,3,0,3,2,1,2,0,1,7,0,1 Case 2, 3, 7,0,1,2,0,3,0,4,2,3,0,3,0,3,2,1,2,0,1,7,0,0 Case 3, 3, 7,0,1,2,0,3,0,4,2,3,0,3,0,3,2,1,2,0,1,7,0,2
I made these codes in c++
//C++ implementation of above algorithm #include
// Function to find page faults using indexes int pageFaults(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
// To store least recently used indexes // of pages. unordered_map
// Start from initial page int page_faults = 0; for (int i=0; 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; } // Driver code int main() { int pages[] = {3, 7,0,1,2,0,3,0,4,2,3,0,3,0,3,2,1,2,0,1,7,0,0}; int n = sizeof(pages)/sizeof(pages[0]); int capacity = 4; cout << pageFaults(pages, n, capacity); return 0; } but instead of getting 14 faults I got 9 . Can you explain where is my mistake please?
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started