4.2.27 · Coding › Operating Systems
Intuition The Big Picture (WHY)
Ek program ek giant blob nahi hota — yeh logical pieces se bana hota hai: code, stack, heap, global data, ek library, etc. Segmentation ek memory-management scheme hai jo memory ko programmer ki nazar se dekhta hai: har logical piece apna khud ka segment hota hai jiska apna naam (number) aur apni length hoti hai.
Yeh raw addresses se behtar kyun hai: programmer sochta hai "math segment mein function sqrt", na ki "address 0x4FA2". OS har segment ko physical memory mein kahin bhi rakh sakta hai, har ek ko alag tarike se protect kar sakta hai, aur unhe independently grow kar sakta hai.
Ek segment ek variable-length contiguous block hota hai address space ka jo program ke ek logical unit (code, stack, heap, ek single array, ek procedure...) ko represent karta hai. Har logical address ek two-part address hota hai:
logical address = ( s , d )
jahaan s = segment number aur d = us segment ke andar offset (displacement) hai.
Key contrast yeh hai: paging mein saare blocks ek hi fixed size ke hote hain aur yeh split programmer ko dikhai nahi deta. Segmentation mein blocks alag-alag sizes ke hote hain aur programmer ko visible/meaningful hote hain.
Hume logical pair ( s , d ) ko ek real physical address mein convert karna hai. Chaliye machinery scratch se banate hain.
Step 1 — Segment s physical memory mein kahan rehta hai?
Har segment contiguous hota hai, toh hume sirf uska starting physical address chahiye. Ise base b s kehte hain.
Step 2 — Segment s kitna bada hai?
Kyunki segments ki length alag-alag hoti hai, hume har segment ka limit (length) L s store karna padta hai taaki program apne end ke baad read na kar sake.
Toh har segment ke liye hum do numbers store karte hain: ( base , limit ) . Inhi pairs ki table ko segment table kehte hain. Register STBR (Segment Table Base Register) table ko point karta hai; STLR (Segment Table Length Register) batata hai ki kitni entries valid hain.
Step 3 — Translation formula
Intuition WHY protection naturally segmentation mein rehti hai
Kyunki ek segment = ek logical unit, hum permission bits ek saath poore unit par attach kar sakte hain. Code read/execute hona chahiye lekin writable nahi; ek constant table read-only honi chahiye; stack read/write hona chahiye lekin executable nahi. Paging bhi yeh kar sakta hai, lekin segmentation mein yeh natural lagta hai kyunki har segment ka pehle se ek hi meaning hota hai.
Har segment-table entry ko extend kiya jaata hai:
Field
Meaning
base b s
physical start address
limit L s
length (bounds check)
R/W/X bits
read / write / execute permissions
valid bit
kya yeh segment present/legal hai?
Intuition Sharing for free
Do processes same code segment ko map kar sakti hain apni segment-table entries ko same base & limit par point karke, dono R/X (not W) permission ke saath. RAM mein ek hi copy, safely shared. Isi wajah se segmentation ne shared libraries ko elegant banaya.
Worked example Example 1 — valid translation
Segment table:
seg
base
limit
0
1400
1000
1
6300
400
2
4300
1100
Logical address ( 2 , 500 ) translate karo.
Limit pehle check kyun? Confirm karna hai ki 500 < 1100 . ✓ (yeh andar hai) — Kyun? next segment ko protect karta hai.
Base add kyun? physical = 4300 + 500 = 4800 . Kyun? offset seg start ke relative hota hai.
Worked example Example 2 — out-of-bounds
( 1 , 400 ) translate karo.
Seg 1 ka limit 400 hai. Check karo 400 < 400 ? Nahi (strictly less hona chahiye).
Yeh trap kyun karta hai: offset 400 401st byte hoga; segment mein sirf bytes 0..399 hain. → segmentation fault . Strict < kyun? L length ke segment mein valid offsets 0 se L − 1 tak hote hain.
Worked example Example 3 — protection violation
Seg 0 code segment hai jiske permissions = R/X (no W) hain. Ek program store r1, (0, 20) execute karta hai.
Bounds: 20 < 1000 ✓.
Permission: seg 0 par write? perm W ( 0 ) = 0 . ✗
Offset legal hone ke bawajood trap kyun? Bounds ≠ permission. Byte exist karta hai lekin code mein likhna forbidden hai (self-modifying-code attacks/bugs se bachata hai). → protection fault .
Common mistake "Bounds check ke liye
d ≤ L s use karo."
Yeh sahi kyun lagta hai: "limit hi size hai, toh offset size tak ho sakta hai." Fix: offsets 0-indexed hote hain, toh largest valid offset L s − 1 hai. d = L s allow karne se next segment ka ek byte read ho jaata hai. Strict < use karo.
Common mistake "Segmentation mein internal fragmentation hota hai."
Yeh sahi kyun lagta hai: paging mein internal fragmentation hota hai, aur hum dono schemes ko ek saath lump kar dete hain. Fix: segments apni zaroorat ke hisaab se exactly size ke hote hain → koi internal fragmentation nahi, lekin external fragmentation hota hai (variable-size segments ke beech free holes, compaction ki zaroorat pad sakti hai). Paging iska opposite hai.
Common mistake "Segment table CPU registers mein hoti hai jaise base/limit."
Yeh sahi kyun lagta hai: plain base+limit relocation ek register pair use karta hai. Fix: zyada segments hone par table memory mein rehti hai, STBR se point ki jaati hai; har access ko ek extra memory read ki zaroorat pad sakti hai (aksar TLB-jaisi associative store mein cached hoti hai).
Segmentation logical address ke do parts kya hote hain? Segment number s aur offset/displacement d .
Segment-table entry mein minimum kaun se do fields hote hain? Base (physical start) aur limit (length).
( s , d ) ke liye address-translation rule likhiye.Agar d < L s toh physical = b s + d ; warna trap (segmentation fault).
Bounds check strict kyun hota hai (d < L s , ≤ nahi)? Valid offsets 0.. L s − 1 hote hain; d = L s allow karne se next segment mein read ho jaata hai.
Segment table ko kaunsa register point karta hai, aur uski length kaun deta hai? STBR (base) aur STLR (length).
Segmentation safe code sharing kaise enable karta hai? Do processes ki table entries same base/limit ko R/X permission ke saath point karti hain → ek shared copy.
Ek segment entry mein kaunse protection bits hote hain? Read, Write, Execute (aur ek valid bit).
R/X code segment par write kyun trap karta hai jab offset in-bounds ho? Bounds OK ≠ permission OK; write permission bit 0 hai → protection fault.
Pure segmentation mein kaun si fragmentation hoti hai? External fragmentation (variable-size holes); koi internal fragmentation nahi.
Segmentation vs paging — block size kaun decide karta hai? Segmentation: programmer/logical (variable). Paging: system (fixed).
Recall Feynman: ek 12-saal ke bachche ko samjhao
Socho tumhara school ka saman labelled boxes mein hai: ek "Homework" box, ek "Lunch" box, ek "Games" box. Kuch dhundhne ke liye tum kehte ho "Box: Games, item 3" instead of ek giant number yaad karne ke. Teacher (OS) ek list rakhti hai jo batati hai har box shelf par kahan hai (base ) aur kitni badi hai (limit ). Agar tum ek aise box se item 50 maango jisme sirf 10 cheezein hain, toh teacher tumhe rokegi — yahi segmentation fault hai. Aur "Games" box sirf dekhne ke liye ho sakta hai (likhna allowed nahi) — isme likhne ki koshish karo aur tum block ho jaoge. Har box ka yeh label idea segmentation hai, aur box ke rules protection hain.
Mnemonic Entry aur check yaad rakhne ka tarika
"BLV-RWX" = B ase, L imit, V alid + R ead/W rite/eX ecute.
Check order: "Valid? In-bounds? Allowed?" (V → d < L → perm).
Paging — page table, TLB — fixed-size counterpart; combine into Segmented Paging .
Memory Management — base & limit registers — segmentation = bahut saare base/limit pairs.
External vs Internal Fragmentation — segmentation → external; paging → internal.
Address Translation & MMU — hardware jo b s + d perform karta hai.
Shared Libraries & Dynamic Linking — shared read-only segments par built.
Protection & Memory Safety — R/W/X bits, segmentation fault traps.