Segmentation vs. Paging: Key Differences Explained

In computer science and operating systems, segmentation is a memory management technique that divides a program’s address space into variable-size blocks called segments, each associated with a logical function or data category (e.g., code segment, data segment, stack segment, heap segment). Unlike paging (which uses fixed-size units), segmentation maps these logical segments to non-contiguous regions of physical memory, enabling direct alignment with the program’s natural structure.

Core Principles

  1. Logical Address StructureA virtual address in a segmented system consists of two parts:
    • Segment Number (S): Identifies the logical segment (e.g., code, data, stack).
    • Offset (O): Specifies the position of the target byte within the segment, ranging from 0 to the maximum size of the segment.The operating system maintains a Segment Table for each process, where each entry stores the base address (starting physical address of the segment in RAM) and the limit (maximum size of the segment) of a corresponding logical segment.
  2. Address Translation Process
    1. The CPU extracts the segment number and offset from the virtual address.
    2. It looks up the segment table using the segment number to get the base address and limit of the target segment.
    3. It checks if the offset exceeds the segment limit: if yes, a segmentation fault (an exception indicating invalid memory access) is triggered.
    4. If valid, the physical address is calculated as: Physical Address = Base Address + Offset.
  3. Segment Types & CharacteristicsSegments are designed based on the program’s runtime needs, with distinct access permissions and growth directions:
    • Code Segment (Text Segment): Stores executable instructions, marked as read-only to prevent accidental modification.
    • Data Segment: Holds global and static variables, divided into initialized data (e.g., constants) and uninitialized data (BSS segment).
    • Stack Segment: Manages function calls, local variables, and return addresses; grows downward (from high to low memory addresses).
    • Heap Segment: Allocates dynamic memory during program execution; grows upward (from low to high memory addresses).

Key Advantages

  • Logical Memory Isolation: Segments correspond to the program’s logical components, making it easy to set access permissions (e.g., read-only for code segments) and prevent unauthorized operations (e.g., writing to code segments).
  • No Internal Fragmentation: Since segments have variable sizes that match the actual needs of logical components, there is no unused space inside a segment (unlike paging, which causes internal fragmentation due to fixed page sizes).
  • Support for Dynamic Growth: Segments like the stack and heap can grow or shrink dynamically as the program runs, with the OS adjusting the segment limit accordingly.

Limitations

  • External Fragmentation: As segments are allocated and freed, non-contiguous free blocks of physical memory accumulate. These blocks are too small to hold new segments individually but sum up to a sufficient size, wasting physical memory space.
  • Complex Management: Variable-size segments increase the difficulty of memory allocation and deallocation compared to fixed-size paging; the OS needs to use algorithms like best-fitfirst-fit, or worst-fit to manage free memory blocks.

Segmentation vs. Paging

FeatureSegmentationPaging
Unit SizeVariable (matches logical components)Fixed (e.g., 4KB, 8KB)
Address StructureSegment Number + OffsetPage Number + Offset
FragmentationExternal fragmentation onlyInternal fragmentation only
Alignment with ProgramMatches logical structure directlyNo direct logical association

Hybrid Approach: Segmented Paging

To combine the advantages of both techniques, modern operating systems (e.g., x86 architecture) often use segmented paging:

This approach eliminates external fragmentation (via paging) and retains logical isolation (via segmentation).

The program’s address space is first divided into logical segments.

Each segment is further divided into fixed-size pages.



了解 Ruigu Electronic 的更多信息

订阅后即可通过电子邮件收到最新文章。

Posted in

Leave a comment