(操作系统) C++ 先进先出页面置换算法

该文描述了一个编程任务,要求模拟先进先出(FIFO)页面置换算法。输入包括预先分配的物理块数和页面引用串,输出详细展示了何时发生页面替换以及替换的是哪个页面。程序计算了缺失页面的数量并给出了缺页率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

先进先出页面置换算法

【问题描述】编程模拟先进先出页面置换算法。
【输入形式】
【输出形式】
【样例输入】输入用文件形式,文件名为test.txt

3(事先分配的物理块数)

7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0 1 7 0 1(页面引用串)


【样例输出】

missing 7,no page replacement is required, the physicalblock is 7

missing 0,no page replacement is required, the physicalblock is 7 0

missing 1,no page replacement is required, the physicalblock is 7 0 1

missing 2, replacement page 7, the physicalblock is 2 0 1

no missing 0, in physicalblock: 2 0 1

missing 3, replacement page 0, the physicalblock is 2 3 1

missing 0, replacement page 1, the physicalblock is 2 3 0

missing 4, replacement page 2, the physicalblock is 4 3 0

missing 2, replacement page 3, the physicalblock is 4 2 0

missing 3, replacement page 0, the physicalblock is 4 2 3

missing 0, replacement page 4, the physicalblock is 0 2 3

no missing 3, in physicalblock: 0 2 3

no missing 2, in physicalblock: 0 2 3

missing 1, replacement page 2, the physicalblock is 0 1 3

missing 2, replacement page 3, the physicalblock is 0 1 2

no missing 0, in physicalblock: 0 1 2

no missing 1, in physicalblock: 0 1 2

missing 7, replacement page 0, the physicalblock is 7 1 2

missing 0, replacement page 1, the physicalblock is 7 0 2

missing 1, replacement page 2, the physicalblock is 7 0 1

number of missing pages is 15

page miss rate is 75.00%

#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;

int n;//3
int m;//12
int s[20];
int a[20];
int temp[20];
int sum=0;

bool hit(int q)
{
    for(int j=0;j<n;j++)
    {
        if(s[q]==a[j])
        {
            return true;
        }
    }
    return false;
}

void nohit(int q)
{
    int max=0;
    for(int k=1;k<n;k++)
    {
        if(temp[k]>temp[max])
        {
            max=k;
        }
    }
    cout<<"missing "<<s[q]<<", replacement page "<<a[max]<<", the physicalblock is ";
    a[max]=s[q];
    temp[max]=0;
            for(int j=0;j<n;j++)
            {
                cout<<a[j]<<" ";
                temp[j]++;
            }
            cout<<endl;
            sum++;
}


void search1(int q)
{
    if(hit(q))
    {
        cout<<"no missing "<<s[q]<<", in physicalblock: ";
        for(int j=0;j<n;j++)
            {
                cout<<a[j]<<" ";
            }
            cout<<endl;
        }
    else
    {
        nohit(q);
    }
}

int main()
{
         freopen("test.txt", "rt", stdin);
    cin>>n;
    int i=0;
    while(cin>>s[i])
    {
        if(i<n)
        {
            a[i]=s[i];
            cout<<"missing "<<s[i]<<",no page replacement is required, the physicalblock is ";
            for(int j=0;j<=i;j++)
            {
                cout<<s[j]<<" ";
            }
            cout<<endl;
        }
        i++;
        m=i;
    }
    for(int j=0;j<n;j++)
    {temp[j]=n-j;}
    sum=n;
    for(int q=n;q<m;q++)
    {
        search1(q);
    }
    cout<<"number of missing pages is "<<sum<<endl;
    double result = sum/(m*1.0)*100;
    cout<<"page miss rate is "<<fixed<<setprecision(2)<<result<<"%"<<endl;
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值