操作系统请求分页存储模拟实验

实现要求:假设每个页面可以存放10条指令,分配给进程的存储块数为4。页面置换算法:分别采用FIFO、LRU两种算法。并计算缺页率。

我的代码依旧没有实现自定义指令数和物理块数,有需要的兄弟可以改一改,我写的代码真的是非常无脑简单,根本不需要注释。

继续废话不多说,直接给代码,但是我的实验结果不给图了,就输入1或者2之后直接换行粘贴我给的数据就行了:

#include<iostream>

using namespace std;

 

int fifo()

{

    int zuoye[20];

    int ye[10];

    int shijian[10];

    int a=10;

    int b=4;

    int c=4;

    int n=10;

    for(int i=1;i<=10;i++)

    {

        cin>>zuoye[i];

    }

    for(int i=1;i<=10;i++)

    {

        if(i<=4)

        {

            shijian[1]=4;

            shijian[2]=3;

            shijian[3]=2;

            shijian[4]=1;

            ye[i]=zuoye[i];

            cout<<"第"<<i<<"次置换后的页内信息为"<<endl;

             for(int h=1;h<=i;h++)

             {

                cout<<ye[h]<<' ';

             }

            cout<<endl;

        }

        else

        {

            int q=1;

            int w=q;

            if(ye[1]==zuoye[i]||ye[2]==zuoye[i]||ye[3]==zuoye[i]||ye[4]==zuoye[i])

                {

                  cout<<"第"<<i<<"不需要置换,不产生缺页中断"<<endl;

                  for(int h=1;h<=4;h++)

                  {

                    cout<<ye[h]<<' ';

                  }

                  cout<<endl;

                  n=n-1;

                }

                if(ye[1]!=zuoye[i]&&ye[2]!=zuoye[i]&&ye[3]!=zuoye[i]&&ye[4]!=zuoye[i])

                  {

                    for(int p=2;p<=b;p++)

                  {

                   if(shijian[w]<shijian[p])

                  {

                  w=p;

                  }

                  }

                  ye[w]=zuoye[i];

                  for(int t=1;t<=4;t++)

                  {

                      if(shijian[t]<shijian[w])

                        shijian[t]++;

                  }

                  shijian[w]=1;

                  cout<<"第"<<i<<"次置换后的页内信息为"<<endl;

                  for(int h=1;h<=4;h++)

                  {

                     cout<<ye[h]<<' ';

                  }

                  cout<<endl;

                }

        }

    }

    cout<<"共产生"<<n<<"次缺页中断"<<endl;

    cout<<"缺页中断率是:"<<n<<'/'<<"10"<<endl;

    return 0;

}

 

 

int lru()

{

    int zuoye[20];

    int ye[10];

    int shijian[10];

    int a=10;

    int b=4;

    int c=4;

    int n=10;

    for(int i=1;i<=10;i++)

    {

        cin>>zuoye[i];

    }

    for(int i=1;i<=10;i++)

    {

        if(i<=4)

        {

            shijian[1]=4;

            shijian[2]=3;

            shijian[3]=2;

            shijian[4]=1;

            ye[i]=zuoye[i];

            cout<<"第"<<i<<"次置换后的页内信息为"<<endl;

             for(int h=1;h<=i;h++)

             {

                cout<<ye[h]<<' ';

             }

            cout<<endl;

        }

        else

        {

            int q=1;

            int w=q;

            if(ye[1]==zuoye[i]||ye[2]==zuoye[i]||ye[3]==zuoye[i]||ye[4]==zuoye[i])

                {

                  cout<<"第"<<i<<"不需要置换,不产生缺页中断"<<endl;

                  for(int h=1;h<=4;h++)

                  {

                    cout<<ye[h]<<' ';

                  }

                  cout<<endl;

                  n=n-1;

                }

                for(int g=1;g<=4;g++)

                {

                    if(ye[g]==zuoye[i])

                    {

                        for(int y=1;y<=4;y++)

                        {

                            if(shijian[y]<shijian[g])

                            {

                                shijian[y]=shijian[y]+1;

                            }

                        }

                        shijian[g]=1;

                    }

                }

                if(ye[1]!=zuoye[i]&&ye[2]!=zuoye[i]&&ye[3]!=zuoye[i]&&ye[4]!=zuoye[i])

                  {

                    for(int p=2;p<=b;p++)

                  {

                 if(shijian[w]<shijian[p])

                  {

                  w=p;

                  }

                  }

                  ye[w]=zuoye[i];

                  for(int t=1;t<=4;t++)

                  {

                      if(shijian[t]<shijian[w])

                        shijian[t]++;

                  }

                  shijian[w]=1;

                  cout<<"第"<<i<<"次置换后的页内信息为"<<endl;

                  for(int h=1;h<=4;h++)

                  {

                     cout<<ye[h]<<' ';

                  }

                  cout<<endl;

                }

        }

    }

    cout<<"共产生"<<n<<"次缺页中断"<<endl;

    cout<<"缺页中断率是:"<<n<<'/'<<"10"<<endl;

    return 0;

}

 

int main()

{

    int m;

    while(true)

    {

        cout<<"1:FIFO算法"<<endl;

        cout<<"2:LRU算法"<<endl;

        cout<<"请输入序号:"<<endl;

        cin>>m;

        switch(m)

        {

            case 1:fifo();

            break;

            case 2:lru();

            break;

            default:cout<<"输入有误,重新输入!"<<endl;

            break;

        }

    }

    return 0;

}

 实验数据:

7 0 1 2 0 3 0 4 2 3

转载于:https://www.cnblogs.com/CIan-722/p/6178954.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值