0% found this document useful (0 votes)
47 views

Operating Systems Session 13 Segmentation

Uploaded by

mine
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views

Operating Systems Session 13 Segmentation

Uploaded by

mine
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 14

18CS2102R

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

• Each segment can be placed in different part of physical memory.


• Base and bounds exist per each segment.

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

• The offset of virtual address 100 is 100.


• The code segment starts at virtual address 0 in address space.
Segment Base Size 16KB
Segment Base Size
Code 32K 2K
Code 32K 2K
(not in use)
(not in use)

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.)

• The offset of virtual address 4200 is 104.


• The heap segment starts at virtual address 4096 in address space.
Segment Base Size
Segment Base Size
Heap 34K 2K
Heap 34K 2K
(not in use)
(not in use)

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

• Example: virtual address 4200 (01000001101000)


Segment
Segment bits
bits 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
Code
Code 00
00 0 1 0 0 0 0 0 1 1 0 1 0 0 0
0 1 0 0 0 0 0 1 1 0 1 0 0 0
Heap
Heap 01
01
Stack
Stack 10
10
-- 11 Segment Offset
11

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

Segment Register(with Negative-Growth Support)

Segment Base Size Grows Positive?


(not in use) Segment Base Size Grows Positive?
(not in use) Code 32K 2K 1
Code 32K 2K 1
Heap 34K 2K 1
26KB Heap 34K 2K 1
Stack 28K 2K 0
Stack Stack 28K 2K 0
Stack
28KB
(not in use)
(not in use)

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)

Segment Base Size Grows Positive? Protection


Segment Base Size Grows Positive? Protection
Code 32K 2K 1 Read-Execute
Code 32K 2K 1 Read-Execute
Heap 34K 2K 1 Read-Write
Heap 34K 2K 1 Read-Write
Stack 28K 2K 0 Read-Write
Stack 28K 2K 0 Read-Write

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.

• Compaction: rearranging the exiting segments in physical memory.


• Compaction is costly.
• Stop running process.
• Copy data to somewhere.
• Change segment register value.

13
Memory Compaction
Not compacted Compacted
0KB 0KB

8KB Operating System 8KB Operating System


Operating System Operating System

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

You might also like