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