Paging
- In Turkish
- sayfalama
In short
Paging is a memory management scheme that splits memory into fixed-size pages and uses page tables to map each process's virtual pages to physical RAM.
What is paging in operating systems?
Paging is the technique most operating systems use to implement virtual memory. Each process's virtual address space is divided into fixed-size blocks called pages, commonly 4 KB, and physical RAM is divided into frames of the same size. Any page can be placed in any free frame, so a process's memory does not need to be one continuous region of RAM.
Every virtual address is split into a page number and an offset within the page. The CPU's memory management unit looks up the page number in the process's page table, a multi-level tree on 64-bit systems, to find the matching frame, and a small cache called the translation lookaside buffer (TLB) remembers recent lookups so most translations are nearly free. Each page table entry also holds flags, such as whether the page is present, writable, or executable. If the page is not present, the CPU raises a page fault, and the kernel loads the page from disk, updates the table, and retries the instruction.
Paging is like a book printed on loose, numbered pages that are stored in whichever slots of a big filing cabinet happen to be free, with an index card saying which slot holds each page. Because pages are loaded only when first touched, known as demand paging, programs start quickly and unused parts never take up RAM. Paging also enables shared libraries, memory-mapped files, and copy-on-write, where a forked process shares its parent's pages until one of them writes.
Paging is often confused with virtual memory and with swapping. Virtual memory is the overall idea of giving each process a private address space, and paging is the main mechanism that implements it. Swapping, or paging out, is what happens when the kernel moves pages to swap space on disk because RAM is full. Paging in an operating system is also unrelated to pagination in web APIs, which splits long result lists into pages.
Key takeaways
- Memory is divided into fixed-size pages, commonly 4 KB, and RAM into frames.
- Page tables map each virtual page to a physical frame.
- The TLB caches recent translations so lookups stay fast.
- Accessing a page that isn't in RAM causes a page fault, which the kernel handles.
- Paging is the main mechanism behind virtual memory.
Example
PAGE_SIZE = 4096 # 4 KB pages
def translate(virtual_addr, page_table):
page_number = virtual_addr // PAGE_SIZE
offset = virtual_addr % PAGE_SIZE
if page_number not in page_table:
raise RuntimeError("page fault: the kernel must load this page")
frame = page_table[page_number]
return frame * PAGE_SIZE + offset
# Virtual page 2 is stored in physical frame 7
print(hex(translate(0x2ABC, {2: 7}))) # 0x7abcReaders ask
What is a page fault?
A page fault is the CPU's signal that a program touched a page that isn't currently mapped to RAM. A minor fault is resolved without disk access, for example by mapping a page already in memory, while a major fault requires reading the page from disk and is much slower.
What is the TLB?
The translation lookaside buffer is a small cache inside the CPU that stores recent virtual-to-physical address translations. It saves the CPU from walking the page table on almost every memory access.
What is the difference between paging and swapping?
Paging is the general scheme of managing memory in fixed-size pages. Swapping means moving pages out of RAM to swap space on disk when memory runs low, and reading them back when they are needed.
See also
- Virtual MemoryOperating Systems, p. 37Virtual memory is an operating system technique that gives each process its own private address space and maps it to physical RAM or disk behind the scenes.
- Swap SpaceOperating Systems, p. 30Swap space is an area on disk that the operating system uses as overflow for RAM, moving rarely used memory pages there when physical memory runs low.
- KernelOperating Systems, p. 17A kernel is the core part of an operating system that manages the CPU, memory, and hardware devices and controls how programs get access to those resources.
- CPU CacheOperating Systems, p. 5A CPU cache is a small, very fast memory on the processor that keeps copies of recently used data from RAM, so the CPU spends less time waiting for memory.
- ProcessOperating Systems, p. 23A process is a running instance of a program, with its own memory space, resources, and at least one thread of execution managed by the operating system.
- Memory LeakOperating Systems, p. 19A memory leak is a bug where a program keeps holding memory it no longer needs, so its memory usage grows over time and can slow down or crash the system.
Spotted a mistake or something missing on this page?Suggest an edit