- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Page Faults in LFU
The Least Frequently Use aka LFU is a concept of page memory management, can also be used as a replacement algorithm. This process take a lead we the particular page needs a replacement when a new page is on the way by the process. LFU is one of the page replacement policy where an user can replace the least frequency of a particular operation page. If the page frequency is same in a process, then it will come first on the replacement list.
Here we will take a page sequence of an array of pages denoted as pages[], whose length is N with a memory capacity of C. We need to evaluate the number of occurrence as page faults by imposing the algorithm mentioned below.
Input for the LFU algorithm −
N = 7, C = 3
The pages taken for the process = { 1, 2, 3, 4, 2, 1, 5 }
Output For The Process Is −
Faults Occurrence In This Process = 6
Hits Needed For This Process = 1
Here the total capability of this particular process is here 3, which can save the max to max 3 pages in that allotted time in a memory location.
Page 1 - It is required to run the process.
As page 1 is not exist in the list,
So, we can consider it as a page fault occurence: The value of page fault is = 1
Page 2 - It is needed for to run the process.
Since page 2 is not exist in the list,
So, we can consider it as a page fault occurence: The value of page fault is = 1 + 1 = 2
Page 3 - It is required to run the process.
Since page 3 is not exist in the list,
So, we can consider it as a page fault occurence: The value of page fault is =2 + 1 = 3
Page 4 - It is required to run the process.
Since page 4 is not exist in the list,
it replaces LFU page 1 : page fault = 3 + 1 = 4
Page 2 - It is required to run the process.
Since page 2 is not exist in the list,
So, we can not consider it as a page fault occurence: The value of page fault is = = 4 + 0 = 4
Continue the process....
Algorithm of the Page Faults in LFU
The LFU algorithm is a replacement process mentioned here in the field of operating system. This process always least frequently used pages present in the process.
Step 1 − Initialize the process of LFU operation.
Step 2 − Declare and mention the total count as 0.
Step 3 − Create a vector class.
Step 4 − Construct and declare an array with the desired array size.
Step 5 − Start the process with the size of memory capacity.
Step 6 − Create a map.
Step 7 − Store the frequency value to the map of the pages.
Step 8 − Traverse page elements.
Step 9 − If; the desired element is available in that particular location in the memory, then erase and push it for next level.
Step 10 − Step 9 increase the process frequency.
Step 11 − Else; the main memory totally occupied; remove the initial element and decrease the possible frequency of the data.
Step 12 − Count increase.
Step 13 − Compare the frequency result.
Step 14 − Sort the pages as per their frequency and time based result.
Step 15 − If we get the same frequency, then the page will arrive at first.
Step 16 − Repeat the process.
Step 17 − Return the result.
Step 18 − Terminate the process
Syntax for the Page Faults in LFU
int main() { int n,o,m,a,i,r,j,p,k,w; cout <<" DECLARE THE FRAMES WE WANT TO TAKE \n"; cin >> o; cout <<"PROCESS OF THE DECLARATION \n"; cin>>m; vector<int> p(m); cout<<"ENTER THE PROCESSES\n"; for(r=0;r<a;r++){ Cin >> p[r]; } @Declare a core class of vector with a map frame Vector <vector<int>> b(o,vector<int>(a,-1)); map <int1, int2> mp,lfmj; for(r=0;r<a;r++){ Vector <int val> op; Vector <pair<int1,int2>> d, lf_val; for(auto q: mp){ c.push_back({q.second,q.first}); } for(auto q_1:lfmj){ lf.push_back_val ({q.second_val,q.first_val}); } sort(lf.begin_ val(),lf.end_ val()); bool dontCall_val = true_val; if(lf.size_val() > 2){ if(lf[0].first_val!=lf[1].first_val){ dontCall_val_occ = as_false; } } @Declare the process sorting method in this process Sort_val (c.begin_val(),c.end_val()); bool hasrun_val = false_number; for(p=0;p<q;p++){ if(a[p][r]==p[r]){ mp[p[r]]++; lfmp_val [p[r]]++; hasrun_val = true; break; } if(a[p][i]==-1){ for(w=i;w<m;w++) a[p][k]=p[r]; mp[p[r]]++; lfmp[p[r]]++; hasrun_val =true; break; } } @Declare the true and flase notation if(p==o||hasrun_value == false){ for(p=0;p<n;p++){ if(dontCall==true){ int q; if(lf[lf.size_val()-1].second==c[c.size_val()- 1].second&&lf[lf.size_val()-1].first_val>1){ if(a[p][r]==c[c.size()-2].second_val){ mp.erase(a[p][r]); lfmp.erase(a[p][r]); for(w=i;w<m;w++) a[p][w]=p[i]; mp_val[p[i]]++; lfmp_val[p[i]]++; break; } } else{ if(a[p][r]==c[c.size_val()-1].second){ mp.erase_val(a[p][r]); lfmp.erase_val(a[p][r]); for(w=r;w<a;w++) a[r][w]=p[r]; mp_val [p[r]]++; lfmp_val [p[r]]++; break; } } } else If (dontCall_value == as_false){ if(a[p][r]==lf[0].second_value){ mp.erase_val (a[p][r]); lfmp.erase_val (a[p][r]); for(w=r;w<a;w++) a[p][w]=p[r]; mp_val [p[r]]++; lfmp_val [p[r]]++; break; } } } } for(auto_val q:mp){ if(q.first!=p[r]){ mp_value [q.first_val]++; } } } @Declaere A value for theVector Class Here For The Further Process int hit_val = 0; vector_val <int> hitv_val(a); for(r=1;r<a;r++){ for(p=0;p<o;p++){ if(p[r]==a[p][r-1]){ Hit_val ++; Hitv_value [r]=1; break; } } } cout<<"Process the value of page fault "; for(r=0;r<a;r++){ cout<<j[r]<<" "; } cout<<'THE VALUE \n'; for(r=0;r<n;r++){ cout<<"Frame value for the process"<<r<<" "; for(p=0;p<m;p++){ if(a[r][p]==-1) cout<<"E IS THE VALUE "; else cout<<a[r][p]<<" "; } cout<<'\n VALUE'; } @Count The Process Of HIT count cout<<"HIT VALUE "; for(r=0;r<hitv.size _ VAL();r++){ if(hitv[r]==0) cout<<" "; else cout<<hitv_val[r]<<" "; } cout<<"\n_VAL"; cout<<"Hit "<<hit<<'\n'<<"GET THE PAGE FAULT RESULT "<<m-hit<<'\n'; return 0; }
In this possible syntax mentioned above, we have tried to show you the possible implementation of a LFU page fault management in the field of operating system. With this intersected syntax we are going to build some C++ codes to explain and solve the problem statement in an efficient manner.
Approach
Approach 1 − C++ program to demonstrate the faults in a page as occurrence in LFU - Implementation attached with Operating System.
Approach 2 − C++ program to demonstrate the LFU page replacement algorithm that is in that use as paging for virtual memory management.
C++ program to demonstrate the faults in a page as occurrence in LFU - Implementation attached with Operating System
In this approach we are going to demonstrate the page fault occurrence in LFU – Implementation by using the steps below –
Declare the input output stream.
Declare function to count the number of page faults.
Initialise the count to zero.
Store the elements in the memory.
Store the frequency pages.
Sech for the elements present in the memory or not.
Remove the first element if it is present with the use of least frequently.
Increse frequency and count.
Example Code 1
//C++ program to demonstrate the page faults occurrence in LFU - Implementation attached with Operating System #include <bits/stdc++.h> using namespace std; //Declare the number of page faults as a value of count related to the OS int pageFaults_val (int a, int r, int pages[]) { //Initialise the parameter and set the page count to 0 as start int count_val = 0; //Try to save the data as values to the main memory location where the volume of the memory is c vector<int> v; //Set a minimum value for the data to save the value of frequency of the faulty pages unordered_map <int, int> mp; //Declare the unordered data set int t; for (t = 0; t <= a - 1; t++) //Declare a loop method to iterate the process { //Run a search method for an element; is it present in memory location or not auto it = find(v.begin(), v.end(), pages[t]); //Declare the page begining and end notation //If element is there in the data source or memory if (it == v.end()) { //The process came to an end //If memory is full, decrease the frequency if (v.size() == a) { mp[v[0]]--; //Remove the first element as by using the least frequently here v.erase(v.begin()); } //Add some new elements here at the end of the memory node v.push_back(pages[t]); //Increase the frequency for the pages mp[pages[t]]++; //Increse the page count and declare the iteration for the process count_val++; } else { //If the element is present in the list, remove the elements one by one //And add the elements at the end of the node to increase its frequency value mp[pages[t]]++; //Page iteration v.erase(it); //Erase notation v.push_back(pages[t]); //Declare the push back } //Compare the frequency with all other pages which will start from the 2nd last page of the node int k = v.size() - 2; //Start the sorting process based on their frequency consumed time at which they arrived and stopped //If the frequency value is same for those elements and processes; then the first page must be given a place at the first position while (mp[v[k]] > mp[v[k + 1]] && k > -1) { swap(v[k + 1], v[k]); k--; } } //Get return value of the total faults for a page in the LFU process return count_val; //Return the count value } //Declare the driver program to test the page faults and hits occurence here int main() { int pages[] = { 1, 2, 3, 4, 2, 1, 5, 7, 16}; int n = 7, c = 3; cout << "Page Faults occured here in LFU= " << pageFaults_val(n, c, pages) //Page Fault Result << endl; cout << "Page Hits occured here in LFU= " << n - pageFaults_val(n, c, pages); //Page Hits Result return 0; }
Output
Page Faults occured here in LFU= 5 Page Hits occured here in LFU= 2
C++ program to demonstrate the LFU page replacement algorithm that is in that use as paging for virtual memory management
By following the above mentioned algorithm and syntax we are going to decode the process of LFU and find out the page faults that can be used as paging for virtual memory management.
Example Code 2
//C++ program to demonstrate the LFU page replacement algorithm that is in those use as paging value for virtual storage management #include<bits/stdc++.h> using namespace std; int main(){ int n,m,i,j,k; //Declare the integer value cout<<"Enter number of frames releted to the process\n"; //Get The Value Of Frames cin>> n; cout<<"Enter number of processes present here for the process\n"; //Declare the process value cin>>m; vector<int> p(m); cout<<"Enter processes we want to perform\n"; //Desired process value for(i=0;i<m;i++) //Declare the process loop { cin>>p[i]; } vector<vector<int>> a(n,vector<int>(m,-1)); map <int, int> mp,lfmp; for(i=0;i<m;i++) //Declare the process loop { vector<int> op; //Declare the vector class vector<pair<int,int>> c,lf; //Go for the pair value for(auto q: mp) { c.push_back({q.second,q.first}); } for(auto q:lfmp){ lf.push_back({q.second,q.first}); } sort(lf.begin(),lf.end()); bool dontCall=true; if(lf.size()>2){ if(lf[0].first!=lf[1].first){ dontCall=false; } } sort(c.begin(),c.end()); bool hasrun=false; for(j=0;j<n;j++) //Declare the process loop { if(a[j][i]==p[i]){ mp[p[i]]++; lfmp[p[i]]++; hasrun=true; break; } if(a[j][i]==-1){ for(k=i;k<m;k++) a[j][k]=p[i]; mp[p[i]]++; lfmp[p[i]]++; hasrun=true; break; } } if(j==n||hasrun==false){ for(j=0;j<n;j++){ if(dontCall==true){ int q; if(lf[lf.size()-1].second==c[c.size()-1].second&&lf[lf.size()-1].first>1){ if(a[j][i]==c[c.size()-2].second){ mp.erase(a[j][i]); lfmp.erase(a[j][i]); for(k=i;k<m;k++) a[j][k]=p[i]; mp[p[i]]++; lfmp[p[i]]++; break; //Break the value } } else{ if(a[j][i]==c[c.size()-1].second){ mp.erase(a[j][i]); lfmp.erase(a[j][i]); for(k=i;k<m;k++) a[j][k]=p[i]; mp[p[i]]++; lfmp[p[i]]++; break; } } } else if(dontCall==false){ if(a[j][i]==lf[0].second){ mp.erase(a[j][i]); lfmp.erase(a[j][i]); for(k=i;k<m;k++) a[j][k]=p[i]; mp[p[i]]++; lfmp[p[i]]++; break; } } } } for(auto q:mp){ if(q.first!=p[i]){ mp[q.first]++; } } } int hit=0; vector<int> hitv(m); for(i=1;i<m;i++){ for(j=0;j<n;j++){ if(p[i]==a[j][i-1]){ hit++; hitv[i]=1; break; } } } cout<<"Process are here "; for(i=0;i<m;i++){ cout<<p[i]<<" "; } cout<<'\n'; for(i=0;i<n;i++){ cout<<"Frame count"<<i<<" "; for(j=0;j<m;j++){ if(a[i][j]==-1) cout<<"E "; else cout<<a[i][j]<<" "; } cout<<'\n'; } cout<<"HIT "; for(i=0;i<hitv.size();i++){ if(hitv[i]==0) cout<<" "; else cout<<hitv[i]<<" "; } cout<<"\n"; cout<<"Hit "<<hit<<'\n'<<"Page Fault Counts Are Here"<<m-hit<<'\n'; return 0; }
Output
Enter number of frames releted to the process Enter number of processes present here for the process Enter processes we want to perform Process are here HIT Hit 0 Page Fault Counts Are Here0
Conclusion
In the field of operating system the paging used for the virtual primary data source management, runs as an algorithm for page replacement. It decides which memory page to run a function for page out also known as the swaping out process. In this article today we have learned about the least frequency used aka LFU process and its algorithm and, syntax; by which we have tried to solve the problem statement by explain the process in an efficient manner.