đ Segmentation
Segmentation divides memory into variable-sized logical units (segments) that correspond to program structure: code, data, stack, and heap.
Memory Segments
Logical Division
Code (Text)
Executable instructions. Read-only, fixed size. Shared between processes for memory efficiency.
Data
Global and static variables. Read-write. Includes BSS (uninitialized data, zero-filled).
Stack
Function call frames, local variables, return addresses. Grows downward. LIFO structure per thread.
Heap
Dynamically allocated memory (malloc/new). Grows upward. Shared across threads of a process.
Segment Table & Address Translation
Address Format
Segment Table
| Seg # | Name | Base | Limit | Prot |
|---|---|---|---|---|
| 0 | Code | 0x10000 | 0x3000 | R |
| 1 | Data | 0x20000 | 0x4000 | R/W |
| 2 | Stack | 0x30000 | 0x1000 | R/W |
| 3 | Heap | 0x40000 | 0x2000 | R/W |
đą Interactive Translation
Segmentation with Paging (Seg-Paged)
Hybrid Approach
Segment # + Offset within segment
Look up segment base + limit. Check offset ⤠limit.
Base + Offset = linear address (intermediate)
Linear address â Page # + Offset â Page walk
Frame # + Offset â actual RAM access
Benefits
- â Each segment is paged â no external fragmentation
- â Segments provide logical structure and protection
- â Page sharing within segments is possible
- â Combined: best of both worlds
Drawbacks
- â Two levels of indirection = more TLB/page table lookups
- â More complex hardware (MMU must handle both)
- â Segment limits can waste page table entries
- â Variable segment sizes complicate OS management
Protection & Sharing
Protection
Each segment has protection bits in the segment descriptor:
Sharing
Multiple processes can share the same segment via their segment tables:
- â Shared libraries (.so/.dll) loaded as shared code segments
- â Each process' segment table entry points to same physical segment
- â Protection bits can make shared segment read-only
- â Saves significant memory (e.g., libc shared across 100 processes)
Segmentation vs Paging
| Property | Paging | Segmentation |
|---|---|---|
| Size | Fixed-size pages | Variable-sized segments |
| View | Hardware-oriented | Programmer-oriented |
| Fragmentation | Internal (wasted space within page) | External (gaps between segments) |
| Protection | Per-page bits | Per-segment descriptor |
| Sharing | Shared as pages (coarse) | Shared as segments (logical units) |
| Table Size | Large (one entry per page) | Small (one entry per segment) |
| Addressing | (page#, offset) | (segment#, offset) |
| Fault Handling | Page fault | Segmentation fault (segviolation) |
Code Example: Segment-Based Protection
python
class SegmentTableEntry:
def __init__(self, base, limit, prot):
self.base = base
self.limit = limit
self.prot = prot # "R", "RW", "RX", "RWX"
def translate(segment_table, seg_num, offset):
if seg_num < 0 or seg_num >= len(segment_table):
raise SegmentationFault("Invalid segment")
entry = segment_table[seg_num]
if offset > entry.limit:
raise SegmentationFault(
f"Offset {offset} exceeds limit {entry.limit}"
)
physical = entry.base + offset
return physical
# Example usage
seg_table = [
SegmentTableEntry(0x10000, 0x3000, "RX"), # Code
SegmentTableEntry(0x20000, 0x4000, "RW"), # Data
SegmentTableEntry(0x30000, 0x1000, "RW"), # Stack
]
# Access code segment, offset 0x100
addr = translate(seg_table, 0, 0x100) # â 0x10100
# Access beyond limit -> Segmentation fault!
try:
addr = translate(seg_table, 0, 0x4000)
except SegmentationFault as e:
print(f"Fault: {e}")Interview Questions
What is segmentation and how does it differ from paging?
Segmentation divides memory into variable-sized logical units (code, data, stack, heap) that match program structure. Paging divides memory into fixed-size pages. Segmentation has no internal fragmentation (segments are exactly sized) but suffers external fragmentation. Paging has internal fragmentation but eliminates external fragmentation. Most modern systems use both (seg-paged).
How does segmentation provide memory protection?
Each segment descriptor in the segment table contains a base address, limit (size), and protection bits (R/W/X). The MMU checks every access: if offset > limit, a segmentation fault is raised. This prevents a program from accessing memory outside its segments. Protection bits prevent code segments from being written, stack from being executed, etc.
What is the advantage of combining segmentation with paging?
Seg-Paged combines the logical structure of segments (each segment corresponds to a program section) with the fragmentation-free management of paging. Each segment is divided into pages. Address translation: virtual â (segment table) â linear address â (page tables) â physical. Used in x86-64 architecture. Provides flexible protection, sharing, and efficient memory use.
Explain the difference between a segmentation fault and a page fault.
A segmentation fault occurs when a program accesses memory outside its valid segment (offset > limit) or violates protection (writing to read-only segment). It is a programming error that typically kills the process. A page fault occurs when a valid page is not in physical memory â the OS handles it transparently by loading the page from disk, and the program continues execution unaware.