• Post author:Bhaskar Soni
  • Post published:
  • Post category:Operating System
  • Post comments:1 Comment
  • Reading time:4 mins read

Paging in operating system design is a memory management technique that splits memory into fixed-size blocks. A process’s memory is divided into pages, and physical RAM is divided into frames of the same size. The OS can place any page into any free frame, so a process no longer needs one large continuous block of RAM.

This guide explains how address translation works, what page tables and the TLB do, how demand paging and page faults work, and how page replacement algorithms choose which page to remove.

paging in operating system

Why paging exists

Early systems gave each process one contiguous region of memory. Over time, free memory broke into small gaps that were too small to use. This is external fragmentation. Paging removes it, because any free frame can hold any page. The trade-off is a small amount of internal fragmentation: the last page of a process may not be completely full.

How address translation works

The CPU generates a logical (virtual) address. The memory management unit (MMU) splits it into two parts:

  • page number: which page of the process;
  • offset: the position inside that page.

The MMU looks up the page number in the process’s page table, finds the frame number and adds the offset. The result is the physical address.

Example: with 4 KB pages (4,096 bytes), the logical address 0x3A7F has page number 3 and offset 0xA7F. If the page table maps page 3 to frame 9, the physical address is 9 × 4,096 + 0xA7F = 0x9A7F.

Page tables and the TLB

Each process has its own page table. Besides the frame number, each entry holds flags, such as:

  • valid/present: the page is in RAM;
  • read/write: whether writes are allowed;
  • user/supervisor: whether user code may access it;
  • dirty: the page changed since it was loaded;
  • accessed: the page was recently used.

Looking up the page table for every memory access would be slow. Therefore, CPUs cache recent translations in a translation lookaside buffer (TLB). Most accesses hit the TLB, so translation adds little delay. On 64-bit systems, page tables have several levels, such as four or five on x86-64, to save memory.

Demand paging and page faults

With demand paging, the OS loads a page only when a process first uses it. When a process touches a page that is not in RAM, the CPU raises a page fault. The OS then:

  1. checks that the access is legal;
  2. finds a free frame, or frees one with a replacement algorithm;
  3. reads the page from disk or swap;
  4. updates the page table;
  5. restarts the instruction.

Page faults are normal, but too many of them cause thrashing: the system spends more time swapping pages than doing useful work.

Page replacement algorithms

When RAM is full, the OS must choose a page to evict.

AlgorithmRuleNotes
FIFORemove the oldest loaded pageSimple, but can remove busy pages
LRURemove the least recently used pageGood in practice; costly to track exactly
OptimalRemove the page used furthest in the futureBest possible, but needs knowledge of the future
Clock (second chance)Like FIFO, but skips pages with the accessed bit setCheap approximation of LRU

Example: with three frames and the reference string 1, 2, 3, 1, 4, 2, 5, FIFO causes 5 page faults, LRU causes 6 and Optimal causes 5. So, LRU is usually good, but not always the best for every pattern.

Real operating systems use variants. For instance, Linux uses active and inactive page lists and, in newer kernels, multi-generational LRU.

Paging vs segmentation

PagingSegmentation
Fixed-size pagesVariable-size segments, such as code, stack and data
No external fragmentationExternal fragmentation possible
Invisible to the programmerMatches the program’s logical structure

Modern x86-64 systems rely almost entirely on paging.

Related guides

Sources

Running Linux servers in production? Our Linux server hardening & security audit checks configuration, patching, SSH and logging. You can also use our free Linux server hardening checklist, or contact us.

Post author avatar

Bhaskar Soni

Bhaskar Soni is a Security Engineer and the founder of Youngster Company, based in Ahmedabad, Gujarat. He works hands-on with Linux server hardening, vulnerability assessment and penetration testing (VAPT), ISO 27001 compliance and OSINT investigations. He writes practical, tested guides for IT professionals and Indian businesses, and every article on this site is written or reviewed by him.

This Post Has One Comment

Leave a Reply