130 likes | 173 Views
Chapter 16 Segmentation. Jake M. Choi (kidcoder@snu.ac.kr) School of Computer Science and Engineering Seoul National University. Too Much Wasted Space. 0KB 1KB 2KB . . 4KB 5KB 6KB . . . . . . 14KB 15KB 16KB. Wasted Space. Address Space
E N D
Chapter 16Segmentation Jake M. Choi (kidcoder@snu.ac.kr) School of Computer Science and Engineering Seoul National University
Too Much Wasted Space 0KB 1KB 2KB . . 4KB 5KB 6KB . . . . . . 14KB 15KB 16KB Wasted Space • Address Space • Using only one pair of base and bounds registers is wasteful • Too much free space between the stack and heap • 32-bit address space • The entire address space will not fit in memory • 4 GB in size • Typical program uses MB of memory
Segmentation 0KB 16KB 32KB 48KB 64KB MMU 3 • Base and bounds pair • For each logical segment • Operating system’s role • Separate code, stack, and heap • Different parts of physical memory • Avoids having unused virtual address space
Example Translation 0KB 2KB 4KB 6KB 14KB 16KB • Virtual Address 100 • 0KB < 100 < 2KB = Code Segment (0KB – 2KB) • Is 100 < 2K Size? • Physical Address = 100 + 46KB = 47204 • Virtual Address 4200 • 4096 < 4200 < 6144 = Heap Segment (4KB – 6KB) • 4200 – 4096 = 104 • Is 104 < 2K Size? • Physical Address = 104 + 49152= 49256
Example Translation 0KB 2KB 4KB 6KB 14KB 16KB • Virtual Address 7KB (7168) • 6KB < 7168 • 7168 – 4096 = 3072 • Is 3072 < 2K Size? • Results in Segmentation Fault
Which Segment Though? • 12 11 10 9 8 7 6 5 4 3 2 1 0 Offset Segment • How does the hardware know which segment an address refers to? • Explicit Approach
Which Segment Though? • How does the hardware know which segment an address refers to? • Implicit Approach • Hardware notices how the address was formed • Generated from PC = Code Segment • Address is based off stack or base pointer = Stack Segment • Anything else = Heap Segment
The Stack • However, the stack grows backwards • Extra hardware support is needed • Extra bit • Set to 1 when segment grows in positive direction • Set to 0 when segment grows in negative direction • Example Translation • Access Virtual Address 15KB = 11 1100 0000 0000 (hex 0x3C00) • Top two bits 11 = stack segment • Offset is 3KB, 2KB – 3KB = -1KB • Absolute value of (-1KB) = 1KB < 2KB? • Simply add to base register (28K) to become 27K
Sharing Memory Segments • Sometimes memory segments are shared • Code sharing is common even today • Protection bits • Read-only code segment • Additional hardware checks • If user process tries to write to read-only page or execute the non-executable • Exception should be raised by hardware
Fine-grained vs. Coarse-grained Segmentation • Our examples had only a few segments (code, heap, stack) • Coarse-grained segmentation • Early systems had more flexibility • Fine-grained segmentation • Segment table • Stored in memory • Supports creation of large number of segments • Burroughs B5000 • OS/Hardware managed thousands of segments • Fine-grained segments = Better main memory utilization?
OS Support 0KB 64KB • Segment registers • Must be saved and restored on context switch • Managing free space in physical memory • Each address space used to be same size • Now different sizes for each segment • External Fragmentation • 20KB Request comes in • 24KB free in separate chunks • OS cannot satisfy
OS Support 0KB 64KB • Compacted memory • OS stops processes • Copies data to one contiguous region of memory • Changes registers to point to new location • Expensive • Memory-intensive • Uses too much processor time • Needs a simpler approach • Best-fit, worst-fit, first-fit algorithms • Buddy algorithm • Yet, external fragmentation is unavoidable
Summary • Segmentation saves memory • Is fast • Has fringe benefits like code-sharing • However, has two problems • External fragmentation • Still not flexible enough • Large but sparsely-used heap • Entire heap must still reside in memory