4.2.27Operating Systems

Segmentation — segment table, protection

1,873 words9 min readdifficulty · medium5 backlinks

WHAT is a segment?

The key contrast: in paging all blocks are the same fixed size and the split is invisible to the programmer. In segmentation blocks are different sizes and visible/meaningful to the programmer.


HOW the segment table works (derive the translation)

We need to turn a logical pair (s,d)(s, d) into a real physical address. Let's build the machinery from scratch.

Step 1 — Where does segment ss live in physical memory? Each segment is contiguous, so we only need its starting physical address. Call it the base bsb_s.

Step 2 — How big is segment ss? Because segments vary in length, we must store each segment's limit (length) LsL_s to stop a program reading past its end.

So per segment we store two numbers: (base, limit)(\text{base},\ \text{limit}). The table of all these pairs is the segment table. Register STBR (Segment Table Base Register) points to the table; STLR (Segment Table Length Register) says how many entries are valid.

Step 3 — The translation formula

Figure — Segmentation — segment table, protection

Protection — the real payoff

Each segment-table entry is extended:

Field Meaning
base bsb_s physical start address
limit LsL_s length (bounds check)
R/W/X bits read / write / execute permissions
valid bit is this segment present/legal?

Worked Examples


Common Mistakes (Steel-manned)


Flashcards

What two parts make up a segmentation logical address?
Segment number ss and offset/displacement dd.
What two fields are the minimum per segment-table entry?
Base (physical start) and limit (length).
Write the address-translation rule for (s,d)(s,d).
If d<Lsd < L_s then physical =bs+d= b_s + d; else trap (segmentation fault).
Why is the bounds check strict (d<Lsd < L_s, not \le)?
Valid offsets are 0..Ls10..L_s-1; allowing d=Lsd=L_s reads into the next segment.
Which register points to the segment table, and which gives its length?
STBR (base) and STLR (length).
How does segmentation enable safe code sharing?
Two processes' table entries point to the same base/limit with R/X permission → one shared copy.
What protection bits live in a segment entry?
Read, Write, Execute (and a valid bit).
Why does a write to a R/X code segment trap even if the offset is in bounds?
Bounds OK ≠ permission OK; write permission bit is 0 → protection fault.
What fragmentation type does pure segmentation suffer?
External fragmentation (variable-size holes); no internal fragmentation.
Segmentation vs paging — who decides block size?
Segmentation: programmer/logical (variable). Paging: system (fixed).

Recall Feynman: explain to a 12-year-old

Imagine your school stuff is in labeled boxes: one "Homework" box, one "Lunch" box, one "Games" box. To find something you say "Box: Games, item 3" instead of memorizing one giant number. The teacher (OS) keeps a list saying where each box sits on the shelf (base) and how big it is (limit). If you ask for item 50 from a box that only has 10 things, the teacher stops you — that's a segmentation fault. And the "Games" box might be look-only (no writing) — try to scribble in it and you get blocked. That label-per-box idea is segmentation, and the box rules are protection.

Connections

  • Paging — page table, TLB — fixed-size counterpart; combine into Segmented Paging.
  • Memory Management — base & limit registers — segmentation = many base/limit pairs.
  • External vs Internal Fragmentation — segmentation → external; paging → internal.
  • Address Translation & MMU — hardware performing bs+db_s + d.
  • Shared Libraries & Dynamic Linking — built on shared read-only segments.
  • Protection & Memory Safety — R/W/X bits, segmentation fault traps.

Concept Map

split into

addressed by

s indexes

points to

bounds valid entries

stores base b_s

stores limit L_s

check d < L_s

no, trap

yes

physical = b_s + d

holds permission bits

per-unit R/W/X

Program logical units

Segments

Logical address (s, d)

Segment table

STBR register

STLR register

Base

Limit

Valid?

Segmentation fault

Physical address

Protection

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Dekho, segmentation ka core idea ye hai ki ek program ek single blob nahi hota — usme alag-alag logical pieces hote hain: code, stack, heap, ek array, ek library. Har piece ko hum ek segment bolte hain. Isliye har address do parts ka hota hai: (s,d)(s, d)s matlab kaunsa segment, aur d matlab us segment ke andar kitna offset (displacement). Programmer ki soch ke hisaab se ye natural hai, kyunki tum sochte ho "math wale segment ka function", na ki ek raw address number.

Ab translation kaise hota hai? OS ek segment table rakhta hai jisme har segment ke liye do cheezein store hoti hain — base (physical memory me kahan se start hota hai) aur limit (segment kitna lamba hai). Jab CPU ko (s,d)(s,d) milta hai, pehle wo check karta hai d < limit. Agar offset limit se bada ya barabar hai, toh segmentation fault — kyunki tum apne segment ke bahar jaa rahe ho. Agar theek hai, toh physical address =base+d= base + d. Bas itna simple, lekin yahi do-step (bounds check, phir base add) puri kahani hai.

Protection yahin sabse mast feature hai. Har segment-table entry me R/W/X bits bhi hote hain. Code segment ko sirf Read/Execute do, Write mat do — toh agar koi galti se ya hack karke code ke upar likhne ki koshish kare, hardware turant protection fault maar dega, chahe offset bounds ke andar hi kyun na ho. Aur sharing ka faida: do processes same code segment ke base/limit ko point kar sakte hain (sirf R/X ke saath) — ek hi copy RAM me, dono safely use karte hain. Isliye shared libraries elegant ban gayi.

Ek important yaad rakhne wali baat: bounds check strict < hota hai, <= nahi — kyunki offsets 0 se start hote hain, toh valid offsets sirf 0 se limit-1 tak. Aur ek galat-fehmi clear kar lo: segmentation me external fragmentation hoti hai (variable-size holes), internal nahi — paging ulta hai.

Test yourself — Operating Systems

Connections