
分页是与操作系统相关的内存管理过程。它通过使用页面段将一些进程数据从辅助数据存储器存储或检索到主数据存储器或内存中。分页过程发生在进程在页面上遇到任何错误时,我们不能在此处使用新的空闲页面来满足分配过程。LRU过程生成了特定的替换算法需求。当进程产生一个新页面时,它决定哪个页面需要被替换。让我们举个例子 -
输入的内容用于该过程 -
N = 9, C = 4
Present pages for the process = {5, 0, 1, 3, 2, 4, 1, 0, 5}
输出结果为:8
解释 -
分配的内存页面为 5, 0, 1, 3
这个过程中发生的故障 = 4
需要分配内存,值为2,替换LRU 5:
这个过程中发生的错误 = 4+1 = 5
需要分配值为4的内存,替换LRU 0:
这个过程中发生的错误 = 5 + 1 = 6
需要的值为1的内存已经存在:
这个过程中发生的错误 = 6 + 0 = 6
需要分配值为0的内存,以替换最近最少使用的3个内存块:
这个过程中发生的错误 = 6 + 1 = 7
需要分配值为5的内存,这将替换LRU 2:
在这个过程中发生的错误 = 7 + 1 = 8。
算法评估LRU中的页面错误
LRU算法是操作系统领域中提到的一种替换过程。容量是内存中所持有页面的数量。现在我们将在特定内存中设置当前的页面集合。该过程总是将最不经常使用的页面置于进程的值中。
步骤 1 - 启动 LRU 操作的过程。
第二步 - 在这里声明总计为0。
步骤 3 - 创建一个向量类。
第四步 - 构建并声明一个具有所需数组大小的数组。
第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++代码来以高效的方式解释和解决问题陈述。
方法
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 << "
Enter no of pages for which you want to calculate page faults:>";
cin >> nopages;
//it will store the numer of Pages
cout << "
Enter the Reference String:";
for (i = 0; i < nopages; i++)
{
cout << " "; cin >> page[i];
}
cout << "
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 << "
***
";
cout << " " << 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 << " |" << frame[j] << "|";
j++;
}
}
i++;
}
cout << "
***
";
cout << "
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
使用带有LRU算法的索引和分页的C++程序,通过使用哈希函数在操作系统中进行内存管理来查找页面错误
在页面跟踪过程中,当代码尝试访问一个在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, v
.........................................................