Paging in operating systems is a memory management scheme that allows the OS to store and retrieve data from secondary storage (like a hard disk) for use in main memory (RAM). It eliminates the need for contiguous allocation of physical memory, enabling more efficient use of memory and supporting virtual memory.
Key Concepts of Paging:
-
Division of Memory:
- Physical memory (RAM) is divided into fixed-size blocks called frames.
- Logical memory (process address space) is divided into same-sized blocks called pages.
- Typical page sizes: 4 KB, 8 KB, etc.
-
Page Table:
- Each process has a page table that maps its logical (virtual) pages to physical frames.
- The OS maintains this table to translate virtual addresses to physical addresses.
-
Address Translation:
- When a process accesses a memory location, the CPU generates a virtual address.
- The Memory Management Unit (MMU) splits this address into:
- Page Number (index into page table)
- Page Offset (location within the page)
- The page table provides the frame number, which is combined with the offset to get the physical address.
-
Handling Page Faults:
- If a page is not in RAM (page fault), the OS loads it from disk (swap space).
- This allows demand paging, where only required pages are loaded.
Advantages of Paging:
✔ Eliminates external fragmentation (since memory is allocated in fixed-size pages).
✔ Enables virtual memory, allowing processes to use more memory than physically available.
✔ Simplifies memory allocation (no need for contiguous blocks).
Disadvantages of Paging:
❌ Overhead due to page tables (can be large for big address spaces).
❌ Internal fragmentation (last page may not be fully used).
❌ Page faults can slow down performance if too frequent (thrashing).
Example:
- A process has 3 pages (P0, P1, P2), and RAM has 4 frames (F0, F1, F2, F3).
- The page table maps:
- When the CPU accesses a virtual address, the MMU checks the page table to find the correct frame.
Extensions of Paging:
- Multi-level Paging (to handle large page tables, e.g., x86 uses 2-4 levels).
- Translation Lookaside Buffer (TLB) (a cache for page table entries to speed up address translation).
- Inverted Page Tables (to reduce page table size by storing mappings per frame instead of per process).
Conclusion:
Paging is a fundamental OS mechanism that enables efficient memory utilization, virtual memory, and process isolation. It is widely used in modern operating systems like Linux, Windows, and macOS.