LFU 中的頁面錯誤
最不常用 (LFU) 是頁面記憶體管理的概念,也可以用作替換演算法。當新頁面即將由程序到達時,此過程會引導我們進行特定頁面的替換。LFU 是一種頁面替換策略,使用者可以使用它來替換特定操作頁面的最低頻率。如果頁面頻率在程序中相同,則它將首先出現在替換列表中。
在這裡,我們將採用頁面序列,該序列是一個表示為 pages[] 的頁面陣列,其長度為 N,記憶體容量為 C。我們需要透過實施下面提到的演算法來評估頁面錯誤的出現次數。
LFU 演算法的輸入:
N = 7, C = 3
程序使用的頁面 = {1, 2, 3, 4, 2, 1, 5}
程序的輸出:
此程序中發生的錯誤 = 6
此程序所需的命中 = 1
此特定程序的總容量為 3,在分配的時間內,它最多可以在記憶體位置中儲存 3 個頁面。
頁面 1 - 需要執行該程序。
由於列表中不存在頁面 1,
因此,我們可以將其視為頁面錯誤的發生:頁面錯誤的值 = 1
頁面 2 - 需要執行該程序。
由於列表中不存在頁面 2,
因此,我們可以將其視為頁面錯誤的發生:頁面錯誤的值 = 1 + 1 = 2
頁面 3 - 需要執行該程序。
由於列表中不存在頁面 3,
因此,我們可以將其視為頁面錯誤的發生:頁面錯誤的值 = 2 + 1 = 3
頁面 4 - 需要執行該程序。
由於列表中不存在頁面 4,
它將替換 LFU 頁面 1:頁面錯誤 = 3 + 1 = 4
頁面 2 - 需要執行該程序。
由於列表中不存在頁面 2,
因此,我們不能將其視為頁面錯誤的發生:頁面錯誤的值 = 4 + 0 = 4
繼續該過程……
LFU 中頁面錯誤的演算法
LFU 演算法是此處在作業系統領域提到的替換過程。此過程始終使用程序中存在的最低頻率頁面。
步驟 1 - 初始化 LFU 操作的過程。
步驟 2 - 宣告並提及總計數為 0。
步驟 3 - 建立向量類。
步驟 4 - 構造並宣告一個具有所需陣列大小的陣列。
步驟 5 - 以記憶體容量大小開始該過程。
步驟 6 - 建立一個對映。
步驟 7 - 將頻率值儲存到頁面的對映中。
步驟 8 - 遍歷頁面元素。
步驟 9 - 如果;所需元素在記憶體中的特定位置可用,則將其擦除並將其推送到下一級。
步驟 10 - 步驟 9 增加程序頻率。
步驟 11 - 否則;主記憶體完全佔用;刪除初始元素並降低資料的可能頻率。
步驟 12 - 計數增加。
步驟 13 - 比較頻率結果。
步驟 14 - 根據其頻率和基於時間的進行排序。
步驟 15 - 如果我們得到相同的頻率,則頁面將首先到達。
步驟 16 - 重複該過程。
步驟 17 - 返回結果。
步驟 18 - 終止該過程
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;
}
在上面提到的這種可能的語法中,我們試圖向您展示在作業系統領域實現 LFU 頁面錯誤管理的可能方法。透過這種交叉的語法,我們將構建一些 C++ 程式碼來解釋和解決問題陳述,以有效的方式。
方法
方法 1 - C++ 程式演示頁面錯誤在 LFU 中的發生 - 附帶作業系統的實現。
方法 2 - C++ 程式演示 LFU 頁面替換演算法,該演算法用作虛擬記憶體管理的分頁。
C++ 程式演示頁面錯誤在 LFU 中的發生 - 附帶作業系統的實現
在這種方法中,我們將使用以下步驟演示 LFU 中的頁面錯誤發生 - 實現:
宣告輸入輸出流。
宣告一個函式來計算頁面錯誤的數量。
將計數初始化為零。
將元素儲存在記憶體中。
儲存頻率頁面。
搜尋記憶體中是否存在元素。
如果使用最低頻率存在,則刪除第一個元素。
增加頻率和計數。
示例程式碼 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;
}
輸出
Page Faults occured here in LFU= 5 Page Hits occured here in LFU= 2
C++ 程式演示 LFU 頁面替換演算法,該演算法用作虛擬記憶體管理的分頁
透過遵循上述演算法和語法,我們將解碼 LFU 的過程並找出可用作虛擬記憶體管理分頁的頁面錯誤。
示例程式碼 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;
}
輸出
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
結論
在作業系統領域,用於虛擬主要資料來源管理的分頁作為頁面替換的演算法執行。它決定哪個記憶體頁面執行一個函式以進行頁面輸出,也稱為交換輸出過程。在今天的這篇文章中,我們學習了最不常用 (LFU) 過程及其演算法和語法;透過這些,我們試圖透過有效地解釋該過程來解決問題陳述。
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP