Demand paging
Encyclopedia
In computer
operating systems, demand paging (as opposed to anticipatory paging) is an application of virtual memory
. In a system that uses demand paging, the operating system copies a disk page
into physical memory only if an attempt is made to access it (i.e., if a page fault
occurs). It follows that a process
begins execution with none of its pages in physical memory, and many page faults will occur until most of a process's working set
of pages is located in physical memory. This is an example of lazy loading
techniques.
as only those pages demanded by the process are swapped from secondary storage to main memory. Contrast this to pure swapping
, where all memory for a process is swapped from secondary storage to main memory during the process startup.
When a process is to be swapped into main memory for processing, the pager guesses which pages will be used prior to the process being swapped out again. The pager will only load these pages into memory. This process avoids loading pages that are unlikely to be used and focuses on pages needed during the current process execution period. Therefore, not only is unnecessary page load during swapping avoided but we also try to preempt which pages will be needed and avoid loading pages during execution.
Commonly, to achieve this process a page table
implementation is used. The page table maps logical memory to physical memory. The page table uses a bitwise
operator to mark if a page is valid or invalid. A valid page is one that currently resides in main memory. An invalid page is one that currently resides in secondary memory. When a process tries to access a page, the following steps are generally followed:
Computer
A computer is a programmable machine designed to sequentially and automatically carry out a sequence of arithmetic or logical operations. The particular sequence of operations can be changed readily, allowing the computer to solve more than one kind of problem...
operating systems, demand paging (as opposed to anticipatory paging) is an application of virtual memory
Virtual memory
In computing, virtual memory is a memory management technique developed for multitasking kernels. This technique virtualizes a computer architecture's various forms of computer data storage , allowing a program to be designed as though there is only one kind of memory, "virtual" memory, which...
. In a system that uses demand paging, the operating system copies a disk page
Paging
In computer operating systems, paging is one of the memory-management schemes by which a computer can store and retrieve data from secondary storage for use in main memory. In the paging memory-management scheme, the operating system retrieves data from secondary storage in same-size blocks called...
into physical memory only if an attempt is made to access it (i.e., if a page fault
Page fault
A page fault is a trap to the software raised by the hardware when a program accesses a page that is mapped in the virtual address space, but not loaded in physical memory. In the typical case the operating system tries to handle the page fault by making the required page accessible at a location...
occurs). It follows that a process
Process (computing)
In computing, a process is an instance of a computer program that is being executed. It contains the program code and its current activity. Depending on the operating system , a process may be made up of multiple threads of execution that execute instructions concurrently.A computer program is a...
begins execution with none of its pages in physical memory, and many page faults will occur until most of a process's working set
Working set
Peter Denning defines “the working set of information W of a process at time t to be the collection of information referenced by the process during the process time interval ”. Typically the units of information in question are considered to be memory pages...
of pages is located in physical memory. This is an example of lazy loading
Lazy loading
Lazy loading is a design pattern commonly used in computer programming to defer initialization of an object until the point at which it is needed. It can contribute to efficiency in the program's operation if properly and appropriately used...
techniques.
Advantages
Demand paging, as opposed to loading all pages immediately:- Only loads pages that are demanded by the executing process.
- As there is more space in main memory, more processes can be loaded reducing context switchContext switchA context switch is the computing process of storing and restoring the state of a CPU so that execution can be resumed from the same point at a later time. This enables multiple processes to share a single CPU. The context switch is an essential feature of a multitasking operating system...
ing time which utilizes large amounts of resources. - Less loading latency occurs at program startup, as less information is accessed from secondary storage and less information is brought into main memory.
Disadvantages
- Individual programs face extra latency when they access a page for the first time. So demand paging may have lower performance than anticipatory paging algorithms such as prepaging.
- Programs running on low-cost, low-power embedded systemEmbedded systemAn embedded system is a computer system designed for specific control functions within a larger system. often with real-time computing constraints. It is embedded as part of a complete device often including hardware and mechanical parts. By contrast, a general-purpose computer, such as a personal...
s may not have a memory management unitMemory management unitA memory management unit , sometimes called paged memory management unit , is a computer hardware component responsible for handling accesses to memory requested by the CPU...
that supports page replacement. - Memory management with page replacement algorithms becomes slightly more complex.
- Possible security risks, including vulnerability to timing attackTiming attackIn cryptography, a timing attack is a side channel attack in which the attacker attempts to compromise a cryptosystem by analyzing the time taken to execute cryptographic algorithms...
s; see Percival 2005 Cache Missing for Fun and Profit (specifically the virtual memory attack in section 2).
Basic concept
Demand paging follows that pages should only be brought into memory if the executing process demands them. This is often referred to as lazy evaluationLazy evaluation
In programming language theory, lazy evaluation or call-by-need is an evaluation strategy which delays the evaluation of an expression until the value of this is actually required and which also avoids repeated evaluations...
as only those pages demanded by the process are swapped from secondary storage to main memory. Contrast this to pure swapping
Swapping
Swapping can mean:* In computer systems, an older form of memory management, similar to Paging* Swapping * Hot swapping* Book swapping* Wife swapping* Cumswapping...
, where all memory for a process is swapped from secondary storage to main memory during the process startup.
When a process is to be swapped into main memory for processing, the pager guesses which pages will be used prior to the process being swapped out again. The pager will only load these pages into memory. This process avoids loading pages that are unlikely to be used and focuses on pages needed during the current process execution period. Therefore, not only is unnecessary page load during swapping avoided but we also try to preempt which pages will be needed and avoid loading pages during execution.
Commonly, to achieve this process a page table
Page table
A page table is the data structure used by a virtual memory system in a computer operating system to store the mapping between virtual addresses and physical addresses. Virtual addresses are those unique to the accessing process...
implementation is used. The page table maps logical memory to physical memory. The page table uses a bitwise
Bitwise
Bitwise may refer to:* Bitwise operation, a basic function in computer programming* Bitwise IIT Kharagpur, an algorithm-intensive programming contest by CSE IIT Kharagpur...
operator to mark if a page is valid or invalid. A valid page is one that currently resides in main memory. An invalid page is one that currently resides in secondary memory. When a process tries to access a page, the following steps are generally followed:
- Attempt to access page.
- If page is valid (in memory) then continue processing instruction as normal.
- If page is invalid then a page-fault trap occurs.
- Check if the memory reference is a valid reference to a location on secondary memory. If not, the process is terminated (illegal memory access). Otherwise, we have to page in the required page.
- Schedule disk operation to read the desired page into main memory.
- Restart the instruction that was interrupted by the operating system trap.