虚拟地址转为物理地址

主存的分配和地址变换机构实现

一、设计目的

  1. 能实现操作系统主存的分配。
  2. 能了解逻辑地址和物理块的对映转换。

二、设计要求与内容

  1. 某虚拟存储器的用户空间共有x个页面,每页yKB,主存zKB。
  2. 假定某时刻系统为用户的第0、1、2、3页分配的物理块号为5、10、4、7(这部分允许用户任意变化),而该用户作业的长度为w页,试输入逻辑地址转换成物理地址。例如输入0A5C,输出125C,并且可以判断缺页或越界。

三、设计原理:包含程序想法和做法说明

  1. 程序空间的大小为 xy KB,因此逻辑地址的有效位数是 log(xy)/log(2)+10位。
  2. 存储空间的大小是 zKB,因此物理地址至少需要 log(z)+10位。
  3. 求逻辑地址的页号
  4. 求页内地址
  5. 判断是否缺页或越界
  6. 根据逻辑地址的页号查出物理地址的物理块号
  7. 求出物理地址

四、源代码

#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");
    }
}

鄙人水平有限,代码冗余,恳请批评指正

用例

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值