了解分页虚拟内存的第一步:在初始缺页时创建页表条目
我想了解虚拟内存分页。我有以下代码片段,代表该过程的第一步。这里从主程序中为每个逻辑地址调用 search_tbl ,以检查页表是否已经具有将提供的逻辑地址映射到物理内存中的位置的条目。 vfn
是虚拟帧号。
编辑: 这个实现有任何意义吗?还是我走错了路?
任何帮助/建议将不胜感激。谢谢。
uint vfn_bits;//virtual frame number
static tbl_entry **tbl;
uint page_bits = log_2(pagesize);
vfn_bits = addr_space_bits - page_bits;
tbl = calloc(pow_2(vfn_bits), sizeof (tbl_entry*));
tbl_entry *search_tbl(uint vfn) {
uint index = vfn;
if (tbl[index] == NULL) {
/* Initial miss */
tbl[index] = create_tbl_entry(vfn);
}
return tbl[index];
}
tbl_entry *create_tbl_entry(uint vfn) {
tbl_entry *te;
te = (tbl_entry*) (malloc(sizeof (tbl_entry)));
te->vfn = vfn;
te->pfn = -1;
te->valid = FALSE;
te->modified = FALSE;
te->reference = 0;
return te;
}
I am trying to understand virtual memory paging. I have the following code snippet that represents the first step in the process. Here search_tbl
is called from the main program for each logical address in order to check if the page table already has an entry that maps the provided logical address to a location in physical memory. vfn
is the virtual frame number.
EDITED:
Does this implementation make any sense? Or am I going down the wrong road?
Any help/suggestion would be greatly appreciated. Thank you.
uint vfn_bits;//virtual frame number
static tbl_entry **tbl;
uint page_bits = log_2(pagesize);
vfn_bits = addr_space_bits - page_bits;
tbl = calloc(pow_2(vfn_bits), sizeof (tbl_entry*));
tbl_entry *search_tbl(uint vfn) {
uint index = vfn;
if (tbl[index] == NULL) {
/* Initial miss */
tbl[index] = create_tbl_entry(vfn);
}
return tbl[index];
}
tbl_entry *create_tbl_entry(uint vfn) {
tbl_entry *te;
te = (tbl_entry*) (malloc(sizeof (tbl_entry)));
te->vfn = vfn;
te->pfn = -1;
te->valid = FALSE;
te->modified = FALSE;
te->reference = 0;
return te;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我能看到的唯一真正的问题是
search_tbl()
的返回类型是tbl_entry*
但它实际上返回一个tbl_entry
。不过,考虑一下,如果页表实际上是指向页表条目的指针数组,那么这可能是一个主要问题。另外,如果 sizeof(tbl_entry) > sizeof(tbl_entry*) 您没有为表分配足够的空间。另一个问题可能是
getbits()
。通常的做法是对 n 位整数类型的位进行编号,其中 0 作为最低有效位,n - 1 作为最高有效位。如果getbits()
API 就是这种情况,则您正在根据地址的错误部分计算索引。编辑
对于问题中已被编辑的代码的原始版本来说,上述情况是正确的。
至于评论中的getbits问题,如果使用以下内容(假设32位地址),
则假设最高有效位是数字最高的位,即位31是最高位。因此,如果假设页面大小为 4096 字节,则可以如下获取地址的帧号:
The only real issue I can see with it is that
search_tbl()
's return type istbl_entry*
but it is actually returning atbl_entry
. That could be a major issue though, thinking about, it if the page table is really an array of pointers to page table entries. Also, ifsizeof(tbl_entry) > sizeof(tbl_entry*)
you are not allocating enough space for the table.Another issue might be
getbits()
. It's normal practice to number the bits of an n-bit integer type with 0 as the least significant bit and n - 1 as the most significant bit. If that is the case for thegetbits()
API, you are calculating the index based on the wrong part of the address.Edit
The above was true for the original version of the code in the question which has been edited out.
As for the getbits question in the comment, if the following is used (assuming 32 bit addresses)
That assumes that the most significant bit is the one with the highest number i.e. bit 31 is the highest bit. So, if you assume a page size of 4096 bytes, the frame number of an address can be obtained like this: