Principle of Memory Mappings
Because the total virtual address space of all user processes is substantially larger than the available RAM memory, only the most frequently used elements can be associated with a physical page frame. This is not a problem because most programs occupy only a small part of the memory actually available to them. Let's look at the situation in which a file is manipulated by a text editor. Typically, the user is only bothered with the end of the file so although the complete file is mapped into memory, only a few pages are actually used to store the data at the end of the file. As for the beginning of the file, the kernel need only keep the information in address space about where on the disk to find the data and how to read them when they are required.
The situation is similar with the text segment — only part of it is always needed. If we stay with the example of the text editor, only the code for the central editing function is required. Other parts — the Help system or the obligatory Web and e-mail client common to all programs — are only loaded when explicitly required by the user.3
The kernel must provide data structures to establish an association between the regions of the virtual address space and the places where the related data are located. In the case of a mapped text file, for example, the virtual memory area must be associated with the area on the hard disk in which the filesystem has stored the contents of the file. This is illustrated in Figure 4-4.
Virtual File on hard disk address space
Figure 4-4: Mapping a file into virtual memory.
Of course, I have shown the situation in simplified form because file data are not generally stored contiguously on hard disk but are distributed over several smaller areas (this is discussed in Chapter 9). The kernel makes use of the address_space data structure4 to provide a set of methods to read data from a backing store — from a filesystem, for example. address_spaces therefore form an auxiliary layer to represent the mapped data as a contiguous linear area to memory management.
Allocating and filling pages on demand is known as demand paging. It is based on interaction between the processor and the kernel using a variety of data structures as shown in Figure 4-5.
31 assume that all program parts reside in a single, large binary file. Of course, program parts can also be loaded at the explicit request of the program itself, but I do not discuss this here.
4Unfortunately, the names for the virtual address space and the address space indicating how the data are mapped are identical.
Physical page frames
Virtual address space
Page tables Not mapped, in use Mapped, in use Not mapped, not in use
Physical page frames
Figure 4-5: Interaction of data structures during demand paging.
□ A process tries to access a memory address that is in user address space but cannot be resolved using the page tables (there is no associated page in RAM memory).
□ The processor then triggers a page fault that is forwarded to the kernel.
□ The kernel runs through the process address space data structures responsible for the area in which the fault occurred to find the appropriate backing store or to establish that access was, indeed, incorrect.
□ A physical page is allocated and filled with the requisite data from the backing store.
□ The physical page is incorporated into the address space of the user process with the help of the page tables, and the application resumes.
These actions are transparent to user processes; in other words, the processes don't notice whether a page is actually available or must first be requested by means of demand paging.
Continue reading here: Data Structures
Was this article helpful?