先决条件——贝拉迪的异常
Belady’s Anomaly 是指即使在增加帧数后页面错误的数量也会增加。
在本文中,我们使用 FIFO 页替换算法演示 Belady 异常。
例子:
Reference array is: 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5
Output :
When number of frames is 3, page fault : 9
When number of frames is 4, page fault : 10
Reference array is: 0, 1, 5, 3, 0, 1, 4, 0, 1, 5, 3, 4
Output :
When number of frames is 3, page fault : 9
When number of frames is 4, page fault : 10
执行 :
FIFO 页替换算法用于展示 Belady’s Anomaly。首先,使用一个大小等于帧数的数组,这个数组模拟页帧,对这个数组的操作以循环数组类型的方式执行。计数变量用于计算页面错误,每当在页框数组中插入/重写元素时,该变量就会递增。
在这段代码中:
分别测试了两个帧大小 3 和 4,随着帧数的增加,页面错误应该会减少,但发现异常,因为 3 个帧导致 9 个页面错误,而 4 个帧导致 10 个页面错误。这是 Belady 的异常,它是在参考数组和帧大小的特殊情况下观察到的。
C++
#include
using namespace std;
void pageFault(int frame_size, int* ref, int len)
{
// To dynamically allocate an array,
// it represents the page frames.
int* arr = new int[frame_size];
// To initialize the array
for (int i = 0; i < frame_size; i++) {
arr[i] = -1;
}
// To count page faults
int cnt = 0;
int start = 0;
int flag;
int elm;
for (int i = 0; i < len; i++) {
elm = ref[i];
// Linear search to find if the page exists
flag = 0;
for (int j = 0; j < frame_size; j++) {
if (elm == arr[j]) {
flag = 1;
break;
}
}
// If the page doesn't exist it is inserted,
// count is incremented
if (flag == 0) {
if (start < frame_size) {
arr[start] = elm;
start++;
}
else if (start == frame_size) {
arr[0] = elm;
start = 1;
}
cnt++;
}
}
cout << "When the number of frames are: " << frame_size << ", ";
cout << "the number of page faults is : " << cnt << endl;
}
int main()
{
// Reference array
int ref[] = { 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5 };
int len = sizeof(ref) / sizeof(ref[0]);
// The frame size
int frame_size = 3;
pageFault(frame_size, ref, len);
// Increase value of frame size
frame_size = 4;
// The page fault increases
// even after increasing the
// the number of frames.
// This is Belady's Anomaly
pageFault(frame_size, ref, len);
}
Java
// Java Implementation of the above approach
class GFG
{
static void pageFault(int frame_size,
int []ref, int len)
{
// To dynamically allocate an array,
// it represents the page frames.
int []arr = new int[frame_size];
// To initialize the array
for (int i = 0; i < frame_size; i++)
{
arr[i] = -1;
}
// To count page faults
int cnt = 0;
int start = 0;
int flag;
int elm;
for (int i = 0; i < len; i++)
{
elm = ref[i];
// Linear search to find
// if the page exists
flag = 0;
for (int j = 0; j < frame_size; j++)
{
if (elm == arr[j])
{
flag = 1;
break;
}
}
// If the page doesn't exist it is inserted,
// count is incremented
if (flag == 0)
{
if (start < frame_size)
{
arr[start] = elm;
start++;
}
else if (start == frame_size)
{
arr[0] = elm;
start = 1;
}
cnt++;
}
}
System.out.print("When the number of frames are: " +
frame_size + ", ");
System.out.println("the number of page faults is : " + cnt);
}
// Driver Code
public static void main (String[] args)
{
// Reference array
int ref[] = { 1, 2, 3, 4, 1, 2,
5, 1, 2, 3, 4, 5 };
int len = ref.length;
// The frame size
int frame_size = 3;
pageFault(frame_size, ref, len);
// Increase value of frame size
frame_size = 4;
// The page fault increases
// even after increasing the
// the number of frames.
// This is Belady's Anomaly
pageFault(frame_size, ref, len);
}
}
// This code is contributed by AnkitRai01
Python3
# Python3 Implementation of the above approach
def pageFault(frame_size, ref, len):
# To dynamically allocate an array,
# it represents the page frames.
arr = [0] * frame_size;
# To initialize the array
for i in range(frame_size):
arr[i] = -1;
# To count page faults
cnt = 0;
start = 0;
for i in range(len):
elm = ref[i];
# Linear search to find if the page exists
flag = 0;
for j in range(frame_size):
if (elm == arr[j]):
flag = 1;
break;
# If the page doesn't exist it is inserted,
# count is incremented
if (flag == 0):
if (start < frame_size):
arr[start] = elm;
start += 1;
elif (start == frame_size):
arr[0] = elm;
start = 1;
cnt += 1;
print("When the number of frames are: ",
frame_size, end = ", ");
print("the number of page faults is : ", cnt);
# Driver Code
# Reference array
ref = [1, 2, 3, 4, 1, 2,
5, 1, 2, 3, 4, 5 ];
len = len(ref);
# The frame size
frame_size = 3;
pageFault(frame_size, ref, len);
# Increase value of frame size
frame_size = 4;
# The page fault increases
# even after increasing the
# the number of frames.
# This is Belady's Anomaly
pageFault(frame_size, ref, len);
# This code is contributed by 29AjayKumar
C#
// C# Implementation of the above approach
using System;
class GFG
{
static void pageFault(int frame_size,
int []refer, int len)
{
// To dynamically allocate an array,
// it represents the page frames.
int []arr = new int[frame_size];
// To initialize the array
for (int i = 0; i < frame_size; i++)
{
arr[i] = -1;
}
// To count page faults
int cnt = 0;
int start = 0;
int flag;
int elm;
for (int i = 0; i < len; i++)
{
elm = refer[i];
// Linear search to find
// if the page exists
flag = 0;
for (int j = 0; j < frame_size; j++)
{
if (elm == arr[j])
{
flag = 1;
break;
}
}
// If the page doesn't exist it is inserted,
// count is incremented
if (flag == 0)
{
if (start < frame_size)
{
arr[start] = elm;
start++;
}
else if (start == frame_size)
{
arr[0] = elm;
start = 1;
}
cnt++;
}
}
Console.Write("When the number of frames are: " +
frame_size + ", ");
Console.WriteLine("the number of page " +
"faults is : " + cnt);
}
// Driver Code
public static void Main()
{
// Reference array
int []refer = { 1, 2, 3, 4, 1, 2,
5, 1, 2, 3, 4, 5 };
int len = refer.Length;
// The frame size
int frame_size = 3;
pageFault(frame_size, refer, len);
// Increase value of frame size
frame_size = 4;
// The page fault increases
// even after increasing the
// the number of frames.
// This is Belady's Anomaly
pageFault(frame_size, refer, len);
}
}
// This code is contributed by kanugargng
Javascript
输出:
When the number of frames are: 3, the number of page faults is : 9
When the number of frames are: 4, the number of page faults is : 10