LRU 中的页面错误
分页是与操作系统相关的内存管理过程。它使用页面段将某些进程数据从辅助数据存储存储或检索到主数据存储或内存中。当进程在页面中遇到任何错误并且我们无法使用新的空闲页面来满足此处分配过程时,就会发生分页过程。LRU 过程产生了替换算法的特定需求。它决定了当进程生成新页面时需要替换哪个页面。让我们举个例子 -
进程的输入 -
N = 9, C = 4
进程的当前页面 = {5, 0, 1, 3, 2, 4, 1, 0, 5}
输出为:8
解释 -
分配给进程的内存为 4 个页面:5、0、1、3
在此过程中发生的错误 = 4
需要分配值为 2 的内存,替换 LRU 5
在此过程中发生的错误 = 4 + 1 = 5
需要分配值为 4 的内存,替换 LRU 0
在此过程中发生的错误 = 5 + 1 = 6
需要分配值为 1 的内存,该内存已存在
在此过程中发生的错误 = 6 + 0 = 6
需要分配值为 0 的内存,替换 LRU 3
在此过程中发生的错误 = 6 + 1 = 7
需要分配值为 5 的内存,替换 LRU 2
在此过程中发生的错误 = 7 + 1 = 8。
评估 LRU 中页面错误的算法
LRU 算法是在操作系统领域中提到的替换过程。容量是内存中保存页面的数量。现在,我们将设置该特定内存中的当前页面集。此过程始终使用进程值中存在的最近使用频率最低的页面。
步骤 1 - 启动 LRU 操作过程。
步骤 2 - 在此处将总计数声明为 0。
步骤 3 - 创建一个向量类。
步骤 4 - 使用所需的数组大小构造和声明一个数组。
步骤 5 - 使用内存容量的大小启动过程。
步骤 6 - 为该方法创建一个映射。
步骤 7 - 将频率值存储到页面的映射中。
步骤 8 - 遍历页面元素。
步骤 9 - 如果;此处所需的元素存在于基本存储位置中,那么我们将
将其删除并推入。
步骤 10 - 步骤 9 增加了频率的过程。
步骤 11 - 否则;内存已满。删除第一个元素并降低频率。
步骤 12 - 计数增加。
步骤 13 - 比较频率结果。
步骤 14 - 根据频率和基于时间的结果对页面进行排序。
步骤 15 - 如果我们得到相同的频率,则页面将首先到达。
步骤 16 - 重复此过程。
步骤 17 - 返回结果。
步骤 18 - 终止进程。
评估 LRU 中页面错误的语法
int main() {
int capacity = 4;
int arr[] = {7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2};
deque<int> q(capacity);
int count=0;
int page_faults=0;
deque<int>::iterator itr;
q.clear();
for(int i:arr)
{
itr = find(q.begin(),q.end(),i);
if(!(itr != q.end()))
{
++page_faults;
if(q.size() == capacity)
{
q.erase(q.begin());
q.push_back(i);
}
else{
q.push_back(i);
}
}
else
{
q.erase(itr);
q.push_back(i);
}
}
cout<<page_faults;
}
{
int capacity = 4;
int arr[] = {7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2};
ArrayList<Integer> s=new ArrayList<>(capacity);
int count=0;
int page_faults=0;
for(int i:arr)
{
if(!s.contains(i))
{
if(s.size()==capacity)
{
s.remove(0);
s.add(capacity-1,i);
}
else
s.add(count,i);
page_faults++;
++count;
} else {
s.remove((Object)i);
s.add(s.size(),i);
}
}
在上面提到的这种可能的语法中,我们尝试向您展示了在操作系统领域中 LRU 页面错误管理的可能实现。使用此交叉语法,我们将构建一些 C++ 代码来以有效的方式解释和解决问题陈述。
方法
方法 1 - C++ 程序演示与操作系统中内存管理的分页相关的最近最少使用 (LRU) 算法。
方法 2 - C++ 程序使用索引和与操作系统中内存管理的分页相关的 LRU 算法查找页面错误,使用散列函数。
C++ 程序演示与操作系统中内存管理的分页相关的最近最少使用 (LRU) 算法
LRU 也称为最近最少使用算法,是一种在操作系统中处理页面错误的策略。以下是流程 -
开始遍历页面。
将数据插入集合。
请求页面进程。
保持同步发生。
声明索引。
页面错误开始递增。
在集合中查找页面。
用当前页面替换找到的页面。
递增错误。
更新索引
示例代码 1
//C++ program to demonstrate the Least Recently Used (LRU) Algorithm attached with the paging for memory management in Operating System
#include<iostream>
using namespace std;
int main ()
{
int nopages, nofaults, page[20], i, count = 0;
cout << "\n\t Enter no of pages for which you want to calculate page faults:>";
cin >> nopages;
//it will store the numer of Pages
cout << "\n\t Enter the Reference String:";
for (i = 0; i < nopages; i++)
{
cout << "\t"; cin >> page[i];
}
cout << "\n\t Enter the Number of frames:"; cin >> nofaults;
int frame[nofaults], fcount[nofaults];
for (i = 0; i < nofaults; i++)
{
frame[i] = -1;
fcount[i] = 0;
}
i = 0;
while (i < nopages)
{
int j = 0, flag = 0;
while (j < nofaults)
{
if (page[i] == frame[j])
{
flag = 1;
fcount[j] = i + 1;
}
j++;
}
j = 0;
cout << "\n\t***\n";
cout << "\t" << page[i] << "-->";
if (flag == 0)
{
int min = 0, k = 0;
while (k < nofaults - 1) { if (fcount[min] > fcount[k + 1])
min = k + 1;
k++;
}
frame[min] = page[i];
fcount[min] = i + 1;
count++;
while (j < nofaults)
{
cout << "\t|" << frame[j] << "|";
j++;
}
}
i++;
}
cout << "\n\t***\n";
cout << "\n\t Page Fault:" << count;
return 0;
}
输出
Enter no of pages for which you want to calculate page faults:> Enter the Reference String: Enter the Number of frames: *** Page Fault:0
C++ 程序使用索引和与操作系统中内存管理的分页相关的 LRU 算法查找页面错误,使用散列函数
在分页跟踪过程中,当代码尝试访问根本不存在或未在 RAM 中列出的内存(也称为页面)时,会发生页面错误。为了解释此过程,我们将遵循下面提到的这些步骤
迭代进程和参考页面。
删除当前页面。
递增页面错误。
将当前页面追加到页面中。
从页面中删除第一个页面。
使用哈希字符串。
将页面命中作为数字返回
示例代码 2
//C++ program to find page faults by using indexes with LRU algorithm attached with the paging for memory management in Operating System using hashing function
#include<bits/stdc++.h>
using namespace std;
int pageFaults(int pages[], int n, int capacity)
{
unordered_set<int> s;
unordered_map<int, int> indexes;
int page_faults = 0;
for (int i=0; i<n; i++)
{
if (s.size() < capacity)
{
if (s.find(pages[i])==s.end())
{
s.insert(pages[i]);
page_faults++;
}
indexes[pages[i]] = i;
}
else
{
if (s.find(pages[i]) == s.end())
{
int lru = INT_MAX, val;
for (auto it=s.begin(); it!=s.end(); it++)
{
if (indexes[*it] < lru)
{
lru = indexes[*it];
val = *it;
}
}
s.erase(val);
s.insert(pages[i]);
page_faults++;
}
indexes[pages[i]] = i;
}
}
return page_faults;
}
int main()
{
int pages[] = {7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2};
int n = sizeof(pages)/sizeof(pages[0]);
int capacity = 4;
cout << pageFaults(pages, n, capacity);
return 0;
}
输出
6
结论
LRU 替换算法是我们可以比任何其他算法使用更长时间的特定页面。此过程返回较少的页面错误,并且能够完成页面分析。在本文中,我们学习了分页过程及其应用。通过使用上面提到的算法和语法,我们创建了一些代码来以有效的方式解决问题陈述。
数据结构
网络
关系数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP