Demand Paging is a technique used to implement the concept of virtual memory.
It combines the concept of Paging and Swapping.
The basic idea behind demand paging is that when a process is swapped in, its pages are not swapped in all at once. Rather they are swapped in only when the process needs them.
The task of bringing in and out the various pages of a process is done by a Pager
This concept is called Demand Paging because the page is not loaded the memory until it is not required.
When a process is to be swapped in, the pager guesses which pages will be used before the process is swapped out again.
As a result, only required pages are brought into the memory.
Thus, it avoids reading into memory pages that will not be used in anyway, decreasing the swap time and the amount of physical memory needed.
The pages that are not moved into the memory are marked as invalid in the page table. For an invalid entry the rest of the table is empty.
In case of pages that are loaded in the memory, they are marked as valid along with the information about where to find the swapped out page.
When the process requires any of the page that is not loaded into the memory, a page fault trap is triggered and following steps are followed,
§ The memory address which is requested by the process is first checked, to verify the request made by the process.
§ If its found to be invalid, the process is terminated.
§ In case the request by the process is valid, a free frame is located, possibly from a free-frame list, where the required page will be moved.
§ A new operation is scheduled to move the necessary page from disk to the specified memory location.
§ When the I/O operation is complete, the process’s page table is updated with the new frame number, and the invalid bit is changed to valid.
§ The instruction that caused the page fault must now be restarted from the beginning.
There are cases when no pages are loaded into the memory initially; pages are only loaded when demanded by the process by generating page faults. This is called Pure Demand Paging.
The only major issue with Demand Paging is, after a new page is loaded, the process starts execution from the beginning. Its is not a big issue for small programs, but for larger programs it affects performance drastically.