Paging is another memory management technique which widely uses virtual memory concept.
When paging is used, the processor divides the linear address space into fixed-size pages (of 4KBytes, 2 MBytes, or 4 MBytes in length) that can be mapped into physical memory and/or disk storage. When a program (or task) references a logical address in memory, the processor translates the address into a linear address and then uses its paging mechanism to translate the linear address into a corresponding physical address.
Linear Page Translation during Paging
If the page containing the linear address is not currently in physical memory, the processor generates a page-fault exception (#14).
The exception handler for the page-fault exception typically directs the operating system to load the page from disk storage into physical memory.
When the page has been loaded in physical memory, a return from the exception handler causes the instruction that generated the exception to be restarted.
The information that the processor uses to map linear addresses into the physical address space and to generate page-fault exceptions (when necessary) is contained in page directories and page tables stored in memory.
Page-Directory & Page-Table Entries
Advantages of paging
- Address translation: each task has the same virtual address
- Address translation: turns fragmented physical addresses into contiguous virtual addresses
- Memory protection (buggy or malicious tasks can't harm each other or the kernel)
- Shared memory between tasks (a fast type of IPC, also conserves memory when used for DLLs)
- Demand loading (prevents big load on CPU when a task first starts running, conserves memory)
- Memory mapped files
- Virtual memory swapping (lets system degrade gracefully when memory required exceeds RAM size)