"次置换后的页内音讯为"&lt

作者:澳门娱乐

实现要求:假设每个页面可以存放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[澳门娱乐6165,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

本文由澳门娱乐6165发布,转载请注明来源

关键词: