0
0
Operating Systemsknowledge~6 mins

Segmentation in Operating Systems - Full Explanation

Choose your learning style9 modes available
Introduction
Imagine trying to organize a large book by breaking it into chapters and sections so you can find information easily. Computers face a similar problem when managing memory for programs, needing a way to divide memory into meaningful parts.
Explanation
Memory Division by Logical Units
Segmentation divides a program's memory into segments based on logical units like functions, data arrays, or modules. Each segment represents a meaningful part of the program, making it easier to manage and protect.
Segmentation breaks memory into logical parts that match the program's structure.
Segment Table and Addresses
The system keeps a segment table that stores the base address and length of each segment. When a program accesses memory, it uses a segment number and an offset within that segment to find the exact location.
Memory addresses in segmentation use a segment number plus an offset to locate data.
Protection and Sharing
Because segments are separate, the system can protect them individually. It can also share segments like code between programs without copying, saving memory and improving efficiency.
Segmentation allows individual protection and sharing of program parts.
Variable Segment Sizes
Segments can be different sizes depending on the program's needs. This flexibility helps use memory efficiently but can cause gaps or fragmentation between segments.
Segments vary in size, which helps flexibility but can lead to fragmentation.
Real World Analogy

Think of a filing cabinet where each drawer holds folders for different projects. Each folder contains papers related to that project, making it easy to find and protect important documents. Sometimes, folders can be shared between drawers if needed.

Memory Division by Logical Units → Folders in a drawer holding related papers for a project
Segment Table and Addresses → Labels on folders and pages that tell you where to find each paper
Protection and Sharing → Locking a folder to protect its papers or sharing a folder between drawers
Variable Segment Sizes → Folders of different thickness depending on how many papers they hold
Diagram
Diagram
┌───────────────┐
│   Segment 0   │
│  (Code)      │
├───────────────┤
│   Segment 1   │
│  (Data)      │
├───────────────┤
│   Segment 2   │
│  (Stack)     │
└───────────────┘

Segment Table:
[Segment # | Base Address | Length]
[0         | 1000         | 500   ]
[1         | 1500         | 300   ]
[2         | 1800         | 200   ]
This diagram shows memory divided into three segments with a segment table listing their base addresses and sizes.
Key Facts
SegmentA logical unit of a program's memory such as code, data, or stack.
Segment TableA data structure that stores base addresses and lengths of all segments.
Logical AddressAn address consisting of a segment number and an offset within that segment.
FragmentationUnused gaps in memory caused by variable-sized segments.
Memory ProtectionPreventing unauthorized access to segments by controlling permissions.
Common Confusions
Segmentation is the same as paging.
Segmentation is the same as paging. Segmentation divides memory by logical program parts of variable size, while paging divides memory into fixed-size blocks without regard to program structure.
Logical address is a single number.
Logical address is a single number. In segmentation, a logical address has two parts: a segment number and an offset within that segment.
Summary
Segmentation organizes memory into logical parts that reflect a program's structure, like code and data sections.
Each segment has a base address and length stored in a segment table, enabling precise memory access.
Segmentation supports protection and sharing of program parts but can cause fragmentation due to variable segment sizes.