1. Introduction
Paging divides memory into fixed-size blocks, but it does not reflect how programs are naturally organized.
Programs are logically divided into:
Functions
Procedures
Data structures
Stack
Heap
Modules
Segmentation is a memory management technique that divides a program into variable-sized logical units called segments.
2. What is Segmentation?
Segmentation is a memory management scheme in which a process is divided into variable-sized logical segments based on the program's structure.
Definition
A segment is a logical unit of a program such as:
Code
Data
Stack
Heap
Function module
Key Idea
Program
↓
Segment 0 (Code)
Segment 1 (Data)
Segment 2 (Stack)
Segment 3 (Heap)
Unlike paging, segmentation follows the programmer's view of memory.
3. Why Segmentation is Needed
Paging solves external fragmentation but ignores the logical structure of programs.
Problems with Paging:
Program divided arbitrarily
Difficult protection management
Difficult sharing of logical components
Segmentation provides:
Logical organization
Easier protection
Easier sharing
Better modularity
4. Structure of a Program in Segmentation
A typical process may be divided as follows:
| Segment Number | Segment |
|---|---|
| 0 | Code |
| 1 | Data |
| 2 | Stack |
| 3 | Heap |
Example:
Segment 0 → Program Instructions
Segment 1 → Global Variables
Segment 2 → Function Call Stack
Segment 3 → Dynamic Memory
Key Insight
Each segment represents a meaningful program component.
5. Memory Layout in Segmentation
A process does not need to occupy one contiguous block.
Different segments can be stored at different locations.
Example:
Physical Memory
----------------
Segment 1 (Data)
----------------
Free Space
----------------
Segment 0 (Code)
----------------
Free Space
----------------
Segment 3 (Heap)
----------------
Segment 2 (Stack)
----------------
Segments may be scattered throughout memory.
6. Logical Address in Segmentation
In segmentation, a logical address consists of two parts:
Logical Address
=
(Segment Number, Offset)
Components
Segment Number (s)
Identifies which segment is being accessed.
Offset (d)
Specifies location within the segment.
Example:
(2, 150)
Meaning:
Segment Number = 2
Offset = 150 bytes
7. Segment Table
The operating system maintains a Segment Table.
Each entry contains:
Base Address
Limit (Size)
Example:
| Segment | Base | Limit |
|---|---|---|
| 0 | 1000 | 500 |
| 1 | 2500 | 800 |
| 2 | 4000 | 300 |
| 3 | 5000 | 1000 |
8. Segment Table Components
8.1 Base Address
Starting physical address of the segment.
Example:
Segment 1 Base = 2500
8.2 Limit
Maximum size of the segment.
Example:
Segment 1 Size = 800 bytes
Used for protection checking.
9. Address Translation in Segmentation
The CPU generates:
(Segment Number, Offset)
The MMU performs:
Access segment table
Obtain base and limit
Verify offset is valid
Compute physical address
Formula
Physical Address
=
Base + Offset
10. Address Translation Example
Given:
| Segment | Base | Limit |
|---|---|---|
| 0 | 1000 | 500 |
| 1 | 2500 | 800 |
| 2 | 4000 | 300 |
Logical Address:
(1, 200)
Step 1
Segment Number:
1
Step 2
Segment Table Lookup:
Base = 2500
Limit = 800
Step 3
Check Offset
200 < 800
Valid.
Step 4
Calculate Physical Address
Physical Address
= Base + Offset
= 2500 + 200
= 2700
Final Answer
Physical Address = 2700
11. Invalid Address Example
Suppose:
Logical Address = (2, 500)
Segment Table:
Segment 2 Limit = 300
Check:
500 > 300
Invalid.
The hardware generates a trap (segmentation fault).
12. Hardware Support
Segmentation requires special registers.
Segment Table Base Register (STBR)
Points to start of segment table.
Segment Table Length Register (STLR)
Stores number of segments.
Example:
STBR → Address of Segment Table
STLR → Total Segments
13. Protection in Segmentation
Each segment can have different access rights.
Example:
| Segment | Permission |
|---|---|
| Code | Read, Execute |
| Data | Read, Write |
| Stack | Read, Write |
| Shared Library | Read, Execute |
Advantage
Fine-grained protection becomes possible.
14. Sharing in Segmentation
Segments can be shared between processes.
Example:
Shared Code Segment
used by:
Process A
Process B
Process C
Benefits:
Saves memory
Reduces duplication
Supports shared libraries
15. Fragmentation in Segmentation
Because segments are variable-sized, segmentation suffers from:
External Fragmentation
Example:
Free Space
100 KB
50 KB
75 KB
Total free memory:
225 KB
Request:
150 KB
Allocation may fail because memory is scattered.
Key Insight
Segmentation suffers from external fragmentation.
16. Does Segmentation Have Internal Fragmentation?
Generally:
No
because segments are allocated according to their exact size.
Only negligible waste may occur due to alignment requirements.
17. Advantages of Segmentation
Logical Organization
Matches program structure.
Better Protection
Different permissions for different segments.
Easy Sharing
Code and libraries can be shared.
Supports Modular Programming
Each module can be maintained separately.
Flexible Growth
Stack and heap can grow independently.
18. Disadvantages of Segmentation
External Fragmentation
Memory becomes scattered over time.
Complex Allocation
Variable-sized segments are harder to manage.
Compaction May Be Required
Processes may need relocation.
Higher Management Overhead
Segment tables must be maintained.
19. Segmentation vs Paging
| Feature | Paging | Segmentation |
|---|---|---|
| Division Basis | Physical | Logical |
| Unit Size | Fixed | Variable |
| Address Format | Page + Offset | Segment + Offset |
| Fragmentation | Internal | External |
| Protection | Difficult | Easier |
| Sharing | Limited | Better |
| Programmer View | Hidden | Visible |
20. Paging vs Segmentation Example
Paging
Program divided into:
Page 0
Page 1
Page 2
Page 3
Pages have no logical meaning.
Segmentation
Program divided into:
Code Segment
Data Segment
Stack Segment
Heap Segment
These correspond to actual program components.
21. Real-World Analogy
Think of a book.
Paging
Divide the book into equal-sized bundles:
Pages 1–50
Pages 51–100
Pages 101–150
No regard for chapter boundaries.
Segmentation
Divide the book by chapters:
Chapter 1
Chapter 2
Chapter 3
Each chapter may have a different size.
Segmentation follows the logical organization of the book.
Most Important Point
Segmentation divides memory according to the logical structure of a program, using variable-sized segments and segment tables to perform address translation while providing better protection and sharing than paging.