G53OPS - Operating Systems

This course is run at the The University of Nottingham within the School of Computer Science & IT. The course is run by Graham Kendall (EMAIL : gxk@cs.nott.ac.uk)


Paging

In a computer system that does not support virtual memory, when a program generates a memory address it is placed directly on the memory bus which causes the requested memory location to be accessed.

On a computer that supports virtual memory, the address generated by a program goes via a memory management unit (MMU). This unit maps virtual addresses to physical addresses.

This diagram shows how virtual memory operates. The computer in this example can generate 16-bit addresses. That is addresses between 0 and 64K. The problem is the computer only has 32K of physical memory so although we can write programs that can access 64K of memory, we do not have the physical memory to support that.

We obviously cannot fit 64K into the physical memory available so we have to store some of it on disc.

The virtual memory is divided into pages. The physical memory is divided into page frames. The size of the virtual pages and the page frames are the same size (4K in the diagram above). Therefore, we have sixteen virtual pages and eight physical pages. Transfers between disc and memory are done in pages.

Now let us consider what happens when a program generates a request to access a memory location. Assume a program tries to access address 8192. This address is sent to the MMU. The MMU recognises that this address falls in virtual page 2 (assume pages start at zero). The MMU looks at its page mapping and sees that page 2 maps to physical page 6. The MMU translates 8192 to the relevant address in physical page 6 (this being 24576). This address is output by the MMU and the memory board simply sees a request for address 24576. It does not know that the MMU has intervened. The memory board simply sees a request for a particular location, which it honours.

If a virtual memory address is not on a page boundary (as in the above example) then the MMU also has to calculate an offset (in fact, there is always an offset - in the above example it was zero).

As an exercise, and using the diagram above, work out the physical page and physical address that are generated by the MMU for each of the following addresses. The answers are at the end of this section.

So far, all we have managed to do is map sixteen virtual pages onto eight physical pages. We have not really achieved anything yet as, in effect, we have eight virtual pages which do not map to a physical page.

In the diagram above, we represented these pages with an 'X'. In reality, each virtual page will have a present/absent bit which indicates if the virtual page is mapped to a physical page.

We need to look at what happens if the program tries to use an unmapped page. For example, the program tries to access address 24576 (i.e. 24K).

The MMU will notice that the page is unmapped (using the present/absent bit) and will cause a trap to the operating system. This trap is called a page fault. The operating system would decide to evict one of the currently mapped pages and use that for the page that has just been referenced. The sequence of events would go like this.

· The program tries to access a memory location in a (virtual) page that is not currently mapped.
· The MMU causes a trap to the operating system. This results in a page fault.
· A little used virtual page is chosen (how this choice is made we will look at later) and its contents are written to disc.
· The page that has just been referenced is copied (from disc) to the virtual page that has just been freed.
· The virtual page frames are updated.
· The trapped instruction is restarted.

In the example we have just given (trying to access address 24576) the following would happen.

· The MMU would cause a trap to the operating system as the virtual page is not mapped to a physical location.
· A virtual page that is mapped is elected for eviction (we'll assume that page 11 is nominated).
· Virtual page 11 is mark as unmapped (i.e. the present/absent bit is changed).
· Physical page 7 is written to disc (we'll assume for now that this needs to be done). That is the physical page that virtual page 11 maps onto.
· Virtual page 6 is loaded to physical address 28672 (28K).
· The entry for virtual page 6 is changed so that the present/absent bit is changed. Also the 'X' is replaced by a '7' so that it points to the correct physical page.
· When the trapped instruction is re-executed it will now work correctly.

You might like to work through this to see how the mapping is changed.

Last Page Back to Main Index Next Page

 

 


 Last Updated : 23/01/2002