Segmentation
Scenario
Paging treats memory like a giant grid of equal-sized boxes, blindly chopping functions and arrays into pieces. Segmentation maps memory the way a programmer actually thinks about it.
Mental model
Paging is cutting a novel into exactly 500-word pages, splitting sentences in half. Segmentation is binding the novel by chapters, regardless of their length.
Segmentation divides memory into logically distinct, variable-sized units (segments) like 'code', 'data', and 'stack', mapping closely to the programmer's logical view of the program.
Explanation
While paging is excellent for preventing external fragmentation, it completely ignores the logical structure of a program. Paging divides memory into rigid, fixed-size blocks, meaning a single continuous array or function might be fractured across multiple random frames in memory. Segmentation takes the opposite approach: it divides the process into variable-sized segments that correspond to actual logical entities, such as the main program, standard library routines, symbol tables, the stack, and specific data arrays.
In a segmented memory system, a logical address consists of two parts: a segment number and an offset within that segment. When a program tries to access a variable, it specifies which segment the variable is in and how far into that segment it lives. The hardware uses a Segment Table, which keeps track of the physical starting address (base) and the exact size (limit) of each segment.
When an address is translated, the CPU first checks if the requested offset is greater than the segment's limit. If it is, this means the program is trying to access memory outside of its allowed boundaries, and the hardware triggers a segmentation fault (the infamous 'segfault'). If the offset is valid, the hardware simply adds the offset to the segment's base address to find the exact physical location in memory. This inherent limit-checking provides excellent memory protection and prevents buffer overflows from spilling into other segments.
However, segmentation has a major drawback: because segments are variable in size, loading and removing them from memory leaves behind irregularly sized gaps of free space. Over time, these gaps become too small to hold new segments, leading to severe external fragmentation. Modern operating systems usually solve this by combining both concepts into 'paged segmentation', where logically variable segments are broken down into fixed-size pages underneath the hood, giving the logical benefits of segmentation with the memory-management benefits of paging.
Key points
- Variable Size: Unlike pages (which are always a fixed size), segments grow and shrink based on the actual logical size of the data structure or code module they contain.
- Segment Table: Contains base (starting physical address) and limit (length of the segment). Both are required because segments vary in size.
- Protection: Segmentation makes it trivial to mark the 'code' segment as read-only and the 'data' segment as read-write, preventing accidental code modification.
Common mistakes
- Assuming modern OSs use pure segmentation: This is a common source of confusion.
- Confusing internal and external fragmentation: This is a common source of confusion.
Glossary
- segmentation
- A memory management technique that groups related parts of a program together (like keeping all the code here and all the variables there) instead of just slicing it blindly.
- logical address space
- The imaginary, neat memory layout a program thinks it has, hiding the fact that its data is actually scattered all over the physical RAM chips.
- segment table
- A directory maintained by the operating system that translates a program's imaginary memory addresses into the actual, physical locations in RAM.
Recall questions
- What two values does a segment table entry hold?
- Why does segmentation cause external fragmentation?
- What is the primary advantage of segmentation over paging?
Questions & answers
A segment has a base of 1000 and a limit of 500. A logical address is generated with segment offset 530. What happens?
A trap / segmentation fault is generated.
Approach: The offset (530) must be strictly less than the limit (500). Since it exceeds the limit, it is an illegal memory access.
Compare paging and segmentation with respect to fragmentation.
Paging causes internal fragmentation; segmentation causes external fragmentation.
Approach: Fixed-size chunks (pages) leave unused space inside the chunk. Variable-sized chunks (segments) leave unused space between the chunks.
Continue learning
Previous: Paging & Page Tables