Segmentation

11 min read2,079 words

Segmentation

Segmentation is a memory-management technique that divides a program's logical address space into variable-sized segments based on logical parts of the program.

Segmentation divides a program into logical units such as code, data, stack, heap, functions, or modules, and each segment can have a different size.

Unlike paging:

  • Paging → fixed-size blocks
  • Segmentation → variable-size logical blocks

1. Why Segmentation?

Consider a program containing:

Program │ ├── Code ├── Global Data ├── Heap ├── Stack └── Functions/Modules

These are logically different parts.

Segmentation allows the OS to treat them separately:

+----------------+ | Segment 0 | → Code +----------------+ | Segment 1 | → Data +----------------+ | Segment 2 | → Heap +----------------+ | Segment 3 | → Stack +----------------+

Each segment can have its own:

  • Size
  • Starting address
  • Protection permissions
  • Sharing properties

2. Segmentation vs Paging

The fundamental difference:

PAGING Process ↓ Fixed-size pages P0 | P1 | P2 | P3

Whereas:

SEGMENTATION Process ↓ Logical variable-size segments Code | Data | Stack | Heap

Easy memory trick

Paging = physical-size view

Segmentation = logical-program view

3. What Is a Segment?

A segment is a logical portion of a program.

Examples:

Segment 0 → Code Segment 1 → Data Segment 2 → Stack Segment 3 → Heap

Segments are variable-sized.

For example:

Code = 50 KB Data = 20 KB Stack = 100 KB Heap = 300 KB

There is no requirement that every segment has the same size.

4. Segmentation Address

A logical address in segmentation consists of:

+----------------+----------------+ | Segment Number | Segment Offset | +----------------+----------------+

For example:

Logical Address: (2, 500)

means:

Segment number = 2 Offset = 500

The OS uses the segment number to locate the segment and then uses the offset to locate the exact byte within that segment.

5. Segment Table

The OS maintains a segment table.

A simplified segment table might look like:

SegmentBaseLimit
01000400
150001000
28000600
3120001500

Two very important fields are:

Base

The physical starting address of the segment.

Limit

The size/maximum valid offset of the segment.

6. Base and Limit

Suppose:

Segment 1 Base = 5000 Limit = 1000

This means the segment occupies:

5000 → 5999

because its size is 1000 units.

The valid offsets are approximately:

0 → 999

7. Address Translation

Suppose logical address is:

Segment = 1 Offset = 300

Segment table:

Segment 1 Base = 5000 Limit = 1000

First check:

Offset < Limit 300 < 1000

Valid.

Then physical address:

Physical Address = Base + Offset = 5000 + 300 = 5300

Therefore:

Physical address = 5300

8. Invalid Address

Suppose:

Segment 1 Base = 5000 Limit = 1000

But the process requests:

Offset = 1200

Check:

1200 < 1000

False.

Therefore:

INVALID ACCESS

The hardware/OS generates a protection exception.

This is one of the important advantages of segmentation: each segment can have a defined boundary.

9. Complete Address Translation

CPU | ↓ Logical Address (Segment, Offset) | ↓ Segment Number | ↓ Segment Table | +------+------+ | | Base Limit | | +------+------+ | Check Offset < Limit | +-----+-----+ | | Valid Invalid | | ↓ ↓ Base + Offset Protection | Exception ↓ Physical Address | ↓ RAM

10. Example with a Program

Suppose a program has:

Segment 0 → Code Segment 1 → Data Segment 2 → Stack

Segment table:

SegmentMeaningBaseLimit
0Code100004000
1Data200002000
2Stack300003000

Suppose CPU generates:

(1, 500)

This means:

Segment = 1 Offset = 500

Segment 1:

Base = 20000 Limit = 2000

Check:

500 < 2000 ✓

Physical address:

20000 + 500 = 20500

11. Segment Protection

One major advantage of segmentation is that each segment can have different protection permissions.

For example:

Code Read + Execute Data Read + Write Stack Read + Write Shared Library Read + Execute

Conceptually:

+----------------+------------------+ | Segment | Permissions | +----------------+------------------+ | Code | Read, Execute | | Data | Read, Write | | Stack | Read, Write | | Shared Code | Read, Execute | +----------------+------------------+

This helps protect different parts of a program.

12. Segment Sharing

Segmentation can also make sharing logical program components easier.

Suppose two processes use the same library:

Process A | +------+ ↓ Code Segment ↑ +------+ | Process B

Both processes can potentially map a shared segment.

This saves memory when sharing is appropriate.

13. Segmentation and Logical Organization

Segmentation matches how programmers think about programs.

For example:

Application │ ├── Main code ├── Functions ├── Global variables ├── Heap ├── Stack └── Shared libraries

These can be represented as different logical segments.

Therefore segmentation provides a more natural logical view of memory.

14. External Fragmentation

The biggest memory-management problem with segmentation is external fragmentation.

Why?

Because segments have variable sizes.

Suppose:

+----------+ | Segment A | +----------+ | Free 50 | +----------+ | Segment B | +----------+ | Free 100 | +----------+ | Segment C | +----------+ | Free 80 | +----------+

Total free memory:

50 + 100 + 80 = 230

But a new segment requires:

200

There is no single contiguous 200-unit block.

So allocation may fail.

This is external fragmentation.

15. Compaction

One possible solution is compaction.

Before:

+--------+ | S1 | +--------+ | Free | +--------+ | S2 | +--------+ | Free | +--------+ | S3 | +--------+ | Free | +--------+

After moving segments:

+--------+ | S1 | +--------+ | S2 | +--------+ | S3 | +--------+ | | | FREE | | | +--------+

Now scattered free memory becomes one larger block.

But compaction can be expensive because segments must be moved.

16. Segmentation Hardware

Typical hardware support includes:

  • Segment table
  • Segment-table base register
  • Segment-table length register
  • Protection checking
  • Address translation hardware

A simplified view:

CPU ↓ Segment Number + Offset ↓ Segment Table ↓ Base + Limit ↓ Protection Check ↓ Physical Address

17. Segment Table Registers

Two important registers traditionally associated with segmentation are:

Segment Table Base Register

Stores the starting address of the segment table.

Segment Table Length Register

Stores information about the size/number of valid segment-table entries.

Conceptually:

STBR → Where is the segment table? STLR → How many segment entries are valid?

18. Segmentation with Protection

Suppose:

Segment 0 Base = 10000 Limit = 500 Permission = Read + Execute

If a process tries:

Offset = 300 Read

Then:

300 < 500 ✓ Read allowed ✓

But if it tries:

Write

and the segment is not writable:

Write → DENIED

So segmentation can provide both:

Boundary protection + Permission protection

19. Segmentation Numerical Problem

Suppose the segment table is:

SegmentBaseLimit
050001000
180001200
212000500

Logical address:

(1, 700)

Step 1: Find segment

Segment = 1

Step 2: Find base and limit

Base = 8000 Limit = 1200

Step 3: Check offset

700 < 1200 ✓

Step 4: Calculate physical address

8000 + 700 = 8700

Therefore:

Physical address = 8700

20. Another Numerical Example

Suppose:

Segment 2 Base = 12000 Limit = 500

Logical address:

(2, 450)

Check:

450 < 500 ✓

Physical:

12000 + 450 = 12450

Valid.

Now suppose:

(2, 700)

Check:

700 < 500

False.

Therefore:

Invalid memory access

21. Paging vs Segmentation

This is a very important exam comparison.

FeaturePagingSegmentation
DivisionFixed-size pagesVariable-size segments
Based onFixed-size memory unitsLogical program units
Physical memoryFramesSegments placed in memory
AddressPage + offsetSegment + offset
TablePage tableSegment table
FragmentationInternal fragmentationExternal fragmentation
Programmer viewUsually transparentMore closely matches logical program structure
ProtectionPage-levelSegment-level
SharingPossibleNatural for logical modules
SizeSame page sizeDifferent segment sizes

22. Paging Example vs Segmentation Example

Paging

Suppose:

Process = 16 KB Page size = 4 KB

Process:

P0 = 4 KB P1 = 4 KB P2 = 4 KB P3 = 4 KB

All pages have equal size.

Segmentation

Suppose:

Code = 5 KB Data = 2 KB Stack = 7 KB Heap = 10 KB

Segments:

Code → 5 KB Data → 2 KB Stack → 7 KB Heap → 10 KB

Different sizes.

That's the key difference.

23. Advantages of Segmentation

1. Logical organization

Program components can be represented naturally.

2. Protection

Different permissions can be assigned to different segments.

3. Sharing

Logical modules can be shared between processes.

4. Independent growth

Different segments can grow independently.

For example:

Heap ↑ Stack ↓

5. Modular programming

Functions, libraries, code, and data can be managed separately.

24. Disadvantages of Segmentation

1. External fragmentation

Variable-size segments create scattered holes.

2. Compaction may be required

Compaction can be expensive.

3. More complex memory management

The OS has to manage variable-size regions.

4. Allocation can be slower

Finding suitable contiguous space can be difficult.

5. Segment-table overhead

The OS needs segment metadata.

25. Pure Segmentation vs Paging

Pure Segmentation: Program ↓ Variable-size segments ↓ Segment Table ↓ Physical Memory
Paging: Program ↓ Fixed-size pages ↓ Page Table ↓ Physical Memory

26. Segmentation with Paging

Some architectures/systems can combine segmentation and paging conceptually.

Instead of:

Segment → Physical Memory

we can have:

Logical Address ↓ Segment ↓ Page ↓ Frame ↓ RAM

So:

Segment ↓ Pages ↓ Frames

This can combine the logical organization of segmentation with the physical-memory flexibility of paging.

27. Modern Systems

Pure segmentation is less prominent in modern 64-bit general-purpose operating systems than paging.

Modern systems rely heavily on paging and multi-level page tables for virtual memory and protection.

However, segmentation remains extremely important conceptually because it teaches:

  • Logical memory organization
  • Protection
  • Sharing
  • Base/limit addressing
  • Variable-sized memory regions

28. Complete Segmentation Diagram

CPU | ↓ Logical Address (Segment, Offset) | ↓ Segment Number | ↓ Segment Table | +-------+-------+ | | Base Limit | | +-------+-------+ | Offset < Limit ? / \ Yes No | | ↓ ↓ Base + Offset Protection | Exception ↓ Physical Address | ↓ RAM

🧠 Exam Revision

Definition

Segmentation is a memory-management technique that divides a process into variable-sized logical segments such as code, data, stack, and heap. A segment table stores the base address and limit of each segment and is used for address translation and protection.

Important terms

Segment Segment Number Offset Segment Table Base Limit Protection Sharing External Fragmentation Compaction

Main formula

If Offset < Limit: Physical Address = Base + Offset

Otherwise:

Invalid memory access

Easy memory trick

Paging = equal-sized pieces

Segmentation = meaningful logical pieces

And remember:

PAGING Page → Frame SEGMENTATION Segment → Base + Limit → Physical Address

Keep learning. Keep building. Keep growing.

© 2026 Ugenz Academy