Virtual Memory-Demand Paging

Lets Discuss Virtual Memory Over here ,All the memory management policies we have discussed so far, try to keep a number of processes in the memory simultaneously to allow multiprogramming. But they require an entire process to be loaded in the memory before it can execute.

With the virtual memory technique, we can execute a process which is only partially loaded in memory. Thus, the logical address space may be larger than physical memory, and we can have more processes executing in memory at a time, hence a greater degree of multiprogramming.

1 Demand Paging

Demand paging is the most common virtual memory management system. It is based on the locality model of program execution. As a program executes, it moves from locality to locality.

Locality is defined as a set of pages actively used together. A program is composed of several different localities which may overlap. For example, a small procedure when called, defines a new locality. A while-do loop when being executed defines a new locality. As a program executes, it moves from locality to locality. Procedural languages with while-do, repeat-until, for-do structures (i.e. Pascal, Algol, C, etc.) have less frequently changing localities than other high-level languages with go-to structure (i.e. Basic, Fortran).

In demand paging, programs reside on a swapping device commonly known as the backing store. The backing store, for most of today’s operating systems is a disk.
When the operating system decides to start a new process, it swaps only a small part of this new process (a few pages) into memory. The page table of this new process is prepared and loaded into memory, and the valid/invalid bits of all pages that are brought into memory are set to “valid”. All the other valid/invalid bits are set to “invalid” showing that those pages are not brought into memory.

If the process currently executing tries to access a page which is not in the memory, a page fault occurs, and the OS brings that page into the memory. If a process causes page fault then the following procedure is applied:

  1. Trap the OS.
  2. Save registers and process state for the current process.
  3. Check if the trap was caused because of a page fault and whether the page reference is legal.
  4. If yes, determine the location of the required page on the backing store
  5. Find a free frame.
  6. Read (swap in) the required page from the backing store into the free frame. (During this I/O, the processor may be scheduled to some other process)
  7. When I/O is completed, restore registers and process state for the process which caused the page fault and save state of the currently executing process.
  8. Modify the corresponding page table entry to show that the recently copied page is now in memory.
  9. Resume execution with the instruction that caused the page fault.


While executing a process, in the case of a page fault, the OS finds the desired page on the backing store and if there is no free frames, the OS must choose a page in the memory (which is not the one being used) as a victim, and must swap it out (slow) to the backing store.

Then, the OS changes the valid/invalid bit for the victim page in the page table to indicate that it is no longer in memory. It swaps the desired page into newly freed frame, modifies the frame table, and sets the valid/invalid bit of the new page to valid. The executing process may now go on.

This operations can be summarized as:

  1. Checking the address and finding a free frame or victim page (fast)
  2. Swap out the victim page to secondary storage (slow)
  3. Swap in the page from secondary storage (slow)
  4. Context switch for the process and resume its execution (fast)


In servicing a page fault, the time is spent mainly for swap-out and swap-in. The time required for other operations are negligible.

Virtual memory can be implemented in:

  • paging systems
  • paged segmentation systems
  • segmentation systems (However, segment replacement is much more sophisticated than page replacement since segments are of variable size.)


Try Now – Operating Systems MCQs
Practice Now – Operating System Online Test