本文介绍了需求分页交换帧的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

免责声明:此问题与我的操作系统类有关.但是,它不是针对任何特定的家庭作业问题的.我只是想了解.

Disclaimer: This question is related to my operating systems class. However, it's not for any specific homework question. I'm just trying to understand.

这是到目前为止我对需求分页的了解.每个进程都有自己的分页表.该表包含逻辑地址的索引,其对应的物理地址以及一个有效/无效位,该位指示该页当前是否已加载到主存储器中.

Here's what I think I know about demand paging so far. Each process has its own paging table. This table contains an index of the logical address, its corresponding physical address, and a valid/invalid bit which indicates whether the page is currently loaded into main memory.

访问页面时,如果将有效/无效位设置为有效,则只需通过表中的逻辑地址条目在主存储器中访问该页面.如果该页面未包含在物理内存中,则将产生页面错误.这将导致程序尝试将页面加载到内存中.

When a page is accessed, if the valid/invalid bit is set to valid, then the page is simply accessed in main memory via the logical address entry in the table. If the page is not contained in physical memory, then a page fault is generated. This causes the program to try and load the page into memory.

系统首先检查所请求的地址是否有效.如果不是,则该过程终止.如果地址有效,则系统检查空闲帧.如果找到空闲帧,则系统从磁盘加载请求的数据并将其放置在空闲帧中.在分页表中将有效/无效位设置为有效,将逻辑地址设置为先前的空闲帧,然后程序可以继续执行.

The system first checks if the requested address is valid. If not, then the process is terminated. If the address is valid, then the system checks for free frames. If a free frame is found, then the system loads the requested data from the disk and places it in the free frame. The valid/invalid bit is set to valid in the paging table, the logical address is set to the previously free frame, and the program can continue executing.

如果不存在空闲帧,则系统必须交换出当前使用的帧并将请求的数据放入该帧中.系统使用诸如LRU,MRU等之类的算法来确定要换出的页面.系统换出旧页面,装入新页面,然后将控制权返回给该流程.

If no free frames are present, then the system must swap out a currently used frame and place the requested data in the frame. The system an algorithm such as LRU, MRU, etc. to determine which page to swap out. The system swaps out the old page, loads in the new page and then returns control to the process.

这是我的问题:当换出整个帧时,如何设置访问该帧的分页表的有效/无效位?也就是说,系统如何知道进程的分页表正在访问换出的帧?

Here's My Question: When the full frame is swapped out, how does the valid/invalid bit of a paging table which accesses this frame get set? That is, how does the system know that the paging table of a process access the frame which is being swapped out?

这是一个例子.假设有两个进程,每个进程的页表大小为2,而物理内存的大小为2.让进程的页表如下:

Here's an example. Suppose there are two process, each with paging tables of size 2 and a physical memory of size 2. Let the paging tables of the processes be as follows:

处理虚拟内存:

Address  Contents
0        'A'
1        'B'

进程B虚拟内存:

Address  Contents
0        'C'
1        'D'

处理分页表:

Logical  Physical  Valid/Invalid
0        0         Valid
1        0         Invalid

进程B分页表:

Logical  Physical  Valid/Invalid
0        1         Valid
1        0         Invalid

主内存:

Address  Contents
0        'A'
1        'C'

现在假设进程A尝试访问其逻辑地址B.系统换出主存储器地址1的内容并换入'D'.系统如何知道将Process B页表中逻辑地址0的有效/无效位设置为无效?

Now suppose Process A attempts to access its logical address of B. The system swaps out the contents of main memory address 1 and swaps in 'D'. How does the system know to set the valid/invalid bit to invalid for the logical address of 0 in Process's B paging table?

推荐答案

在页表本身或OS内核的其他地方,都有更多的控制字段来区分根本没有映射的内存范围(从来没有已分配/已映射或已释放/未映射)和指向已换出的内存位置的内存范围.在这两种情况下,都无法访问范围,并且访问会导致页面错误,但是在这些情况下,操作系统会以不同的方式处理此页面错误.在前者中,它终止该过程.在后者中,它尝试将数据带回到内存中,并将映射恢复到可以访问数据的状态.

There are more control fields either in the page tables themselves or elsewhere in the OS kernel to distinguish between a memory range that has no mapping at all (has never been allocated/mapped or has been freed/unmapped) and a memory range that points to a memory location that's been swapped out. In both cases the range cannot be accessed and an access results in a page fault, but the OS handles this page fault differently in these cases. In the former it terminates the process. In the latter it tries to bring the data back to the memory and restore the mapping to a state in which the data can be accessed.

OS知道在哪里使用物理内存的页面.它要么扫描页表以找出该表,要么查询它自己维护的数据结构.如果OS需要从进程中窃取页面,则将其保存(如果已被修改),它将更改映射,以便进程无需OS干预即可不再直接访问此页面,然后在其他地方重用此页面,例如通过用正确的数据填充数据并适当地映射它,以便进程可以访问数据来在不同的过程中进行操作.

The OS knows where pages of the physical memory are used. It either scans page tables to find that out or consults its own data structures that it maintains. If the OS needs to steal a page from a process, it saves it (if it's been modified), it alters the mapping so that process can no longer directly access this page without the OS intervention, then reuses this page elsewhere, e.g. in a different process by filling it with the right data and mapping it appropriately so that process can access the data.

这篇关于需求分页交换帧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-18 05:12