操作系统——页面置换算法模拟

Opt&FIFO题目

#include <iostream>
#include <time.h>
#include <vector>

using namespace std;

void calculate(vector<int> pg, int hit)
{
    cout << "命中次数:" << hit << endl;
    cout << "未命中次数:" << pg.size() - hit << endl;
    cout << "缺页率:" << (pg.size() - hit) * 100 / pg.size() << "%" << endl;
}

bool serach(vector<int> &frame, int key)
{
    for (auto &fr : frame)
    {
        if (key == fr)
            return true;
    }
    return false;
}

int to_used_optimal(vector<int> frame, vector<int> pg, int index)
{
    int res = -1, farthest = index + 1;

    for (int i = 0; i < frame.size(); i++)
    {
        int j;
        for (j = index; j < pg.size(); j++)
        {
            if (pg[j] == frame[i])
            {
                if (j > farthest)
                {
                    farthest = j;
                    res = i;
                }
                break;
            }
        }
        if (j == pg.size())
            return i;
    }
    return res == -1 ? 0 : res;
}

void Optimal(vector<int> pg, int fn)
{
    vector<int> frame;
    int hit = 0;

    for (int i = 0; i < pg.size(); i++)
    {

        if (serach(frame, pg[i]))
        {
            hit++;
            continue;
        }

        if (frame.size() < fn)
            frame.push_back(pg[i]);
        else
        {
            int res = to_used_optimal(frame, pg, i);
            frame[res] = pg[i];
        }
    }
    calculate(pg, hit);
}

void FIFO(vector<int> pg, int fn)
{
    vector<int> frame;
    int hit = 0;

    int index = 0; // 逻辑变量
    for (int i = 0; i < pg.size(); i++)
    {
        if (serach(frame, pg[i]))
        {
            hit++
            continue;
        }

        if (frame.size() < fn)
        {
            frame.push_back(pg[i]);
        }
        else
        {

            frame[index % fn] = pg[i];
            index++;
        }
    }
    calculate(pg, hit);
}

void memu()
{
    cout << "*********************************" << endl;
    cout << "*           |  ̄▽ ̄ |           *" << endl;
    cout << "*"
         << "                               "
         << "*" << endl;
    cout << "*"
         << "                               "
         << "*" << endl;
    cout << "*"
         << "          1.Show  Opt          "
         << "*" << endl;
    cout << "*"
         << "          2.Show FIFO          "
         << "*" << endl;
    cout << "*"
         << "          3.Show  ALL          "
         << "*" << endl;
    cout << "*"
         << "                               "
         << "*" << endl;
    cout << "*"
         << "                               "
         << "*" << endl;
    cout << "*********************************" << endl;
}

int main()
{
    vector<int> pg;
    srand((unsigned)time(NULL));
    int pn, fn, visit_num;
    cout << "请输入访问次数:";
    cin >> visit_num;
    cout << "请输入页的数量pn:";
    cin >> pn;
    for (int i = 0; i < visit_num; i++)
    {
        int rand_num = rand() % pn;
        pg.push_back(rand_num);
    }
    // 看一下随机数
    cout << "电脑随机生成页面走向" << endl;
    for (auto &c : pg)
    {
        cout << c << " ";
    }
    puts("");

    cout << "请输入内存块数:";
    cin >> fn;

    cout << "请选择你要替换的算法,选择如下" << endl;
    memu();
    int num;
    cout << "请输入你的选择";
    cin >> num;
    switch (num)
    {
    case 1:
    {
        cout << "最佳置换算法" << endl;
        Optimal(pg, fn);
        break;
    }
    case 2:
    {
        cout << "先进先出算法" << endl;
        FIFO(pg, fn);
        break;
    }
    case 3:
    {
        cout << "最佳置换算法" << endl;
        Optimal(pg, fn);
        cout << "-----------------------" << endl;
        cout << "先进先出算法" << endl;
        FIFO(pg, fn);
        break;
    }
    }
    return 0;
}

在这里插入图片描述

  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

此情可待成追忆w

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值