πŸ“„

Paging

Understand how operating systems divide memory into fixed-size pages for efficient memory management.

Introduction

Paging is a memory management scheme that eliminates the need for contiguous allocation of physical memory. It divides physical memory into fixed-size blocks called frames and logical memory into blocks of the same size called pages. When a process is to be executed, its pages are loaded into any available memory frames from their source (a file system or backing store). Paging allows the physical address space of a process to be non-contiguous. This technique was developed to solve the problems of external fragmentation and the need to fit varying size chunks into the backing store. Paging is one of the most important concepts in modern operating systems.

Why Paging Is Needed

❌ Problems Before
  • β€’Contiguous memory allocation leads to external fragmentation
  • β€’Memory holes form when processes of different sizes are loaded and unloaded
  • β€’Compaction is expensive and time-consuming
  • β€’Variable-size partitions are hard to manage efficiently
  • β€’Physical memory addresses are exposed to programs
βœ“ Solutions Provided
  • β€’Fixed-size pages eliminate external fragmentation
  • β€’Any free frame can hold any page
  • β€’Simple allocation: just find any free frame
  • β€’Logical addresses become independent of physical locations
  • β€’Memory protection is easier to implement

Core Concept Explanation

In paging, the logical address space is divided into pages and the physical address space is divided into frames of the same size. When a process needs memory, its pages can be placed in any available frames.

Key Terms

How It Works (Step by Step)

  1. 1The CPU generates a logical address
  2. 2The logical address is divided into page number (p) and offset (d)
  3. 3The page number is used as an index into the page table
  4. 4The page table entry gives the frame number
  5. 5The physical address is formed by combining frame number and offset
  6. 6Memory is accessed at the physical address

Step-by-Step Worked Example

Consider a system with page size of 4 bytes, physical memory of 32 bytes (8 frames), and logical address space of 16 bytes (4 pages). We want to translate logical address 5.

StepPageRAM StateActionResult
15Logical Address = 5Divide logical address into page number and offset. Page size = 4, so we need 2 bits for offset.5 in binary = 0101. Page Number = 01 (1), Offset = 01 (1)
21Page Table: [Page 0β†’Frame 5, Page 1β†’Frame 6, Page 2β†’Frame 1, Page 3β†’Frame 2]Look up Page 1 in page table.Page 1 maps to Frame 6
36Frame Number = 6Combine frame number with offset. Frame 6 in binary = 110.Physical Address = 110 (frame) + 01 (offset) = 11001 = 25
3
Total References
0
Page Faults
1
Page Hits
100%
Hit Ratio

Logical address 5 translates to physical address 25. The page table enabled this translation without requiring contiguous memory allocation.

Real-Life Analogy

The Book and Page Numbers

Think of a book with chapters that can be printed on any available sheets of paper. Imagine you're printing a book, but instead of printing pages in order on consecutive sheets, you can print any page on any available sheet. Each sheet (frame) can hold exactly one page of content. When you need to find Chapter 3, Page 2: 1. You look up in your index (page table) where that page was printed 2. The index tells you it's on Sheet #15 3. You go to Sheet 15 to find the content This is exactly how paging works: - The book content is divided into fixed-size pages - Physical paper sheets are the frames - The index is the page table - Finding content = address translation

Mapping to Technical Concepts

Book Page→Logical Page
Paper Sheet→Physical Frame
Index→Page Table
Page Number in Index→Page Number (p)
Line on the Page→Offset (d)
Actual Sheet Location→Physical Address
βœ“ Advantages

No External Fragmentation

Since all frames are the same size, any frame can hold any page.

Efficient Memory Use

Memory can be allocated in any order, maximizing utilization.

Easy to Implement Swapping

Pages can be easily moved between RAM and disk.

Memory Protection

Each page can have protection bits (read, write, execute).

Shared Pages

Multiple processes can share the same physical frame (e.g., shared libraries).

⚠ Limitations

Internal Fragmentation

The last page may not be completely filled, wasting space within the page.

Page Table Overhead

Page tables consume memory, especially for large address spaces.

Memory Access Time

Each memory access requires a page table lookup, doubling access time without TLB.

Fixed Page Size

The page size may not be optimal for all types of programs.

πŸ’‘ Common Student Confusions

❌

Page and Frame are the Same Thing

βœ“

Pages are in logical memory, frames are in physical memory. They are of the same size but represent different concepts.

❌

Paging Eliminates All Fragmentation

βœ“

Paging eliminates external fragmentation but introduces internal fragmentation.

❌

Page Table is Stored in the CPU

βœ“

Page tables are stored in main memory. Only a pointer (PTBR) and TLB cache are in hardware.

πŸ“ Exam-Oriented Answer

Definition

Paging is a memory management scheme that permits the physical address space of a process to be non-contiguous by dividing logical memory into pages and physical memory into frames of equal size.

Key Points

  • β€’ Logical memory divided into pages, physical memory into frames
  • β€’ Page table maps page numbers to frame numbers
  • β€’ Logical address = Page Number + Offset
  • β€’ Physical address = Frame Number + Offset
  • β€’ TLB caches recent translations for performance
  • β€’ Eliminates external fragmentation
  • β€’ May cause internal fragmentation

Advantages

  • β€’ No external fragmentation
  • β€’ Simple memory allocation
  • β€’ Supports virtual memory implementation

Disadvantages

  • β€’ Internal fragmentation in last page
  • β€’ Page table overhead
  • β€’ Double memory access without TLB

Conclusion

Paging is essential for modern memory management, enabling virtual memory and efficient multi-programming.

πŸ“Œ Final Summary

  • 1Paging divides logical memory into pages and physical memory into frames
  • 2Page table maintains mapping between page numbers and frame numbers
  • 3Logical address consists of page number and offset
  • 4TLB caches translations for faster memory access
  • 5Eliminates external fragmentation but may cause internal fragmentation
  • 6Enables implementation of virtual memory and memory protection