• In my previous post I had discussed about virtual memory.
  • In this post I will discuss how virtual memory can be implemented using demand paging.
  • In demand paging, a page is loaded in memory when that page is required i.e. when a logical address generated by a process points to a page that is not available in memory. A page is constructed with the individual paging process.
  • The entire logical address space of a process is maintained on a secondary storage device like a disk. We call this disk the paging device. While executing the process, its code and data are into this space. This space is called the swap space of the process. A page is loaded from this area into memory when required.
  • When the virtual memory handler decides to remove a page from memory, the page is copied back into swap space if it is modified. This way the swap space contains an up-to-date copy of every page that is not preset in memory.  Demand paging involves interaction between the MMU and the virtual memory handler.
  • The page table has the valid bit It contains a boolean value 1 if a page is present in memory and 0 if it is not present in memory. Information in the prot info is used to protect the page. ref info contains information about references made to the page.
  • The modified field indicates whether the page has been modified or not. The other info field contains other information about the page like address of the disk bloc in swap space where a copy of the page is maintained.

Demand Paging

  • When a process tries to access a page that is not present in memory access to this page marked as invalid causes a page fault or a missing page interrupt. The OS transfers control to the interrupt handler. When it finds that it is a page fault interrupt, it invokes the virtual memory handler.
  • The interrupt handler passes the page number which generated the page fault (pi) to the virtual handler. The virtual memory checks the free frame list and loads that page into memory from the swap space. The kernel modifies the page table with that entry now that page becomes valid.