首頁  >  文章  >  後端開發  >  最近最少使用(LRU)中的頁面錯誤

最近最少使用(LRU)中的頁面錯誤

WBOY
WBOY轉載
2023-08-29 17:49:07736瀏覽

最近最少使用(LRU)中的頁面錯誤

分頁是與作業系統相關的記憶體管理過程。它透過使用頁面段將一些進程資料從輔助資料記憶體儲存或檢索到主資料記憶體或記憶體。分頁過程發生在進程在頁面上遇到任何錯誤時,我們不能在此處使用新的空閒頁面來滿足分配過程。 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 程式碼來以高效的方式解釋和解決問題陳述。

方法

  • 方法1 - C 程式示範了最近最少使用(LRU)演算法,附帶頁面用於作業系統中的記憶體管理。

  • 方法2 - 使用帶有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 << "\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

使用帶有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, 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)替换算法是一种特定的页面算法,我们可以使用它比任何其他算法更长的时间。该过程返回较少的页面错误,并能够完成页面分析。在本文中,我们学习了分页过程及其应用。通过使用上述提到的算法和语法,我们已经创建了一些代码以高效地解决问题陈述。

以上是最近最少使用(LRU)中的頁面錯誤的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:tutorialspoint.com。如有侵權,請聯絡admin@php.cn刪除