Operating System: Three Easy Pieces --- Page Fault (Note)

Recall that with TLB misses, we have two types of systems: Hardware managed TLBs (where the

hardware looks in the page table to find the desired translation) and software managed TLBs (

where the OS does). In either type of systems, if a page is not present, the OS is put in charge

to handle the page fault. The appropriately-named OS page-fault handler runs to determine what

to do. Virtually all system handle page faults in software, even with a hardware-managed TLB, the

hardware trusts the OS to manage this important duty. If a page is not present and has been

swapped to disk. The OS will need to swap the page into memory in order to service the page fault.

Thus, a question arises: How will the OS know where to find the desired page ? In many systems,

the page table is a natural place to store such information. Thus, the OS could use the bits in the

PTE normally used for data such as the PFN of the page for a disk access. When the OS receives

a page fault for a page, it looks in the PTE to find the address, and issues the request to disk to

fetch the page into memory. When the disk I/O completes, the OS will then update the page table

to mark the page as present, update the PFN field of the page table entry (PTE) to record the memory

location of the newly fetched page, and retry the instruction, this next attempt may generate a

TLB miss, which would then be serviced and update the TLB with the translation (one could alteenately

update the TLB when servicing the page fault to avoid this step). Finally, a last restart would find

the translation in the TLB and thus proceed to fetch the desired data or instruction from memory at

the translated physical address. Note that while the I/O is in flight, the process will be in the blocked

state. Thus, the OS will be free to run other ready processes while the page fault is being serviced.

Because I/O is expensive, this overlap of the I/O (page fault) of one process and the execution of

another is yet abother way a multiprogrammed system can make the most effective use of its hardware.

Why If Memory Is Full ?: In the process descirbed above,  you may notice that we assumed there

is plenty of free memory in which to page in a page from swap space. Of course, this may not be the

case, memory may be full (or close to it). Thus, the OS might like to first page out one or more pages

to make room for the new pages the OS is about to bring in. The process of picking a page to page out,

or replace is known as the page replacement policy. As is turns out, a lot of thought has been put into

creating a good replacement policy, as kicking out the wrong page can exact a great cost on program

performance. Making the wrong decision can cause a program to run at disk like speed instead of memory

like speed; in current technology that means a program could run 10000 or 100000 times slower. Thus,

such a policy is something we should study in some detail; indeed, that is exactly what we will do in the

next chapter. For now, it is good enough to understand that such a policy exists, built on top of the

mechanisms described above.

that is exactly  

原文地址:https://www.cnblogs.com/miaoyong/p/4858378.html