Comprehensive Notes on Memory Management: Paging, Segmentation, Swapping, and Address Binding

Logical Versus Physical Address Space

  • Core Conceptual Distinction:

    • Logical Address (Virtual Address): An address generated directly by the CPU during program execution. The collection of all logical addresses generated by a program constitutes its logical address space.

    • Physical Address: The actual hardware memory location loaded into and recognized by the memory unit (memory bus). The collection of all physical addresses corresponding to the logical addresses constitutes the physical address space.

    • Separating the user’s logical address space from the physical memory space is the fundamental prerequisite for modern memory management, virtual memory, and process isolation.

  • Address-Binding Schemes and Address Equivalence:

    • Compile-Time Binding: If it is known at compile time where the process will reside in physical memory, absolute code can be generated. Logical and physical addresses are identical under compile-time binding schemes.

    • Load-Time Binding: If the physical memory location is not known at compile time, the compiler must generate relocatable code. The final physical addresses are bound when the program is loaded into memory. Logical and physical addresses are identical under load-time address-binding schemes.

    • Execution-Time (Run-Time) Binding: If a process can be moved from one memory segment to another during its execution, address binding must be delayed until execution time. In execution-time address-binding schemes, logical (virtual) addresses and physical addresses differ.

  • Memory-Management Unit (MMU):

    • The MMU is a dedicated hardware device responsible for translating logical (virtual) addresses to physical memory addresses at runtime.

    • In a basic MMU dynamic relocation scheme, a base register—known specifically as a relocation register—is utilized.

    • Address Relocation Mechanism:

    • The user process interacts exclusively with logical addresses and never sees the real physical memory addresses.

    • The value stored in the relocation register is added to every logical address generated by the user process at the exact moment the address is dispatched to the memory bus:       Physical Address=Logical Address+Relocation Register Value\text{Physical Address} = \text{Logical Address} + \text{Relocation Register Value}

    • Illustrative Numeric Example:

      • If a user program generates a logical address of 346346 and the relocation register is loaded with 1400014000, the MMU performs the addition 14000+34614000 + 346 to output the physical memory address 1434614346.

Dynamic relocation using a relocation register

Swapping

  • Fundamental Swapping Mechanism:

    • A process can be temporarily swapped out of main memory to a secondary storage location known as a backing store, making physical memory available to other processes.

    • The swapped-out process is later swapped back into main memory to resume its execution.

  • Backing Store Architecture:

    • Must consist of a fast disk or secondary storage system sufficiently large to accommodate copies of all memory images for all active users.

    • Must provide direct access to these memory images to facilitate rapid loading and retrieval.

  • Roll Out, Roll In Variant:

    • A specialized swapping variant used in conjunction with priority-based CPU scheduling algorithms.

    • If a higher-priority process arrives and requires immediate execution, a lower-priority process currently in memory is swapped out (rolled out).

    • The higher-priority process is loaded into memory (rolled in) and executed to completion or preemption, after which the lower-priority process can be swapped back in to continue.

  • Performance and Overhead Characteristics:

    • The dominant component of swapping time is transfer time.

    • The total transfer time is directly proportional to the amount of memory swapped.

    • For example, swapping an entire process image of several hundred megabytes incurs substantial I/O overhead relative to standard memory operations.

  • Operating System Implementations:

    • Modified, optimized forms of swapping are utilized in modern operating systems, including UNIX, Linux, and Windows.

    • These systems typically do not swap entire processes by default; instead, swapping is activated under memory pressure or integrated directly with paging mechanisms.

Schematic view of process swapping between main memory and backing store

Contiguous Allocation

  • Memory Partitioning Structure:

    • In contiguous allocation, each process is contained in a single contiguous section of physical memory.

    • Main memory is traditionally organized into two primary partitions:

    • Resident Operating System: Usually held in low memory addresses along with the interrupt vector to optimize interrupt latency.

    • User Processes: Allocated in high memory addresses, isolated from the kernel.

  • Single-Partition Allocation and Hardware Protection:

    • Protects user processes from each other and protects operating-system code and data from modification by user programs.

    • Implemented using two hardware registers:

    • Relocation Register: Contains the value of the smallest physical address of the allocated partition.

    • Limit Register: Contains the range or total size of the logical address space allocated to the process.

    • Hardware Verification Sequence:

    • When the CPU generates a logical address, the hardware first evaluates whether logical address<limit register\text{logical address} < \text{limit register}.

    • If the condition is false (logical address≥limit register\text{logical address} \ge \text{limit register}), the hardware generates a trap; addressing error, aborting the illegal memory access.

    • If the condition is true, the logical address is added to the value in the relocation register to yield the valid physical address sent to memory:       Physical Address=Relocation Register+Logical Address\text{Physical Address} = \text{Relocation Register} + \text{Logical Address}

Hardware support for relocation and limit registers
  • Multiple-Partition Allocation:

    • Hole: A contiguous block of free, available memory. Throughout system operation, holes of varying sizes become scattered across physical memory.

    • When a new process arrives, the operating system searches the available memory for a hole large enough to accommodate the process.

    • If an adequate hole is found, memory is allocated to the process, and any unallocated space within that block remains as a smaller hole.

    • The operating system continuously maintains data structures tracking:

    • Allocated partitions.

    • Free partitions (holes).

    • Dynamic Partitioning Lifecycle Example:

    1. Memory contains the operating system (OS), process 5, process 8, and process 2.

    2. process 8 completes and terminates, freeing its partition and creating an empty hole between process 5 and process 2.

    3. process 9 arrives with a memory requirement smaller than the free hole. It is allocated memory adjacent to process 5, leaving a reduced free hole below it.

    4. process 10 arrives and is allocated from the remaining hole space, situated between process 9 and process 2, leaving an even smaller residual hole.

  • Dynamic Storage-Allocation Problem:

    • Addresses how to satisfy a memory request of size nn from an existing list of free holes.

    • First-Fit Algorithm:

    • Scans the list of free holes from the beginning and allocates the first hole that is big enough (size≥n\text{size} \ge n).

    • Search can terminate as soon as an adequate hole is found.

    • Best-Fit Algorithm:

    • Allocates the smallest hole that is big enough to satisfy the request (min⁡(size≥n)\min(\text{size} \ge n)).

    • Requires searching the entire list of holes, unless the list is ordered by hole size.

    • Produces the smallest leftover hole.

    • Worst-Fit Algorithm:

    • Allocates the largest available hole across memory (max⁡(size)\max(\text{size})).

    • Requires searching the entire list, unless sorted by size.

    • Produces the largest leftover hole, theoretically allowing other processes to utilize the remaining space.

    • Algorithm Performance Comparison:

    • Both first-fit and best-fit are proven to be superior to worst-fit in terms of execution speed and memory storage utilization.

  • Memory Fragmentation:

    • External Fragmentation:

    • Total free memory space across the system is sufficient to satisfy an allocation request of size nn, but the space is divided into scattered fragments and is not contiguous.

    • Severe external fragmentation can prevent processes from being loaded despite an abundance of cumulative free memory.

    • Mitigation via Compaction:

      • Compaction relocates processes in physical memory to consolidate all free memory into a single large contiguous block.

      • Compaction is possible only if address relocation is dynamic and executed at runtime via relocation registers.

    • Internal Fragmentation:

    • Allocated physical memory is slightly larger than the requested logical memory.

    • Occurs when memory is allocated in fixed-size blocks or standard increments.

    • The unused difference remains internal to the allocated partition and cannot be used by any other process.

Paging

  • Paging Principles and Block Dimensions:

    • Paging is a memory-management scheme that permits the physical address space of a process to be noncontiguous.

    • Eliminates the need to find contiguous physical memory blocks of arbitrary size.

    • Frames: Physical memory is broken into fixed-sized blocks called frames. The frame size is defined as a power of 22, typically ranging between 512 bytes512\,\text{bytes} (29 bytes2^9\,\text{bytes}) and 8192 bytes8192\,\text{bytes} (213 bytes2^{13}\,\text{bytes}).

    • Pages: Logical memory is partitioned into blocks of the exact same size as physical frames.

    • To execute a program of size nn pages, the operating system must locate nn free frames in physical memory and load the pages into those frames.

    • The operating system tracks all free physical frames via a free-frame list.

    • Fragmentation Profile: Paging eliminates external fragmentation entirely. However, it is subject to internal fragmentation within the final allocated page of a process if the total process size is not an exact multiple of the page size.

  • Address Translation Scheme:

    • A logical address generated by the CPU is divided into two distinct components:

    • Page Number (pp): Used as an index into the process’s page table.

    • Page Offset (dd): Defines the specific location within the referenced page.

    • Page Table Operation:

    • The page table maintains the mapping between logical pages and physical memory frames.

    • Each entry indexed by pp holds the corresponding physical frame number (ff).

    • The hardware translates the logical address ⟨p,d⟩\langle p, d \rangle into the physical address by replacing the page number pp with the base frame number ff, producing ⟨f,d⟩\langle f, d \rangle.

    • The physical address dispatched to memory addresses the frame starting at f0000…0000f0000\dots0000 through f1111…1111f1111\dots1111 based on offset dd.

Paging address translation architecture
  • Paging Mapping Examples:

    • Simple Process Model:

    • Consider a process with 44 logical pages (page 0\text{page } 0, page 1\text{page } 1, page 2\text{page } 2, page 3\text{page } 3) mapped into physical memory containing 88 frames (00 through 77):

      • Page Table Entry 00 maps to Frame 11 (page 0→frame 1\text{page } 0 \rightarrow \text{frame } 1).

      • Page Table Entry 11 maps to Frame 44 (page 1→frame 4\text{page } 1 \rightarrow \text{frame } 4).

      • Page Table Entry 22 maps to Frame 33 (page 2→frame 3\text{page } 2 \rightarrow \text{frame } 3).

      • Page Table Entry 33 maps to Frame 77 (page 3→frame 7\text{page } 3 \rightarrow \text{frame } 7).

    • Physical frames 00, 22, 55, and 66 remain unallocated.

Paging model of logical and physical memory
  • Detailed Byte-Level Addressing Example:

    • Logical memory consists of 16 bytes16\,\text{bytes} containing characters 'a' through 'p' at logical addresses 00 through 1515.

    • Page size is configured to 4 bytes4\,\text{bytes} (dd requires 22 bits; pp requires 22 bits):

    • Page 00 (addresses 0−30-3): contains 'a', 'b', 'c', 'd'.

    • Page 11 (addresses 4−74-7): contains 'e', 'f', 'g', 'h'.

    • Page 22 (addresses 8−118-11): contains 'i', 'j', 'k', 'l'.

    • Page 33 (addresses 12−1512-15): contains 'm', 'n', 'o', 'p'.

    • Page Table Configuration:

    • Entry 0→50 \rightarrow 5

    • Entry 1→61 \rightarrow 6

    • Entry 2→12 \rightarrow 1

    • Entry 3→23 \rightarrow 2

    • Physical Memory Layout:

    • Frame 11 (base physical address 44): contains 'i', 'j', 'k', 'l'.

    • Frame 22 (base physical address 88): contains 'm', 'n', 'o', 'p'.

    • Frame 55 (base physical address 2020): contains 'a', 'b', 'c', 'd'.

    • Frame 66 (base physical address 2424): contains 'e', 'f', 'g', 'h'.

    • Frames at physical addresses 00, 1212, 1616, and 2828 are free.

Paging example with specific memory addresses and byte contents
  • Free-Frame Management:

    • Before process allocation:

    • Free-frame list contains available physical frames: 1414, 1313, 1818, 2020, 1515.

    • A new process arrives on the backing store requiring 44 pages: page 0\text{page } 0, page 1\text{page } 1, page 2\text{page } 2, page 3\text{page } 3.

    • After allocation:

    • Frames 1414, 1313, 1818, and 2020 are allocated from the head of the free-frame list.

    • New-process page table is established:

      • Page 0→Frame 140 \rightarrow \text{Frame } 14

      • Page 1→Frame 131 \rightarrow \text{Frame } 13

      • Page 2→Frame 182 \rightarrow \text{Frame } 18

      • Page 3→Frame 203 \rightarrow \text{Frame } 20

    • Free-frame list is updated to contain only frame 1515.

Free frames allocation before and after loading a new process
  • Shared Pages Architecture:

    • Reentrant Code (Pure Code):

    • Paging allows the sharing of common code across multiple processes.

    • Code must be non-self-modifying (read-only/reentrant), so it never alters during execution.

    • Examples of shared code: text editors, compilers, runtime libraries, and windowing systems.

    • Logical Positioning Rule: Shared code must appear in the exact same logical address space location across all participating processes.

    • Concrete Shared Pages Configuration:

    • Three independent processes (P1P_1, P2P_2, P3P_3) share a common three-page text editor (\text{ed 1}, \text{ed 2}, \text{ed 3}) while retaining private data:

      • Process P1P_1 Page Table: maps pages 0,1,20, 1, 2 to frames 3,4,63, 4, 6 (\text{editor}), and page 33 to frame 11 (\text{data 1}).

      • Process P2P_2 Page Table: maps pages 0,1,20, 1, 2 to frames 3,4,63, 4, 6 (\text{editor}), and page 33 to frame 77 (\text{data 2}).

      • Process P3P_3 Page Table: maps pages 0,1,20, 1, 2 to frames 3,4,63, 4, 6 (\text{editor}), and page 33 to frame 22 (\text{data 3}).

    • Physical memory holds only a single copy of \text{ed 1} in frame 33, \text{ed 2} in frame 44, and \text{ed 3} in frame 66, dramatically reducing aggregate memory consumption.

Sharing of pure code pages in a paging environment

Segmentation

  • User-Centric Memory Architecture:

    • Programmers do not perceive memory as a linear array of bytes; rather, they view a program as a collection of modular, logical units.

    • Segmentation is a memory-management scheme designed to support this user view of memory.

    • A program is defined as a collection of logical segments, which may vary in size.

    • Common segment components include:

    • Main program

    • Procedures and functions

    • Methods

    • Common blocks

    • Stack

    • Symbol table

    • Global or local arrays

User view of a program as a collection of segments
  • Segmentation Addressing Architecture:

    • A logical address under segmentation consists of a two-tuple:     ⟨segment-number,offset⟩  ⟹  ⟨s,d⟩\langle \text{segment-number}, \text{offset} \rangle \implies \langle s, d \rangle

    • Segment Table:

    • Maps user-defined two-dimensional logical addresses to physical memory addresses.

    • Each entry in the segment table consists of:

      • Base: The starting physical address where the segment resides in physical memory.

      • Limit: The length or size of the segment.

  • Hardware Translation and Protection Mechanism:

    • The CPU issues logical address ⟨s,d⟩\langle s, d \rangle.

    • The segment number ss is used as an index into the segment table.

    • The hardware evaluates the offset against the limit register: d<limitd < \text{limit}.

    • If d≥limitd \ge \text{limit}, an illegal memory reference has occurred, causing the hardware to generate a trap; addressing error.

    • If d<limitd < \text{limit}, the address is valid, and the hardware computes the physical address:       Physical Address=base+d\text{Physical Address} = \text{base} + d

    • The resulting physical address is transmitted to the memory unit.

Segmentation hardware with limit and base registers
  • Segmentation System Properties:

    • Relocation: Dynamic, performed at runtime by indexing into the segment table.

    • Allocation: Employs variable-partition dynamic storage allocation algorithms (such as first-fit or best-fit).

    • Fragmentation: Segmentation suffers from external fragmentation because segments vary in length and are allocated contiguously, causing holes to form as segments are created and destroyed.

    • Sharing: Segments can be shared among processes (e.g., shared subroutines or utilities). Shared segments must use the same segment number across participating processes.

  • Segmentation Example:

    • A program is composed of five segments in its logical address space: segment 0\text{segment } 0 (subroutine), segment 1\text{segment } 1 (sqrt), segment 2\text{segment } 2 (main program), segment 3\text{segment } 3 (stack), and segment 4\text{segment } 4 (symbol table).

    • Segment Table Contents:

    • Entry 00: Limit 10001000, Base 14001400 (Physical range: 1400−24001400 - 2400)

    • Entry 11: Limit 400400, Base 63006300 (Physical range: 6300−67006300 - 6700)

    • Entry 22: Limit 400400, Base 43004300 (Physical range: 4300−47004300 - 4700)

    • Entry 33: Limit 11001100, Base 32003200 (Physical range: 3200−43003200 - 4300)

    • Entry 44: Limit 10001000, Base 47004700 (Physical range: 4700−57004700 - 5700)

    • Unused physical memory intervals (holes) reside between 2400−32002400 - 3200 and 5700−63005700 - 6300.

Segmentation address mapping example with segment table and physical memory
  • Sharing of Segments Example:

    • Processes P1P_1 and P2P_2 share an editor code segment while executing independent data segments:

    • Process P1P_1 Segment Table:

      • Segment 00 (\text{editor}): Limit 2528625286, Base 4306243062

      • Segment 11 (\text{data 1}): Limit 44254425, Base 6834868348

    • Process P2P_2 Segment Table:

      • Segment 00 (\text{editor}): Limit 2528625286, Base 4306243062

      • Segment 11 (\text{data 2}): Limit 88508850, Base 9000390003

    • Physical Memory Allocation:

      • Address range 43062−6834843062 - 68348 (43062+2528643062 + 25286): Shared \text{editor}

      • Address range 68348−7277368348 - 72773 (68348+442568348 + 4425): \text{data 1} belonging to P1P_1

      • Address range 90003−9855390003 - 98553 (90003+885090003 + 8850): \text{data 2} belonging to P2P_2

      • Intervening regions (72773−9000372773 - 90003 and beyond 9855398553) represent free memory partitions.

Sharing of segments between two processes

Segmentation with Paging

  • Architectural Synthesis (MULTICS System):

    • Pure segmentation suffers from external fragmentation and lengthy search times when allocating variable-sized blocks.

    • Pure paging provides fixed-size allocation and avoids external fragmentation, but lacks a logical modular structure reflecting programmer intent.

    • The MULTICS operating system resolved both external fragmentation and lengthy search times by combining segmentation with paging: paging the segments.

  • Structural Differences from Pure Segmentation:

    • In pure segmentation, each entry in the segment table contains the actual base physical memory address of the segment.

    • In segmentation with paging, the segment table entry does not contain the base address of the segment in physical memory.

    • Instead, each segment table entry points directly to the base address of a page table assigned to that particular segment.

    • Logical address translation proceeds hierarchically:

    1. The segment number indexes the segment table to locate the segment's page table.

    2. The segment offset is partitioned into a page number and a page offset.

    3. The page number indexes the segment's page table to retrieve the physical frame number.

    4. The physical frame number is combined with the page offset to produce the final physical memory address.