段页式存储管理

版权声明:未经博主允许不得转载,引用,盗用 https://blog.csdn.net/EliminatedAcmer/article/details/80297922

编写并调试一个段页式存储管理的地址转换的模拟程序。

  首先设计好段表、页表,然后给出若干个有一定代表性的地址,通过查找段表页表后得到转换的地址。

要求打印转换前的地址,相应的段表,页表条款及转换后的地址,以便检查。

我设计的段表和页表如下:

代码:

#include <iostream>
#include <cstring>
#define Maxn 5
using namespace std;

int page_table[5] = {2,3,6,8,9};
int Seg_address[5] = {2,12,30,45,73};
bool page_state[10];
int address[100];
int tran_address[100];
int num;
int Seg_table_register[2];

typedef struct
{
    int seg_address;
    int seg_final_address;
} Box;

typedef struct
{
    Box data[Maxn];
} Segment;

Segment seg_table;

void Seg_table()
{
    Seg_table_register[0] = Seg_address[0];
    Seg_table_register[1] = Maxn;
    memset(page_state,false,sizeof(page_state));
    for(int i = 0; i<5; i++)
    {
        seg_table.data[i].seg_address = Seg_address[i];
        seg_table.data[i].seg_final_address = page_table[4] + Seg_address[i] - Seg_table_register[0];
        page_state[page_table[i]] = true;
        cout<<seg_table.data[i].seg_address<<"  "<<seg_table.data[i].seg_final_address<<endl;
    }
}

void init()
{
    cout<<"input the number of the address:"<<endl;
    cin>>num;
    cout<<"input the address:"<<endl;
    for(int i = 0; i<num; i++)
        cin>>address[i];
    Seg_table();
}

void address_translation()
{
    init();
    for(int k = 0; k<num; k++)
    {
        for(int i = 0; i < Maxn; i++)
        {
            if((address[k] >= seg_table.data[i].seg_address) && (address[k] <= seg_table.data[i].seg_final_address))
            {
                int index = 0;
                for(int j = 0; j<5; j++)
                {
                    if(seg_table.data[i].seg_address + page_table[j] - Seg_table_register[0] == address[k]
                            && page_state[page_table[j]] == true)
                    {
                        cout<<address[k]<<"'s converted address:";
                        cout<<"Segment number:"<<i<<"  Segment address:"
                            <<Seg_address[i]<<"  page number:"<<j
                            <<"  block number:"<<page_table[j]<<endl;
                        page_state[page_table[j]] = false;
                        index = 1;
                        i = Maxn;
                    }
                }
                if(index == 0)
                {
                    cout<<"can not find "<<address[k]<<" in page table!!!"<<endl;
                    i = Maxn;
                }
            }
            else if(address[k] < Seg_table_register[0])
            {
                cout<<"address "<<address[k]<<" is too low!"<<endl;
                i = Maxn;
            }
            else if(address[k] > seg_table.data[Maxn-1].seg_final_address)
            {
                cout<<"address "<<address[k]<<" is too high!"<<endl;
                i = Maxn;
            }
            else
            {
                if(i == Maxn - 1)
                    cout<<"can not find "<<address[k]<<" in segment table!!"<<endl;
            }
        }
    }
}

int main()
{
    address_translation();
    return 0;
}

运行结果示例:


1不在段表中,5 不在页表中,37是段表号为 2,页表号为 4,块号为9的地址,54 处在段表不存在的地址,90超过了段表大小。


没有更多推荐了,返回首页