Segmentation
Variable-sized logical regions (code, heap, stack) with base and limit.
Level: intermediate
What is it?
Segmentation divides a process's logical address space into variable-sized regions such as code, data, stack, or independently managed modules. A logical address identifies a segment and an offset; hardware checks the offset against the segment limit and adds the base to form a physical address.
Why does it matter?
Segments match a program's logical structure and can support distinct access permissions or sharing. Their variable sizes make allocation flexible, but freeing and growing segments can leave external fragmentation in physical memory.
Mental model
Each segment is a bounded region with a base address and a length. Translation is valid only if the offset lies within the limit; a valid logical name cannot access an arbitrary physical address.
How it works
- A segmented address is commonly represented as (segment selector, offset).
- The segment descriptor supplies a base, limit, and permission attributes.
- An offset outside the limit triggers a protection fault rather than silently accessing neighboring memory.
- Variable-sized allocations can leave external fragmentation even if total free memory is sufficient.
- Paging uses fixed-size pages/frames and avoids external fragmentation in physical allocation at the cost of page-table overhead.
Steps
- The process generates a logical address referring to its data segment.
- The MMU checks the offset against the segment's limit and checks access permissions.
- It adds the segment base to the offset to obtain the physical location.
- If bounds or permissions fail, the processor raises a protection exception.
Real-world usage
- Comparing segmentation and paging in OS coursework.
- Understanding memory protection across code, data, and stack regions.
- Explaining external fragmentation and compaction.
Common mistakes
- Calling segmentation and paging the same translation model.
- Ignoring the bounds check and describing only base-plus-offset addition.
- Assuming total free memory guarantees a large contiguous segment can be allocated.
Interview questions
Why can segmentation suffer external fragmentation?
Segments have variable sizes, so free physical space can be split into holes that do not fit a requested large segment.
What are two benefits of segmentation?
It matches logical program regions and can apply different sharing or protection attributes to each segment.