Virtual memory is a mechanism that creates the illusion of having a very big main memory. With the concept of virtual memory, the user can store processes with size bigger than the available main memory. Virtual memory is a technique that allows the execution of processes that are not completely in memory. This technique frees programmers from the concerns of memory-storage limitations.
We do not require the whole code/data at any instant, or even within a finite time – we can bring it in only as needed. For example,
- Arrays, lists, and tables are often allocated more memory than they actually need. An array may be declared 100 by 100 elements, even though it is seldom larger than 10 by 10 elements.
- An assembler symbol table may have room for 3,000 symbols, although the average program has less than 200 symbols. etc.
Therefore, in the virtual memory mechanism, the system stores large programs in form of pages during their execution and only the required pages or portions of processes are loaded into the main memory thereby creating an illusion of having very large memory that can accommodate large programs in it.
Demand Paging
According to the concept of virtual memory, for the execution of any process, only a few pages of a process are required at any instant of time.
However, to decide which pages of a process will reside in the main memory, the system will load into the main memory the page that contains its first instruction. After that, the system loads a page from secondary storage to main memory only when the process explicitly demands that page This technique is known as demand paging.
A demand-paging system is similar to a paging system with swapping. When we want to execute a process, we swap it into memory. Rather than swapping the entire process into memory, however, we use a lazy swapper or pager as shown in the figure below.
Valid/Invalid Bit
The system makes use of a valid and invalid bit to identify whether a page resides in the main memory or not. It uses the valid bit to indicate residency. The meaning of invalid bit is twofold. Firstly, the system uses it to indicate the legal page is currently not present in the main memory. Secondly, it can be used to indicate a page not present in memory because the address is illegal.
Page Fault
The page fault occurs when the system accesses the page which is not present in the main memory. In other words, the page with an invalid bit.
Handling Page fault
- Load the instruction, and check for the relevant page in the page table.
- If the bit of the referenced page is invalid, a page fault occurs. A trap occurs that is sent to the operating system.
- The system checks for the required page in the secondary memory.
- Using the frame replacement algorithm, find the frame location. Read the data from disk to memory.
- Update the page table entry by making an invalid bit to valid.
- Restart the instruction.
PPT
The above discussion has been summarized in the form of a PPT with animations below.
Note: Click within the slide area for animations. Clicking on the “next” slide button will not display any animation