Operating Systems Session 13 Segmentation
Operating Systems Session 13 Segmentation
Operating Systems
Session 13
Segmentation
© 2016 KL University – The contents of this presentation are an intellectual and copyrighted property of KL University. ALL RIGHTS RESERVED 1
Inefficiency of the Base and Bound Approach
0KB
1KB Program Code
• Big chunk of “free” space
Program Code
2KB
3KB
• “free” space takes up physical memory.
4KB
Heap
• Hard to run when an address space does not fit into
5KB
Heap
6KB physical memory
(free)
(free)
14KB
15KB Stack
Stack
16KB
2
Segmentation
• Segment is just a contiguous portion of the address space of a
particular length.
• Logically-different segment: code, stack, heap
3
Placing Segment In Physical Memory
0KB
Operating System
Operating System
16KB
(not in use)
(not in use)
Segment
Segment Base
Base Size
Size
Stack
Stack Code
Code 32K
32K 2K
2K
(not in use)
32KB (not in use) Heap
Heap 34K
34K 2K
2K
Code
Code
Heap Stack 28K 2K
Heap Stack 28K 2K
48KB
(not in use)
(not in use)
64KB
Physical Memory
4
Address Translation on Segmentation
0KB 32KB
100 instruction
Code
Program Code Code
2KB Program Code 34KB
Heap
Heap
4KB
(not in use)
(not in use)
5
Address Translation on Segmentation(Cont.)
32KB
Code
Code
4KB 34KB
4200 data
Heap
Heap Heap
6KB Heap 36KB
(not in use)
Address Space (not in use)
Physical Memory
6
Segmentation Fault or Violation
• If an illegal address such as 7KB which is beyond the end of heap is
referenced, the OS occurs segmentation fault.
• The hardware detects that address is out of bounds.
4KB
Heap
Heap
6KB
7KB
(not in use)
8KB (not in use)
Address Space
7
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 6 5 4 3 2 1 0
13 12 11 10 9 8 7 6 5 4 3 2 1 0
Segment Offset
8
Referring to Segment(Cont.)
1 // get top 2 bits of 14-bit VA
1 // get top 2 bits of 14-bit VA
2 Segment = (VirtualAddress & SEG_MASK) >> SEG_SHIFT
2 Segment = (VirtualAddress & SEG_MASK) >> SEG_SHIFT
3 // now get offset
3 // now get offset
4 Offset = VirtualAddress & OFFSET_MASK
4 Offset = VirtualAddress & OFFSET_MASK
5 if (Offset >= Bounds[Segment])
5 if (Offset >= Bounds[Segment])
6 RaiseException(PROTECTION_FAULT)
6 RaiseException(PROTECTION_FAULT)
7 else
7 else
8 PhysAddr = Base[Segment] + Offset
8 PhysAddr = Base[Segment] + Offset
9 Register = AccessMemory(PhysAddr)
9 Register = AccessMemory(PhysAddr)
• SEG_MASK = 0x3000(11000000000000)
• SEG_SHIFT = 12
• OFFSET_MASK = 0xFFF (00111111111111)
9
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
Physical Memory
10
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 for form of Protection bits.
• A few more bits per segment to indicate permissions of read, write and
execute.
Segment Register Values(with Protection)
11
Fine-Grained and Coarse-Grained
• Coarse-Grained means segmentation in a small number.
• 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.
12
OS support: Fragmentation
• External Fragmentation: little holes of free space in physical memory
that make difficulty to allocate new segments.
• There is 24KB free, but not in one contiguous segment.
• The OS cannot satisfy the 20KB request.
13
Memory Compaction
Not compacted Compacted
0KB 0KB
16KB 16KB
(not in use)
(not in use)
24KB 24KB
Allocated Allocated
Allocated Allocated
32KB 32KB
(not in use)
(not in use)
40KB Allocated 40KB
Allocated
48KB 48KB
(not in use)
(not in use) (not in use)
(not in use)
56KB 56KB
Allocated
Allocated
64KB 64KB
14