Friday, January 31, 2020

Demand Paging


Consider a menu driven program that has 50 options. Depending upon user choice one option will execute. But to execute a program it must be loaded into the memory.

One way is to load the complete program in physical memory at program execution time. This makes inefficient use of memory because out of 50 options available, code under one option will be executed.

Alternatively, we can load pages only when they are needed. This is known as demand paging and used in virtual memory management.

Demand Paging is an efficient memory management technique in which page is loaded only when demanded during program execution. Pages that are never accessed, never loaded into the memory. 

This technique has following advantages
i.                     No limitation on size of physical memory available.
ii.                   Each program takes less memory as a result more programs run at same time which in turn increases CPU utilization and throughput.
iii.                  Less number of swapping occurs therefore program runs faster


In demand paging, each process resides in secondary memory(disk). When a process execute instead of loading entire process in physical memory, the page that is needed will only be swapped. As compared to swappere who swaps all the pages of process in memory, we use a lazy swapper that swap only those pages that are need. In addition, since each process is a collection of pages therefore we call it a pager not swapper. Pager guess which page will be used and therefore instead of swapping whole process, pager brings only those pages in memory that is needed.

To distinguish between the pages that are in memory or on the disk, page table uses valid invalid bit scheme is used. If the page is legal and in memory, then the bit is set to valid. The bit is set to invalid, if the page is in the disk or page doesn’t belong to logical address space of the process.

When a process executes and access only pages that are in memory (known as memory resident pages), execution occurs normally. BUT when a process requires a page that was not in memory (bit is invalid), page fault occurs. The following steps is required to handle page fault
1.       Trap sent to the operating system
2.       If page reference was legal and determine the location of the page on the disk to brought the page into the memory.
3.       Find a free frame.
4.       Issue a disk read operation to a free frame
5.       After completing disk read, update page table to show desired page is in the memory now.
6.       Restart the instructions that were interrupted by the trap.

1 comment: