0
0
Operating-systemsComparisonBeginner · 4 min read

Paging vs Segmentation: Key Differences and When to Use Each

In operating systems, paging divides memory into fixed-size blocks called pages, while segmentation divides memory into variable-sized segments based on logical divisions. Paging avoids external fragmentation but can cause internal fragmentation, whereas segmentation matches program structure but may suffer external fragmentation.
⚖️

Quick Comparison

This table summarizes the main differences between paging and segmentation in memory management.

FactorPagingSegmentation
Memory DivisionFixed-size pagesVariable-size segments
AddressingUses page number and offsetUses segment number and offset
FragmentationInternal fragmentation possibleExternal fragmentation possible
Logical ViewNo relation to program structureMatches logical program units
Protection & SharingLimited by page granularityEasier to protect/share segments
ComplexitySimpler hardware supportMore complex due to variable sizes
⚖️

Key Differences

Paging breaks physical memory into equal-sized blocks called pages and maps them to fixed-size frames in physical memory. This makes memory allocation simple and avoids external fragmentation, but can waste space inside pages if the data does not fill them completely, causing internal fragmentation.

Segmentation divides memory into segments that correspond to logical parts of a program, like functions or data arrays. These segments vary in size, which fits the program's structure better and allows easier sharing and protection of segments. However, because segments are variable-sized, it can cause external fragmentation where free memory is split into small unusable pieces.

Paging uses a two-part address (page number and offset) to locate data, while segmentation uses a segment number and offset. Paging is simpler to implement in hardware, while segmentation requires more complex management to handle variable sizes and fragmentation.

⚖️

Code Comparison

Example of how paging translates a logical address to a physical address using page number and offset.

python
def paging_address_translation(logical_address, page_size, page_table):
    page_number = logical_address // page_size
    offset = logical_address % page_size
    frame_number = page_table.get(page_number, None)
    if frame_number is None:
        return None  # Page fault
    physical_address = frame_number * page_size + offset
    return physical_address

# Example usage
page_table = {0: 5, 1: 9, 2: 3}  # Maps page numbers to frame numbers
logical_address = 1026  # Suppose page size is 1024 bytes
page_size = 1024
physical_address = paging_address_translation(logical_address, page_size, page_table)
print(physical_address)
Output
9226
↔️

Segmentation Equivalent

Example of how segmentation translates a logical address using segment number and offset.

python
def segmentation_address_translation(logical_address, segment_table):
    segment_number, offset = logical_address
    segment = segment_table.get(segment_number, None)
    if segment is None or offset >= segment['length']:
        return None  # Segment fault
    physical_address = segment['base'] + offset
    return physical_address

# Example usage
segment_table = {
    0: {'base': 1000, 'length': 500},
    1: {'base': 2000, 'length': 1000},
    2: {'base': 4000, 'length': 1500}
}
logical_address = (1, 300)  # Segment 1, offset 300
physical_address = segmentation_address_translation(logical_address, segment_table)
print(physical_address)
Output
2300
🎯

When to Use Which

Choose paging when you want simple, uniform memory management that avoids external fragmentation and when hardware support for fixed-size pages is available. It is ideal for systems where memory protection and sharing at a fine granularity are less critical.

Choose segmentation when you need to reflect the logical structure of programs in memory, such as separating code, data, and stack segments. It is better when you want easier sharing and protection of logical units but can handle the complexity of managing variable-sized memory blocks and external fragmentation.

Key Takeaways

Paging divides memory into fixed-size pages, avoiding external fragmentation but causing internal fragmentation.
Segmentation divides memory into variable-sized segments matching program structure but may cause external fragmentation.
Paging uses page number and offset for addressing; segmentation uses segment number and offset.
Paging is simpler and widely supported by hardware; segmentation offers better logical memory organization.
Use paging for uniform memory management and segmentation for logical program division and protection.