Facebook Pixel

Segmentation Concepts

What is Segmentation?

Paging divides a process into rigid, identically sized blocks. While this is great for the hardware, it completely ignores how a programmer actually writes code.

A program is not a uniform block of data. It is a collection of logical components: a main function, a set of global variables, a stack for local variables, and perhaps an external math library. Segmentation is a memory management technique that divides a process strictly along these logical lines.

Features of Segmentation

  • Logical Division: It aligns with the programmer's view of memory. The OS groups related data together.
  • Variable Size: Because a main function might be 5 KB and a math library might be 50 KB, segments are completely variable in size.
  • Independent Growth: Because segments are stored separately, a structure like the Stack can grow or shrink dynamically without immediately colliding with the Code segment.

How Segmentation Works?

When the CPU executes a segmented program, it does not just generate a single flat address. It generates a two-part Logical Address:

  • Segment Number (s): Identifies which specific segment the CPU wants to access (e.g., Segment 2: The Data Segment).
  • Offset (d): The exact byte location within that specific segment (e.g., byte 400 inside the Data Segment).

The Segment Table

Because segments are scattered across RAM and are all different sizes, the OS uses a Segment Table to track them. Every entry in the table contains two critical pieces of information for a segment:

  • Base: The physical starting address of the segment in RAM.
  • Limit: The exact length (size) of the segment.
text
Address Translation Logic:

1. CPU requests <Segment Number, Offset>
2. OS looks up the Segment Number in the Segment Table.
3. Protection Check: Is Offset < Limit?
   - NO: Trap! Segmentation Fault (The program tried to access memory outside its segment).
   - YES: The request is valid.
4. Physical Address = Base + Offset.

Pros and Cons of Segmentation

AdvantagesDisadvantages
No Internal Fragmentation: Because segments are sized exactly to what the logical module needs, no space is wasted inside the segment.External Fragmentation: Because segments are variable-sized, they leave behind irregularly sized holes in RAM, eventually requiring expensive Compaction.
Access Control: Permissions can be set per segment. Code can be Read-Only to prevent tampering, while Data is Read-Write.Complex Allocation: The OS must use First-Fit or Best-Fit algorithms to find holes large enough for incoming segments.
Easy Sharing: Two processes can share a single standard library simply by pointing their Segment Tables to the exact same Base Address.Table Overhead: Maintaining the Segment Table requires extra memory and CPU cycles.

Paging vs Segmentation

FeaturePagingSegmentation
Division TypeHardware-driven (arbitrary cuts).Programmer-driven (logical cuts).
Block SizeFixed and identical (Pages).Variable depending on the module (Segments).
FragmentationSuffers from Internal Fragmentation.Suffers from External Fragmentation.
Table UsedPage Table (Tracks Frames).Segment Table (Tracks Base and Limit).

Summary

Segmentation bridges the gap between how a programmer visualizes code and how the operating system manages RAM. By organizing memory into logical modules, it provides excellent security through access control and makes sharing libraries effortless. However, its variable sizing reintroduces the messy problem of External Fragmentation.

Sort the Concepts

Classify the following characteristics as belonging to either Paging or Segmentation.

Paging
Segmentation
Unsorted Items:
Divides memory into strict, fixed-size blocks.
Divides a program based on logical structure (Code, Data, Stack).
Suffers from External Fragmentation due to variable-sized chunks.
Solves External Fragmentation, but suffers from Internal Fragmentation.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.