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:
Illustrative Numeric Example:
If a user program generates a logical address of
and the relocation register is loaded with, the MMU performs the additionto output the physical memory address.

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, andWindows.These systems typically do not swap entire processes by default; instead, swapping is activated under memory pressure or integrated directly with paging mechanisms.

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
.If the condition is false (
), 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:

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:
Memory contains the operating system (
OS),process 5,process 8, andprocess 2.process 8completes and terminates, freeing its partition and creating an empty hole betweenprocess 5andprocess 2.process 9arrives with a memory requirement smaller than the free hole. It is allocated memory adjacent toprocess 5, leaving a reduced free hole below it.process 10arrives and is allocated from the remaining hole space, situated betweenprocess 9andprocess 2, leaving an even smaller residual hole.
Dynamic Storage-Allocation Problem:
Addresses how to satisfy a memory request of size
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 (
).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 (
).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 (
).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
, 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
, typically ranging between() and().Pages: Logical memory is partitioned into blocks of the exact same size as physical frames.
To execute a program of size
pages, the operating system must locatefree 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 (): Used as an index into the process’s page table.
Page Offset (): 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
holds the corresponding physical frame number ().The hardware translates the logical address
into the physical address by replacing the page numberwith the base frame number, producing.The physical address dispatched to memory addresses the frame starting at
throughbased on offset.

Paging Mapping Examples:
Simple Process Model:
Consider a process with
logical pages (,,,) mapped into physical memory containingframes (through):Page Table Entry
maps to Frame().Page Table Entry
maps to Frame().Page Table Entry
maps to Frame().Page Table Entry
maps to Frame().
Physical frames
,,, andremain unallocated.

Detailed Byte-Level Addressing Example:
Logical memory consists of
containing characters'a'through'p'at logical addressesthrough.Page size is configured to
( requiresbits; requiresbits):Page
(addresses): contains'a','b','c','d'.Page
(addresses): contains'e','f','g','h'.Page
(addresses): contains'i','j','k','l'.Page
(addresses): contains'm','n','o','p'.Page Table Configuration:
Entry
Entry
Entry
Entry
Physical Memory Layout:
Frame
(base physical address): contains'i','j','k','l'.Frame
(base physical address): contains'm','n','o','p'.Frame
(base physical address): contains'a','b','c','d'.Frame
(base physical address): contains'e','f','g','h'.Frames at physical addresses
,,, andare free.

Free-Frame Management:
Before process allocation:
Free-frame list contains available physical frames:
,,,,.A new process arrives on the backing store requiring
pages:,,,.After allocation:
Frames
,,, andare allocated from the head of the free-frame list.New-process page table is established:
Page
Page
Page
Page
Free-frame list is updated to contain only frame
.

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 (
,,) share a common three-page text editor (\text{ed 1},\text{ed 2},\text{ed 3}) while retaining private data:Process
Page Table: maps pagesto frames(\text{editor}), and pageto frame(\text{data 1}).Process
Page Table: maps pagesto frames(\text{editor}), and pageto frame(\text{data 2}).Process
Page Table: maps pagesto frames(\text{editor}), and pageto frame(\text{data 3}).
Physical memory holds only a single copy of
\text{ed 1}in frame,\text{ed 2}in frame, and\text{ed 3}in frame, dramatically reducing aggregate memory consumption.

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

Segmentation Addressing Architecture:
A logical address under segmentation consists of a two-tuple:
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
.The segment number
is used as an index into the segment table.The hardware evaluates the offset against the limit register:
.If
, an illegal memory reference has occurred, causing the hardware to generate a trap; addressing error.If
, the address is valid, and the hardware computes the physical address:The resulting physical address is transmitted to the memory unit.

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:
(subroutine),(sqrt),(main program),(stack), and(symbol table).Segment Table Contents:
Entry
: Limit, Base(Physical range:)Entry
: Limit, Base(Physical range:)Entry
: Limit, Base(Physical range:)Entry
: Limit, Base(Physical range:)Entry
: Limit, Base(Physical range:)Unused physical memory intervals (holes) reside between
and.

Sharing of Segments Example:
Processes
andshare an editor code segment while executing independent data segments:Process Segment Table:
Segment
(\text{editor}): Limit, BaseSegment
(\text{data 1}): Limit, Base
Process Segment Table:
Segment
(\text{editor}): Limit, BaseSegment
(\text{data 2}): Limit, Base
Physical Memory Allocation:
Address range
(): Shared\text{editor}Address range
():\text{data 1}belonging toAddress range
():\text{data 2}belonging toIntervening regions (
and beyond) represent free memory partitions.

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
MULTICSoperating 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:
The segment number indexes the segment table to locate the segment's page table.
The segment offset is partitioned into a page number and a page offset.
The page number indexes the segment's page table to retrieve the physical frame number.
The physical frame number is combined with the page offset to produce the final physical memory address.