程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> 操作系統請求分頁存儲模擬實驗,操作系統分頁

操作系統請求分頁存儲模擬實驗,操作系統分頁

編輯:C++入門知識

操作系統請求分頁存儲模擬實驗,操作系統分頁


實現要求:假設每個頁面可以存放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

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved