**
编写一个段页式存储管理模拟程序
** 用C语言编写的一个段页式存储管理模拟程序
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define Maxn 5
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;
printf("%d\t%d\n",seg_table.data[i].seg_address,seg_table.data[i].seg_final_address);
}
}
void init()
{
printf("请输入地址数量:");
scanf("%d",&num);
printf("请输入地址数大小:");
for(int i = 0; i<num; i++)
scanf("%d",&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)
{
printf("%d的转换地址\n",address[k]);
printf("段号:%d\n",i);
printf("段号对应的地址:%d\n",Seg_address[i]);
printf("页号:%d\n",j);
printf("块号:%d\n",page_table[j]);
page_state[page_table[j]] = false;
index = 1;
i = Maxn;
}
}
if(index == 0)
{
printf("在页表中没能找到:%d !!!\n",address[k]);
i = Maxn;
}
}
else if(address[k] < Seg_table_register[0])
{
printf("地址太低:%d !\n",address[k]);
i = Maxn;
}
else if(address[k] > seg_table.data[Maxn-1].seg_final_address)
{
printf("地址越界:%d !\n",address[k]);
i = Maxn;
}
else
{
if(i == Maxn - 1)
printf("在段表中没能找到:%d !!\n",address[k]);
}
}
}
}
int main()
{
address_translation();
return 0;
}
程序输出的结果为:
|