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