Show understanding of virtual memory, paging and segmentation for memory management

Published by Patrick Mutisya · 14 days ago

Cambridge A-Level Computer Science – 16.1 Purposes of an Operating System (OS)

16.1 Purposes of an Operating System (OS)

Memory Management Overview

An operating system must provide each running program with the illusion of a large, contiguous block of memory, even though the physical memory is limited and fragmented. This is achieved through virtual memory, which abstracts the actual hardware resources and allows the OS to:

  • Isolate processes from each other for security and stability.
  • Allow programs to use more memory than is physically available.
  • Facilitate efficient use of RAM by loading only the required portions of a program.
  • Support multitasking by swapping inactive pages to secondary storage.

Virtual Memory

Virtual memory extends the address space of a process by mapping virtual addresses to physical addresses. The mapping is performed by the Memory Management Unit (MMU) using structures such as page tables.

The relationship between a virtual address \$V\$, page number \$p\$, offset \$d\$, and page size \$S\$ can be expressed as:

\$V = p \times S + d\$

When a program accesses \$V\$, the MMU translates it to a physical address \$P\$ using the page table entry for \$p\$.

Paging

Paging divides both virtual and physical memory into fixed‑size blocks called pages (virtual) and frames (physical). Typical page sizes are 4 KB or 8 KB.

  • Advantages

    • Simple allocation – any free frame can hold any page.
    • Eliminates external fragmentation.
    • Easy to implement swapping and demand paging.

  • Disadvantages

    • Internal fragmentation if a process does not use the whole page.
    • Overhead of maintaining page tables.

Segmentation

Segmentation divides a program’s address space into variable‑length logical units called segments (e.g., code, stack, heap). Each segment has a base address and a limit.

The translation from a logical address \$(s, o)\$, where \$s\$ is the segment number and \$o\$ the offset within the segment, to a linear address \$L\$ is:

\$L = \text{Base}s + o \quad \text{provided } o < \text{Limit}s\$

Segmentation provides a more natural view of program structure but can suffer from external fragmentation.

Comparison of Paging and Segmentation

AspectPagingSegmentation
Unit sizeFixed (e.g., 4 KB)Variable (defined by programmer)
Fragmentation typeInternal fragmentation onlyExternal fragmentation possible
Address translationPage number → frame number via page tableSegment number → base address via segment table
ProtectionSame protection for whole pageProtection can differ per segment
Typical useCore memory management in most modern OSesOften combined with paging (paged segmentation) or used in systems emphasizing logical structure

Combined Paging and Segmentation

Many modern operating systems use a hybrid approach: a program is divided into segments, each of which is further divided into pages. This combines the logical organization of segmentation with the allocation efficiency of paging.

Suggested diagram: A two‑level address translation showing a segment table pointing to page tables, which then map to physical frames.

Key Points to Remember

  1. Virtual memory gives each process its own address space, independent of physical memory limits.
  2. Paging eliminates external fragmentation but can waste space inside pages.
  3. Segmentation reflects the logical structure of programs but may lead to external fragmentation.
  4. Hybrid schemes exploit the strengths of both techniques.