Segmentation, or cutting into unequal slices, causes fragmentation, while paging avoids that by cutting into slices of certain length. They are two different approaches.

A page frame(页帧) is a page of physical memory.

If we want to allocate memory to a process, just give them enough page frames so that the sum of frame size is larger than required. In the example, a 64-bit address space occupies page frames 3, 7, 5 and 2.

Paging is flexible, and we don’t have to assume how processes use space. Free-space management is also easier.

64-byte address space in 128-byte physical memory

64-byte address space in 128-byte physical memory

A page table is generated by system, to record where each virtual page is placed in physical memory. It stores address translations for each virtual pages of the address space. Different processes have different page tables.

To translate a virtual address into physical address, we split it into 2 piece, virtual page number (VPN) and offset within the page. The length of offset bits is determined by per page size (usually $2^n$?), and is placed at lower bits. The higher part is VPN, deciding how many pages we can select.

A virtual address and its translation

A virtual address and its translation

The picture above showed an example. Offset is left unchanged, and VPN is translated to physical page number (PPN), a.k.a. physical frame number (PFN).

A page table entry (PTE) describes a translation.

Where are page tables stored?

The page table is stored in physical memory.

What’s actually in the page table?

The simplest form is linear page table, which is just an array.

For the contents of each PTE, there are a valid bit, protection bits, present bit, reference bit, etc.

An x86 page table entry

An x86 page table entry

The valid bit indicates whether the translation is valid. All the unused space between stack and heap is marked invalid. If invalid memory is accessed, it generates a trap to the OS. Why are there invalid pages?

The protection bits indicates whether the page could be read from, written to, or executed from. It seems to be related to cross-process code sharing.

The reference bit (accessed bit) is used to track whether a page has been accessed. Unused pages may be collected.

Paging: also too slow

To perform a memory reference, OS must access memory and do calculation for multiple times, which is really slow.