先决条件:页面替换算法
在使用分页进行内存管理的操作系统中,需要页面替换算法来决定当新页面进入时需要替换哪个页面。每当新页面被引用并且不在内存中时,就会发生页面错误并且操作系统替换其中之一现有页面和新需要的页面。不同的页面替换算法建议不同的方法来决定替换哪个页面。所有算法的目标都是减少页面错误的数量。
先进先出 (FIFO) 页替换算法 –
这是最简单的页面替换算法。在该算法中,操作系统跟踪队列中内存中的所有页面,最旧的页面在队列的前面。当一个页面需要被替换时,队列前面的页面被选中进行删除。
示例-1。考虑页面引用字符串1、3、0、3、5、6 和 3 页槽。
最初所有的槽都是空的,所以当 1、3、0 到来时,它们被分配到空槽——> 3页错误。
当 3 出现时,它已经在内存中,所以 —> 0 Page Faults。
然后是 5,它在内存中不可用,因此它替换了最旧的页槽,即 1。—>1页错误。
最后是 6,它在内存中也不可用,因此它替换了最旧的页槽,即 3 —>1页错误。
所以总页面错误 = 5 。
示例-2。考虑以下引用字符串:0, 2, 1, 6, 4, 0, 1, 0, 3, 1, 2, 1。
使用 FIFO 页替换算法 –
因此,页面错误总数 = 9。
给定内存容量(作为它可以容纳的页面数)和一个表示要引用的页面的字符串,编写一个函数来查找页面错误的数量。
实现——让容量是内存可以容纳的页面数。让 set 是内存中的当前页面集。
1- Start traversing the pages.
i) If set holds less pages than capacity.
a) Insert page into the set one by one until
the size of set reaches capacity or all
page requests are processed.
b) Simultaneously maintain the pages in the
queue to perform FIFO.
c) Increment page fault
ii) Else
If current page is present in set, do nothing.
Else
a) Remove the first page from the queue
as it was the first to be entered in
the memory
b) Replace the first page in the queue with
the current page in the string.
c) Store current page in the queue.
d) Increment page faults.
2. Return page faults.
C++
// C++ implementation of FIFO page replacement
// in Operating Systems.
#include
using namespace std;
// Function to find page faults using FIFO
int pageFaults(int pages[], int n, int capacity)
{
// To represent set of current pages. We use
// an unordered_set so that we quickly check
// if a page is present in set or not
unordered_set s;
// To store the pages in FIFO manner
queue indexes;
// Start from initial page
int page_faults = 0;
for (int i=0; i
Java
// Java implementation of FIFO page replacement
// in Operating Systems.
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Queue;
class Test
{
// Method to find page faults using FIFO
static int pageFaults(int pages[], int n, int capacity)
{
// To represent set of current pages. We use
// an unordered_set so that we quickly check
// if a page is present in set or not
HashSet s = new HashSet<>(capacity);
// To store the pages in FIFO manner
Queue indexes = new LinkedList<>() ;
// Start from initial page
int page_faults = 0;
for (int i=0; i
Python3
# Python3 implementation of FIFO page
# replacement in Operating Systems.
from queue import Queue
# Function to find page faults using FIFO
def pageFaults(pages, n, capacity):
# To represent set of current pages.
# We use an unordered_set so that we
# quickly check if a page is present
# in set or not
s = set()
# To store the pages in FIFO manner
indexes = Queue()
# Start from initial page
page_faults = 0
for i in range(n):
# Check if the set can hold
# more pages
if (len(s) < capacity):
# Insert it into set if not present
# already which represents page fault
if (pages[i] not in s):
s.add(pages[i])
# increment page fault
page_faults += 1
# Push the current page into
# the queue
indexes.put(pages[i])
# If the set is full then need to perform FIFO
# i.e. remove the first page of the queue from
# set and queue both and insert the current page
else:
# Check if current page is not
# already present in the set
if (pages[i] not in s):
# Pop the first page from the queue
val = indexes.queue[0]
indexes.get()
# Remove the indexes page
s.remove(val)
# insert the current page
s.add(pages[i])
# push the current page into
# the queue
indexes.put(pages[i])
# Increment page faults
page_faults += 1
return page_faults
# Driver code
if __name__ == '__main__':
pages = [7, 0, 1, 2, 0, 3, 0,
4, 2, 3, 0, 3, 2]
n = len(pages)
capacity = 4
print(pageFaults(pages, n, capacity))
# This code is contributed by PranchalK
C#
// C# implementation of FIFO page replacement
// in Operating Systems.
using System;
using System.Collections;
using System.Collections.Generic;
class Test
{
// Method to find page faults using FIFO
static int pageFaults(int []pages, int n, int capacity)
{
// To represent set of current pages. We use
// an unordered_set so that we quickly check
// if a page is present in set or not
HashSet s = new HashSet(capacity);
// To store the pages in FIFO manner
Queue indexes = new Queue() ;
// Start from initial page
int page_faults = 0;
for (int i = 0; i < n; i++)
{
// Check if the set can hold more pages
if (s.Count < capacity)
{
// Insert it into set if not present
// already which represents page fault
if (!s.Contains(pages[i]))
{
s.Add(pages[i]);
// increment page fault
page_faults++;
// Push the current page into the queue
indexes.Enqueue(pages[i]);
}
}
// If the set is full then need to perform FIFO
// i.e. Remove the first page of the queue from
// set and queue both and insert the current page
else
{
// Check if current page is not already
// present in the set
if (!s.Contains(pages[i]))
{
//Pop the first page from the queue
int val = (int)indexes.Peek();
indexes.Dequeue();
// Remove the indexes page
s.Remove(val);
// insert the current page
s.Add(pages[i]);
// push the current page into
// the queue
indexes.Enqueue(pages[i]);
// Increment page faults
page_faults++;
}
}
}
return page_faults;
}
// Driver method
public static void Main(String []args)
{
int []pages = {7, 0, 1, 2, 0, 3, 0, 4,
2, 3, 0, 3, 2};
int capacity = 4;
Console.Write(pageFaults(pages, pages.Length, capacity));
}
}
// This code is contributed by Arnab Kundu
输出:
7
注意 –我们还可以找到页面点击次数。只需要保持一个单独的计数。如果当前页面已经在内存中,那么这必须算作页面命中。
贝拉迪的异常——
Belady 的异常证明,在使用先进先出 (FIFO) 页面替换算法的同时增加页面帧数时,可能会出现更多页面错误。例如,如果我们考虑引用字符串3, 2, 1, 0, 3, 2, 4, 3, 2, 1, 0, 4 和 3 个槽,我们总共有 9 个缺页错误,但是如果我们将槽增加到 4,我们得到 10 个页面错误。
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。