Operating Systems Youjip Won 16 Segmentation Youjip Won

  • Slides: 17
Download presentation
Operating Systems Youjip Won

Operating Systems Youjip Won

16. Segmentation Youjip Won 2

16. Segmentation Youjip Won 2

Inefficiency of the Base and Bound Approach 0 KB 1 KB 2 KB Program

Inefficiency of the Base and Bound Approach 0 KB 1 KB 2 KB Program Code 3 KB Big chunk of “free” space takes up physical memory. Hard to run when an address space does not fit 4 KB 5 KB Heap 6 KB into physical memory (free) 14 KB 15 KB Stack 16 KB Youjip Won 3

Segmentation Segment is just a contiguous portion of the address space of a particular

Segmentation Segment is just a contiguous portion of the address space of a particular length. Logically-different segment: code, stack, heap Each segment can be placed in different part of physical memory. Base and bounds exist per each segment. Youjip Won 4

Placing Segment In Physical Memory 0 KB Operating System 16 KB (not in use)

Placing Segment In Physical Memory 0 KB Operating System 16 KB (not in use) Segment Code Heap Stack 32 KB 48 KB 64 KB (not in use) Code Heap Base Size 32 K 2 K 34 K 2 K 28 K 2 K (not in use) Physical Memory Youjip Won 5

Address Translation on Segmentation: code The offset of virtual address 100 is 100. The

Address Translation on Segmentation: code The offset of virtual address 100 is 100. The code segment starts at virtual address 0 in address space. Segment Code Base 32 K Size 2 K 16 KB (not in use) 0 KB 100 instruction 2 KB Program Code 32 KB Code 34 KB Heap 4 KB (not in use) Youjip Won 6

Address Translation on Segmentation: heap The offset of virtual address 4200 is 104. The

Address Translation on Segmentation: heap The offset of virtual address 4200 is 104. The heap segment starts at virtual address 4096 in address space. Segment Heap Base 34 K Size 2 K (not in use) 32 KB Code 4 KB 4200 data 6 KB 34 KB Heap Address Space Heap 36 KB (not in use) Physical Memory Youjip Won 7

Segmentation Fault or Violation If an illegal address such as 7 KB which is

Segmentation Fault or Violation If an illegal address such as 7 KB which is beyond the end of heap is referenced, the OS occurs segmentation fault. The hardware detects that address is out of bounds. 4 KB Heap 6 KB 7 KB 8 KB (not in use) Address Space Youjip Won 8

Referring to Segment Explicit approach Chop up the address space into segments based on

Referring to Segment Explicit approach Chop up the address space into segments based on the top few bits of virtual address. 13 12 11 10 9 8 7 Segment 6 5 4 3 2 1 0 Offset Example: virtual address 4200 (01000001101000) Segment Code Heap Stack - bits 00 01 10 11 13 12 11 10 0 1 0 9 8 7 6 5 4 3 2 1 0 0 1 1 0 0 0 0 Segment Offset Youjip Won 9

Segment selection 1 2 3 4 5 6 7 8 9 // get top

Segment selection 1 2 3 4 5 6 7 8 9 // get top 2 bits of 14 -bit VA Segment = (Virtual. Address & SEG_MASK) >> SEG_SHIFT // now get offset Offset = Virtual. Address & OFFSET_MASK if (Offset >= Bounds[Segment]) Raise. Exception(PROTECTION_FAULT) else Phys. Addr = Base[Segment] + Offset Register = Access. Memory(Phys. Addr) SEG_MASK = 0 x 3000(11000000) SEG_SHIFT = 12 OFFSET_MASK = 0 x. FFF (00111111) Youjip Won 10

Referring to Stack Segment Stack grows backward. Extra hardware support is need. The hardware

Referring to Stack Segment Stack grows backward. Extra hardware support is need. The hardware checks which way the segment grows. 1: positive direction, 0: negative direction Segment Register(with Negative-Growth Support) (not in use) 26 KB Stack Segment Code Heap Stack Base 32 K 34 K 28 K Size 2 K 2 K 2 K Grows Positive? 1 1 0 28 KB (not in use) Physical Memory Youjip Won 11

Support for Sharing Segment can be shared between address space. Code sharing is still

Support for Sharing Segment can be shared between address space. Code sharing is still in use in systems today. by extra hardware support. Extra hardware support is need form of Protection bits. A few more bits per segment to indicate permissions of read, write and execute. Segment Register Values(with Protection) Segment Code Heap Stack Base 32 K 34 K 28 K Size 2 K 2 K 2 K Grows Positive? 1 1 0 Youjip Won Protection Read-Execute Read-Write 12

Fine-Grained and Coarse-Grained segmentation Coarse-Grained means small number of segments. e. g. , code,

Fine-Grained and Coarse-Grained segmentation Coarse-Grained means small number of segments. e. g. , code, heap, stack. Fine-Grained segmentation allows more flexibility for address space in some early system. To support many segments, Hardware support with a segment table is required. Youjip Won 13

OS support: Fragmentation External Fragmentation: little holes of free space in physical memory that

OS support: Fragmentation External Fragmentation: little holes of free space in physical memory that is too small for allocating segment. There is 24 KB free, but not in one contiguous segment. The OS cannot satisfy the 20 KB request. Compaction: rearranging the exiting segments in physical memory. Compaction is costly. Stop running process. Copy data to somewhere. Change segment register value. Youjip Won 14

Memory Compaction Not compacted Compacted 0 KB 8 KB Operating System 16 KB (not

Memory Compaction Not compacted Compacted 0 KB 8 KB Operating System 16 KB (not in use) 24 KB Allocated 32 KB 40 KB 48 KB Allocated 32 KB (not in use) 40 KB Allocated 48 KB (not in use) 56 KB 64 KB 56 KB Allocated (not in use) 64 KB Youjip Won 15

History of segmentation In early days, OS used segmentation. Burroughs B 5000 (first commercial

History of segmentation In early days, OS used segmentation. Burroughs B 5000 (first commercial machine with virtual memory) IBM AS/400 Intel 8086, 80286 80386 and later Intel CPU’s support paging. X 86 -64 does not use segmentation any more in 64 bit mode CS, SS, DS and ES are forced to 0 and 2^24. . Youjip Won 16

Summary Segmentation can better support sparse address spaces. It is also fast as the

Summary Segmentation can better support sparse address spaces. It is also fast as the overheads of translation are minimal. Sharing (such as code) is easy. Issues External fragmentation issue Sparse segment Youjip Won 17