4.2.34 · HinglishOperating Systems

File allocation — contiguous, linked, indexed (inode)

2,344 words11 min readRead in English

4.2.34 · Coding › Operating Systems


WHY this problem even exists

  • WHAT chahiye: kai files store karo, koi bhi byte quickly read/write karo, files ko grow/shrink hone do.
  • WHY mushkil hai: blocks disk par wahan allocate hone chahiye jahan free space actually ho. Agar hum demand karein ki file ek solid run mein ho, toh growth toot jaati hai. Agar freely scatter karein, toh "byte 1,000,000" dhundhna slow ho jaata hai.
  • HOW schemes differ karti hain: jahan woh "next block" pointer store karti hain — data ke andar, ek table mein, ya ek dedicated index block mein.

1. Contiguous Allocation

Pros: access, minimal metadata, sequential reads ke liye great (no seeks). Cons: external fragmentation (free space chhote unusable holes mein split ho jaati hai); file size pehle se jaanani/andazni padti hai; file grow karne ke liye use poora move karna pad sakta hai.


2. Linked Allocation

FAT variant (smart fix): saare "next" pointers ko data blocks se bahar nikaalo aur ek in-memory table mein daalo, yaani File Allocation Table. Tab chain RAM mein traverse ho sakti hai bina disk reads ke, aur blocks poore bytes data ban jaate hain.


3. Indexed Allocation (inode)

Size limit aur Unix isko kaise beat karta hai

Figure — File allocation — contiguous, linked, indexed (inode)

Comparison Table (jo 20% sabse zyada matter karta hai)

Scheme Random access External frag? Grows easily? Metadata
Contiguous Haan Nahi start+len
Linked Nahi Haan first block
Indexed (inode) Nahi Haan index block(s)

Flashcards

Contiguous file ko poori tarah describe karne wale do numbers kaunse hain?
Start block address aur length (number of blocks).
Contiguous allocation ke liye random-access formula kya hai?
phys(i) = start_block + i, giving O(1) access.
Contiguous allocation ka main drawback kya hai?
External fragmentation + file size pehle se jaanni padti hai.
Linked allocation mein random access O(n) kyun hai?
Pehle block se pointer chain follow karni padti hai; block i ka address block i−1 padhne ke baad hi pata chalta hai.
Plain linked allocation mein usable data per block B − p kyun hai?
Next-block pointer (p bytes) block ke andar store hota hai, data ki jagah chura leta hai.
FAT scheme alag kya karta hai?
Saare "next" pointers ko ek in-memory File Allocation Table mein le jaata hai, taaki chains RAM mein traverse ho sakein aur blocks full data hold kar sakein.
Inode kya hota hai?
Unix index node — per-file structure jo metadata plus data blocks ki taraf pointers ka array (direct + indirect) hold karta hai.
Unix 12 direct pointers kyun use karta hai kisi bhi indirection se pehle?
Zyaadatar files chhoti hoti hain; direct pointers common-case tiny files ke liye zero-overhead access dete hain (80/20).
Pointers-per-block k=B/p ke saath, double indirection kya cover karta hai?
k² data blocks (k single-indirect blocks × k pointers each).
Unix inode ke liye max file size formula kya hai?
12·B + k·B + k²·B + k³·B jahan k = B/p.
Triple-indirect region mein ek block padhne ke liye kitne disk reads lagte hain?
3 pointer-block reads (single→double→triple chain) + 1 data read = depend karta hai; triple ke liye 3 index levels padho phir data.
Konsi scheme best sequential read performance deti hai aur kyun?
Contiguous — blocks physically adjacent hain, disk seeks minimize hoti hain.

Recall Feynman: ek 12-saal ke bachche ko explain karo

Socho tumhare khilone (file) boxes ki ek row (disk blocks) mein store karne hain.

  • Contiguous: saare khilone ek doosre ke saath wale boxes mein rakho. Khilona #5 dhundhna aasaan hai (start se count karo), lekin baad mein zyada khilone aane par unke paas jagah nahi ho sakti.
  • Linked: har box mein ek note hota hai jisme likha hota hai "agla khilona box #14 mein hai." Khilone kahin bhi store ho sakte hain, lekin khilona #5 tak pahunchne ke liye notes follow karte hue boxes ek-ek karke kholne padte hain.
  • Indexed (inode): ek special list-box rakho jo bataye ki har khilona exactly kis box mein hai. List dekho, seedha khilone #5 par jump karo. BAHUT bade khilone collections ke liye, list aur lists ki taraf point karti hai (indirect), jaise menus ka menu.

Connections

  • Disk Scheduling — contiguous ka no-seek advantage isi par depend karta hai.
  • External vs Internal Fragmentation — yahan ka core trade-off.
  • File Systems (ext4, FAT, NTFS) — in schemes ki real implementations.
  • Free Space Management (bitmap, free list) — OS blocks allocate karne ke liye unhe kaise dhundhta hai.
  • Virtual Memory Paging — page tables indexed allocation ka in-memory analogue hain.
  • Directory Structure — start block / inode ki taraf pointer store karta hai.

Concept Map

needs

no scheme wins all

scheme 1

scheme 2

scheme 3

stores

formula phys i = b plus i

suffers

block holds

must follow chain

advantage

uses

gives

File allocation problem

Fast access no waste easy grow

Three schemes exist

Contiguous

Linked

Indexed inode

Start block and length

O of 1 random access

External fragmentation

Data plus next pointer

O of n access

No external fragmentation

Index block of pointers

O of 1 access no fragmentation