《操作系统导论》(Operating Systems: Three Easy Pieces)第18章(分页:介绍) 作业习题程序说明

此博客处内容为 《操作系统导论》(Operating Systems: Three Easy Pieces)第18章(分页:介绍) 作业习题程序说明。

原书英文版地址 https://pages.cs.wisc.edu/~remzi/OSTEP/  该地址中包含书中程序代码

第8章 作业习题程序说明链接:https://blog.csdn.net/cai1149735196/article/details/115241018

第7章 作业习题程序说明链接:https://blog.csdn.net/cai1149735196/article/details/115208047

第16章 作业习题程序说明链接:https://blog.csdn.net/cai1149735196/article/details/115627729

程序名:paging-linear-translate.py。该程序旨在帮助与于理解 线性页表中的虚拟到物理地址转换(virtual-to-physical address translation)。使用-h查看帮助

>> python paging-linear-translate.py -h

Usage: paging-linear-translate.py [options]

Options:
-h, --help              show this help message and exit
-s SEED, --seed=SEED    the random seed
-a ASIZE, --asize=ASIZE 
                        address space size (e.g., 16, 64k, ...)
-p PSIZE, --physmem=PSIZE
                        physical memory size (e.g., 16, 64k, ...)
-P PAGESIZE, --pagesize=PAGESIZE
                        page size (e.g., 4k, 8k, ...)
-n NUM, --addresses=NUM number of virtual addresses to generate
-u USED, --used=USED    percent of address space that is used
-v                      verbose mode
-c                      compute answers for me

首先,不带任何参数运行该程序时会有如下结果

>> python paging-linear-translate.py

ARG seed 0
ARG address space size 16k
ARG phys mem size 64k
ARG page size 4k
ARG verbose False
ARG addresses -1


The format of the page table is simple:
The high-order (left-most) bit is the VALID bit.
  If the bit is 1, the rest of the entry is the PFN.
  If the bit is 0, the page is not valid.
Use verbose mode (-v) if you want to print(the VPN # by
each entry of the page table.

Page Table (from entry 0 down to the max size)

0x8000000c

0x00000000

0x00000000

0x80000006

Virtual Address Trace
  VA 0x00003229 (decimal:    12841) --> PA or invalid address?
  VA 0x00001369 (decimal:     4969) --> PA or invalid address?
  VA 0x00001e80 (decimal:     7808) --> PA or invalid address?
  VA 0x00002556 (decimal:     9558) --> PA or invalid address?
  VA 0x00003a1e (decimal:    14878) --> PA or invalid address?

For each virtual address, write down the physical address it translates to
OR write down that it is an out-of-bounds address (e.g., segfault).

该程序提供的是一个特定进程的页表(记住,在具有线性页表的实际系统中,每个进程都有一个页表;这里我们只关注一个进程它的地址空间,因此是一个页表)。

通过页表可知,对于地址空间的每个虚拟页号(VPN),虚拟页被映射到特定的物理帧号(PFN),因此表现为 有效(1) 还是 无效(0)

页表项的格式很简单:最左边(高阶)位是有效位;剩余的位,如果valid为1,则为PFN。

在上面的示例中,页表将VPN 0映射到PFN 0xc(十进制12),将VPN 3映射到PFN 0x6(十进制6),并保留其他两个虚拟页1和2为无效页。


因为页表是一个线性数组,所以上面打印的是内存中所看到的数据的副本,如果您自己查看这些位的话。然而,如果你运行verbose标志(-v),有时更容易使用这个模拟器;这个标志还将VPN(索引)打印到页表中。在上面的例子中,使用-v标志运行

Page Table (from entry 0 down to the max size)
  [       0]   0x8000000c
  [       1]   0x00000000
  [       2]   0x00000000
  [       3]   0x80000006

那么,您的工作就是使用这个页表将跟踪中给出的虚拟地址转换为物理地址。

让我们看看第一个:VA 0x3229。要将这个虚拟地址转换为物理地址,我们首先必须将它分解为其组成部分:虚拟页号( virtual page number)和偏移量(offset)。我们通过记录地址空间的大小和页面大小来做到这一点。

在本例中,地址空间被设置为16KB(非常小的地址空间),页面大小为4KB。因此,我们知道虚拟地址有14位,偏移量为12位,剩下2位留给VPN。因此,对于我们的地址0x3229,它是二进制的11 0010 0010 1001,我们知道前两位指定了VPN。因此,0x3229位于虚拟页3上,其偏移量为0x229。

接下来在页表中查看VPN 3是否有效或无效映射到一些物理帧,我们看到它确实有效(高位为1)并映射到物理页6。

因此,我们可以通过取物理页6并将其添加到偏移量上来形成最终的物理地址,

如下所示:0x6000(移到适当位置的物理页)或0x0229(偏移量),生成最终的物理地址:0x6229。因此,在本例中,我们可以看到虚拟地址0x3229转换为物理地址0x6229。

To see the rest of the solutions (after you have computed them yourself!),
just run with the -c flag (as always):

...
VA  0: 00003229 (decimal: 12841) --> 00006229 (25129) [VPN 3]
VA  1: 00001369 (decimal:  4969) --> Invalid (VPN 1 not valid)
VA  2: 00001e80 (decimal:  7808) --> Invalid (VPN 1 not valid)
VA  3: 00002556 (decimal:  9558) --> Invalid (VPN 2 not valid)
VA  4: 00003a1e (decimal: 14878) --> 00006a1e (27166) [VPN 3]

当然,可以更改其中的许多参数来产生更有趣的问题。运行带有-h标志的程序,查看有哪些选项

-s  标志改变随机种子,从而产生不同的页表值以及要转换的不同虚拟地址。
-a  标志改变地址空间的大小。
-p  改变物理内存的大小。
-P  标志用来改变页面的大小。
-n  标志生成更多需要转换的地址(而不是默认的5)。
-u  标志改变有效的映射的分数,从0% (-u 0)至100% (-u 100)。默认值是50,这意味着虚拟地址空间中大约有1/2的页面是有效的。
-v  标志打印出VPN号码。

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值