Ora

What is the difference between paging and demand paging in OS?

Published in Operating System Memory Management 4 mins read

Paging is a fundamental memory management scheme in operating systems, while demand paging is an optimization of paging that significantly improves efficiency by loading pages into memory only when they are actually needed by the CPU.

Understanding Paging

Paging is a non-contiguous memory allocation technique that allows a process's physical memory to be non-contiguous. It divides the logical address space (of a process) into fixed-size blocks called pages and the physical address space (RAM) into fixed-size blocks called frames (or page frames). Both pages and frames are typically the same size.

The primary goal of paging is to:

  • Enable virtual memory, allowing programs to run even if their entire size exceeds the available physical memory.
  • Eliminate external fragmentation.
  • Simplify memory management for the operating system.

When a process runs with traditional paging, its pages might be loaded into main memory either entirely at startup or in larger blocks, potentially bringing in data that isn't immediately required.

Understanding Demand Paging

Demand paging is a variation of paging that implements a "lazy loading" strategy. Demand paging is a method in which a page is only brought into main memory when the CPU requests it. This means that instead of loading an entire process or large portions of it into memory at once, only those pages are loaded directly required by the operation at a given moment. The remaining pages stay on disk (in secondary storage) until they are explicitly referenced.

Key aspects of demand paging:

  • Reduced I/O: Less time is spent swapping pages in and out of memory that might never be used.
  • Faster Process Startup: Processes can begin execution much quicker because only a minimal set of pages (often just the initial code segment) needs to be loaded.
  • Lower Memory Requirements: Less physical memory is needed per process, allowing more processes to reside in memory concurrently, increasing multiprogramming levels.
  • Page Faults: If the CPU tries to access a page that is not currently in main memory, a "page fault" occurs. The operating system then handles this fault by:
    1. Determining the location of the required page on disk.
    2. Finding a free frame in main memory.
    3. Reading the page from disk into the free frame.
    4. Updating the page table to reflect the new memory location.
    5. Restarting the instruction that caused the page fault.

Key Differences Between Paging and Demand Paging

While demand paging is an advanced form of paging, their operational mechanisms differ significantly.

Feature Paging (General) Demand Paging
Page Loading Pages can be loaded into main memory all at once, or in fixed-size chunks, regardless of immediate need. Pages are loaded into main memory only when requested by the CPU.
Memory Usage Potentially loads more pages than immediately necessary, leading to higher initial memory consumption. Loads only the essential pages, resulting in more efficient memory utilization.
I/O Operations More initial I/O as larger portions (or the whole process) are brought in. Less initial I/O; I/O occurs only on page faults for specific pages.
Process Startup Can be slower as more data needs to be loaded initially. Faster, as only the critically required pages are loaded first.
Page Faults Can still occur if a page is swapped out, but the primary loading strategy is not driven by demand. Core mechanism relies on page faults to trigger page loading.
Overhead Primarily page table management. Page table management plus page fault handling overhead.
Efficiency Good, but can be inefficient if many loaded pages are never used. Highly efficient in terms of memory usage and I/O; optimizes resource utilization.
Implementation A foundational memory management technique. An advanced and optimized implementation strategy within paging.

Practical Insights

  • Modern Operating Systems: Almost all modern operating systems, such as Windows, macOS, and Linux, use demand paging as their primary virtual memory implementation strategy.
  • Benefits for Multitasking: Demand paging is crucial for multitasking environments, as it allows more processes to be active by reducing the physical memory footprint of each.
  • Performance Trade-offs: While demand paging is highly efficient, frequent page faults (known as "thrashing") can occur if the system doesn't have enough physical memory or if programs exhibit poor locality of reference. This can lead to significant performance degradation due to constant disk I/O.

In essence, paging provides the framework for virtual memory, while demand paging refines that framework to ensure that physical memory is used as judiciously as possible, improving overall system performance and resource utilization.