Segmentation in Operating Systems

Segmentation is a memory management scheme that divides a process's memory into variable-sized, logically meaningful units called segments, such as code, stack, data and heap. Unlike paging, which splits memory into fixed-size chunks with no regard for their contents, each segment matches the real size of the logical unit it represents.

3 min read

Segmentation is a memory management scheme that divides a process's address space into variable-sized, logically meaningful units called segments, such as the code segment, the stack segment, the data segment, and the heap segment, rather than the fixed-size, arbitrary chunks paging uses.

How segmentation differs from paging conceptually

Paging splits memory into equal-sized pieces with no regard for what's logically inside them; a single function's code might be split across two pages with no significance to that split point. Segmentation instead mirrors how a programmer or compiler actually thinks about a program's memory: this whole region is the code, this whole region is the stack, this whole region is a specific array or data structure. Each segment can be a different size, matching the real size of the logical unit it represents.

How address translation works with segmentation

A logical address under segmentation is a pair: a segment number and an offset within that segment. The OS maintains a segment table per process, where each entry stores a segment's base address (where it starts in physical memory) and its limit (how large it is). To translate: look up the segment number to get its base and limit, check that the offset is within the limit (if not, raise a protection fault, since the process is trying to access beyond that segment's real bounds), and if valid, physical address = base + offset.

A worked example

A process has a segment table:

SegmentBaseLimit
0 (code)4000800
1 (stack)60001200
2 (data)2000500

The process generates logical address (segment 1, offset 150). Check: is 150 within the limit of 1200? Yes. Physical address = base (6000) + offset (150) = 6150.

Now suppose the process generates (segment 2, offset 700). Check: is 700 within the limit of 500? No, 700 exceeds the segment's actual size. This raises a protection fault instead of translating to a physical address, since the process is trying to reach memory outside that segment's legitimate bounds, a genuine bug or attempted violation, not a valid access.

Where this matters in real systems

Pure segmentation as a standalone scheme is largely historical (older x86 processors supported it directly in hardware), but its logical grouping idea survives inside modern systems in a different form: most operating systems today use paging combined with segmentation concepts baked into how memory regions are managed, a process's virtual memory map is still logically divided into distinct regions for code, stack, heap, and shared libraries, each with its own permissions (like "executable" for code, "writable" for the stack and heap), even though the underlying physical storage is handled through paging.

What interviewers actually ask about segmentation

  • "What's the difference between paging and segmentation?" Paging divides memory into fixed-size, logically meaningless chunks; segmentation divides memory into variable-sized, logically meaningful units like code, stack, and data.
  • "What does a segment table entry store?" A base address and a limit (size) for that segment.
  • "What happens if an offset exceeds a segment's limit?" The hardware raises a protection fault, since the access is outside that segment's legitimate bounds.
  • "Why might segmentation be more natural for a compiler to reason about than paging?" Because segments map directly to logical program units (code, stack, a specific data structure) a compiler already understands, whereas pages are an arbitrary fixed-size slice with no inherent logical meaning.

Key takeaways

  • Segmentation divides memory into variable-sized, logically meaningful units instead of paging's fixed-size chunks.
  • A segment table stores each segment's base address and limit for translation and bounds checking.
  • An offset beyond a segment's limit triggers a protection fault.
  • Modern systems mostly use paging for physical storage, but still logically organize a process's memory into segment-like regions (code, stack, heap) for permissions and organization.
All 205 OS interview questions

Browse all Operating Systems topics

Log in to save which topics you've read across every subject.

Frequently asked questions

What's the difference between paging and segmentation?

Paging divides memory into fixed-size, logically meaningless chunks; segmentation divides memory into variable-sized, logically meaningful units like code, stack, and data.

What does a segment table entry store?

A base address and a limit (size) for that segment.

What happens if an offset exceeds a segment's limit?

The hardware raises a protection fault, since the access is outside that segment's legitimate bounds.

Why might segmentation be more natural for a compiler to reason about than paging?

Because segments map directly to logical program units (code, stack, a specific data structure) a compiler already understands, whereas pages are an arbitrary fixed-size slice with no inherent logical meaning.