主存的分配和地址变换机构实现
主存的分配和地址变换机构实现
一、设计目的
- 能实现操作系统主存的分配。
- 能了解逻辑地址和物理块的对映转换。
二、设计要求与内容
- 某虚拟存储器的用户空间共有x个页面,每页yKB,主存zKB。
- 假定某时刻系统为用户的第0、1、2、3页分配的物理块号为5、10、4、7(这部分允许用户任意变化),而该用户作业的长度为w页,试输入逻辑地址转换成物理地址。例如输入0A5C,输出125C,并且可以判断缺页或越界。
三、设计原理:包含程序想法和做法说明
- 程序空间的大小为 xy KB,因此逻辑地址的有效位数是 log(xy)/log(2)+10位。
- 存储空间的大小是 zKB,因此物理地址至少需要 log(z)+10位。
- 求逻辑地址的页号
- 求页内地址
- 判断是否缺页或越界
- 根据逻辑地址的页号查出物理地址的物理块号
- 求出物理地址
四、源代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
int x, y, z; // 某虚拟存储器的用户空间共有x个页面,每页yKB,主存zKB
int w; //用户作业长度
int pagewide; //页号
int logicalSign; //逻辑地址的有效位数
int physicalSign; //物理地址至少需要的位数
typedef struct page
{
int piece;
} page;
int xToTen(char num[])
{
int x = 16;
int len = strlen(num);
int numTen = 0;
for (int i = 0; i < len; i++)
{
if (num[i] >= 48 && num[i] <= 57)
numTen += (num[i] - '0') * pow(x, len - i - 1);
else
numTen += (num[i] - 'A' + 10) * pow(x, len - i - 1);
}
return numTen;
}
void tenToTwo(int numTen, int *ans, int wide)
{
int top = wide - 1;
int x = numTen;
while (x)
{
ans[top--] = x % 2;
x /= 2;
}
}
int main()
{
printf("plese input the page num:\n");
scanf("%d", &x);
page *pagetable = (page *)malloc(sizeof(page) * (x + 1));
printf("please input the page size:\n");
scanf("%d", &y);
printf("lease input the main memory size\n");
scanf("%d", &z);
printf("pleace input the The length of the user's job:\n");
scanf("%d", &w);
pagewide = log(x) / log(2);
printf("The page number length is %d\n", pagewide);
printf("pleace input physical block number for each page:(num: %d )\n", pagewide);
for (int i = 0; i < pagewide; i++)
{
scanf("%d", &pagetable[i].piece);
}
logicalSign = pagewide + log(y) / log(2) + 10;
physicalSign = log(z) / log(2) + 10;
printf("The significant bits of the logical address is %d \n", logicalSign);
printf("The minimum number of bits required for the physical address is %d\n", physicalSign);
char logical16[55];
printf("please input the logical address:(0x)\n");
while (scanf("%s", logical16) != EOF)
{
int logical10 = xToTen(logical16);
int logical2[50]; //二进制
memset(logical2, 0, logicalSign * sizeof(int) + 1);
tenToTwo(logical10, logical2, logicalSign);
printf("The logical address is :\n");
for (int i = 0; i < logicalSign; i++)
printf("%d", logical2[i]);
printf("\nPage number is:\n");
for (int i = 0; i < pagewide; i++)
printf("%d", logical2[i]);
printf("\nThe in-page address is:\n");
for (int i = pagewide; i < logicalSign; i++)
printf("%d", logical2[i]);
printf("\n");
int pageID = logical10 / (y * 1024);
int pageOffset = logical10 % (y * 1024);
if (pageID >= w)
{
printf("越界\n");
continue;
}
if (pageID >= pagewide)
{
printf("缺页\n");
continue;
}
int physical10 = pagetable[pageID].piece * y * 1024 + pageOffset; // 10
int physical2[50];
memset(physical2, 0, physicalSign * sizeof(int) + 1);
tenToTwo(physical10, physical2, physicalSign);
printf("The physical address is:\n");
for (int i = 0; i < physicalSign; i++)
{
printf("%d", physical2[i]);
}
printf("\n");
printf("%X\n", physical10);
printf("please input the logical address:(0x)\n enter ctrl+z to exit\n");
}
}
鄙人水平有限,代码冗余,恳请批评指正