Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
16 views17 pages

Chapter 16. Segmentation

Uploaded by

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

Chapter 16. Segmentation

Uploaded by

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

16.

Segmentation
Operating System: Three Easy Pieces

Operating Systems by Dr. Praveen Kumar @ CSED, VNIT Nagpur 1


Inefficiency of the Base and Bound Approach

0KB
1KB Program Code  Big chunk of “free” space in VAS
2KB
3KB  In contiguous allocation even “free” space takes
4KB
up physical memory => wasteful
5KB Heap
6KB  Hard to run when the entire address space does
not fit into physical memory

 No partial sharing: Cannot share parts of address


(free) space such as program code

14KB
15KB Stack
16KB

Operating Systems by Dr. Praveen Kumar @ CSED, VNIT Nagpur 2


Segmentation

 Divide address space into segments which corresponds to logical


entity in address space like code, stack, heap

 Segment is a contiguous portion of the address space of a particular


length.

 Each segment has its own Base and bounds pair. They can independ-
ently:
 Be Placed separately in different part of physical memory.

 grow and shrink

 be protected or shared (separate read/write/execute protection bits)

Operating Systems by Dr. Praveen Kumar @ CSED, VNIT Nagpur 3


Example: Placing Segment In Physical Memory

0KB
1KB Program Code
2KB 0KB
3KB
4KB Operating System
5KB Heap
16KB
6KB (not in use)
Segment Base Size
Stack Code 32K 2K
(not in use)
32KB Heap 34K 2K
Code
Heap Stack 28K 2K
(free)
48KB
(not in use)

64KB
Physical Memory
14KB
15KB Stack
16KB

Operating Systems by Dr. Praveen Kumar @ CSED, VNIT Nagpur 4


Address Translation for Code Segment

𝑝ℎ𝑦𝑠𝑖𝑐𝑎𝑙 𝑎𝑑𝑑𝑟𝑒𝑠𝑠 = 𝑜𝑓𝑓𝑠𝑒𝑡 + 𝑏𝑎𝑠𝑒

 The offset of virtual address 100 is 100.


 The code segment starts at virtual address 0 in address space.

Segment Base Size 16KB


Code 32K 2K
(not in use)

0KB 32KB 𝟏𝟎𝟎 + 𝟑𝟐𝑲 𝒐𝒓 𝟑𝟐𝟖𝟔𝟖


100 instruction
Code
is the desired
Program Code physical address
2KB 34KB
Heap
4KB

(not in use)

Operating Systems by Dr. Praveen Kumar @ CSED, VNIT Nagpur 5


Address Translation for Heap Segment

𝑽𝒊𝒓𝒕𝒖𝒂𝒍 𝒂𝒅𝒅𝒓𝒆𝒔𝒔 + 𝒃𝒂𝒔𝒆 is not the correct physical address.

 The offset of virtual address 4200 is 104.


 The heap segment starts at virtual address 4096 in address space.

Segment Base Size


Heap 34K 2K
(not in use)

32KB
Code
4KB 34KB 𝟏𝟎𝟒 + 𝟑𝟒𝑲 𝒐𝒓 𝟑𝟒𝟗𝟐𝟎
4200 data
Heap
is the desired
Heap physical address
6KB 36KB

(not in use)
Address Space

Physical Memory

Operating Systems by Dr. Praveen Kumar @ CSED, VNIT Nagpur 6


Segmentation Fault or Violation

 If an illegal address such as 7KB which is beyond the end of heap is


referenced, the hardware detects it is out of bounds and traps into
OS. What would OS do?
 Likely terminate with the famous message “segmentation fault”

4KB
Heap
6KB
7KB
(not in use)
8KB

Address Space

Operating Systems by Dr. Praveen Kumar @ CSED, VNIT Nagpur 7


Segmented Addressing

 Chop up the address space into segments based on the top few bits
of virtual address
 Use top bits of virtual address to select segment and remaining bits to
select offset within segment 13 12 11 10 9 8 7 6 5 4 3 2 1 0

Segment Offset
 Example: virtual address 4200 (01000001101000)

Segment bits 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Code 00 0 1 0 0 0 0 0 1 1 0 1 0 0 0
Heap 01
- 10
Stack 11 Segment Offset

Operating Systems by Dr. Praveen Kumar @ CSED, VNIT Nagpur 8


Referring to Segment(Cont.)

1 // get top 2 bits of 14-bit VA


2 Segment = (VirtualAddress & SEG_MASK) >> SEG_SHIFT
3 // now get offset
4 Offset = VirtualAddress & OFFSET_MASK
5 if (Offset >= Bounds[Segment])
6 RaiseException(PROTECTION_FAULT)
7 else
8 PhysAddr = Base[Segment] + Offset
9 Register = AccessMemory(PhysAddr)

 SEG_MASK = 0x3000(11000000000000)

 SEG_SHIFT = 12

 OFFSET_MASK = 0xFFF (00111111111111)

Operating Systems by Dr. Praveen Kumar @ CSED, VNIT Nagpur 9


Referring to Stack Segment

 Stack grows backward.

 Extra hardware support is needed.


 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?


Code 32K 2K 1
Heap 34K 2K 1
Stack 28K 2K 0

Operating Systems by Dr. Praveen Kumar @ CSED, VNIT Nagpur 10


Referring to Stack Segment (cont..)

 Example: accessing VA 15KB which should map to 27KB PA.


 15KB (11 1100 0000 0000) – offset of 3KB

 To get correct negative offset – subtract the max segment size (here 4 KB)

 Simply add the correct offset (-1KB) to base (28KB) to arrive at correct PA
(27KB)

(not in use)

26KB
(free) Stack
28KB
(not in use)
14KB
15KB Stack
Physical Memory
16KB

Operating Systems by Dr. Praveen Kumar @ CSED, VNIT Nagpur 11


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 needed in the 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
Code 32K 2K 1 Read-Execute
Heap 34K 2K 1 Read-Write
Stack 28K 2K 0 Read-Write

Operating Systems by Dr. Praveen Kumar @ CSED, VNIT Nagpur 12


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.

Segment Base Bounds RW


0 0x2000 0x6ff 10
1 0x0000 0x4ff 11
2 0x3000 0xfff 11
3 0x0000 0x000 00

Operating Systems by Dr. Praveen Kumar @ CSED, VNIT Nagpur 13


OS support: Fragmentation

 External Fragmentation: little holes of free space in physical memory


that creates difficulty to allocate new segments.
 The OS cannot satisfy a 20KB request.
0KB
 There is 24KB free, but not as one contiguous
8KB Operating System
segment.
16KB
(not in use)
24KB
Allocated
32KB
(not in use)

40KB Allocated

48KB
(not in use)

56KB
Allocated
64KB

Operating Systems by Dr. Praveen Kumar @ CSED, VNIT Nagpur 14


Memory Compaction

 Compaction: rearranging the existing segments in physical memory.


 Compaction is costly. Compacted
0KB
 Stop running process.
8KB Operating System
 Copy data to somewhere.

 Change segment register value. 16KB

24KB
Allocated
32KB

40KB

48KB
(not in use)
56KB

64KB

Operating Systems by Dr. Praveen Kumar @ CSED, VNIT Nagpur 15


Question

 What if not enough bits (small addr space) to do segment addressing?

 For 16 bit logical address, 4 segments; how many bits for segment?
How many bits for offset?

 Translate logical addresses (in hex) to physical addresses


 0x0240:

 0x4108:

0xa65c:

Segment Base Bounds RW
0 0x2000 0x6ff 10
1 0x0000 0x4ff 11
2 0x3000 0xfff 11
3 0x0000 0x000 00

Operating Systems by Dr. Praveen Kumar @ CSED, VNIT Nagpur 16


 Disclaimer: This slide set has been adapted from the initial lecture slides for Operating
System course in Computer Science Dept. at Hanyang University. This lecture slide set is
for OSTEP book written by Remzi and Andrea at University of Wisconsin.

Operating Systems by Dr. Praveen Kumar @ CSED, VNIT Nagpur

You might also like