Yeh page 4.2.32 File operations — open, read, write, seek, close ka exhaustive worked-examples companion hai. Yahan hum sirf baat nahi karte ki open, read, write, lseek, close kaise behave karte hain — balki unhe har possible case se guzarte hain — offset ka har sign, har degenerate input, har limiting value — jab tak koi bhi scenario tumhe surprise na kar sake.
In paanch calls se jo bhi situation aa sakti hai woh neeche kisi na kisi cell mein hai. Aane wale examples mein se har ek uss cell ka label liye hoga jise woh cover karta hai, aur milke yeh sab cells cover karte hain.
Cell
Case class
Tricky kyun hai
A
lseek positive offset, SEEK_SET
absolute jump forward
B
lseek negative offset, SEEK_END
"N bytes before end" — sign matter karta hai
C
lseek offset past EOF + write
ek sparse file (hole) banta hai
D
lseek exact boundary par (offset = 0, offset = size)
Examples se pehle, ek image. Offset ek single non-negative integer hai — file ke start se bytes mein measure kiya gaya ek bookmark position. lseek jo bhi karta hai woh iss ek number par arithmetic hai.
SEEK_END formula apply karo.newpos = size + offset = 20 + (-4) = 16.
Yeh step kyun?SEEK_END position = size (20) se start karta hai. Ek negative offset wahan se peeche chalta hai — yeh "last N bytes" ke liye the idiom hai.
4 bytes read karo offsets 16, 17, 18, 19 par → final chaar bytes. Offset 16 + 4 = 20 = size par khatam hota hai (bookmark ab EOF par hai).
Yeh step kyun?size - 4 last chaar indices mein se pehla hai; size tak read karne se exactly tail consume ho jaati hai.
-25 wala degenerate case:newpos = 20 + (-25) = -5 < 0. Negative result illegal hai — lseek returns -1 aur errno = EINVAL set karta hai. Kuch bhi move nahi hota.
Yeh step kyun? Cell B ko invalid sub-case bhi cover karna chahiye: file ke start se pehle seek nahi kar sakte.
Verify: last valid index size-1 = 19 hai; hamare chaar indices 19 par khatam hote hain. ✔ -25 ke liye: 20-25 = -5, aur bookmark negative nahi ho sakta, toh failure sahi hai. ✔
Size-0 file par 100 tak seek karo.newpos = 100. Yeh allowed hai — EOF se aage seek karna legal hai; isse file abhi grow nahi hoti.
Yeh step kyun?lseek sirf offset integer ko touch karta hai (galti-killer: yeh koi data move nahi karta), isliye yeh happily current end ke baad land kar jaata hai.
Offset 100 par 1 byte write karo. Ab file kam se kam 100 + 1 = 101 bytes ki honi chahiye. Bytes 0–99 kabhi write hi nahi kiye gaye.
Yeh step kyun? Old size (0) aur write position (100) ke beech ka gap ek hole ban jaata hai.
Hole zeros ki tarah read back hota hai. Bytes 0–99 par koi bhi read\texttt{0x00} return karta hai. Disk par hole koi real blocks occupy nahi karta — yahi ek sparse file hai.
Yeh step kyun? Filesystem record karta hai "yeh blocks implicitly zero hain" rather than 100 literal zero bytes store karne ke.
Used slots count karo. fds 0,1,2 stdin/stdout/stderr hain → 3 used, 1024 - 3 = 1021 free.
Yeh step kyun? Limit sab open descriptors count karta hai, including teeno inherited wale.
Har open ek slot consume karta hai (returns 3, phir 4, 5, … 1023). 1021 successful opens ke baad table full ho jaata hai.
Yeh step kyun?close ke bina, reference count kabhi drop nahi karta, toh slots kabhi return nahi hote.
Agla (1022nd) open fail hota hai with -1, errno = EMFILE ("Too many open files").
Yeh step kyun? Isliye descriptors leak karna eventually ek long-running server ko tod deta hai — dekho ulimit and resource limits.
Verify: successful opens = 1024 - 3 = 1021; 1022nd fail hota hai. ✔ Sabse bada fd hand out kiya = 1023 (last free index). ✔
A ::: Example 1 (SEEK_SET forward)
B ::: Example 2 (SEEK_END negative, incl. invalid -25)
C ::: Example 3 (seek past EOF → sparse hole)
D ::: Examples 1 & 2 (offset at boundaries 0 / size)
E ::: Example 4 (short read, n < count)
F ::: Example 4 (read returns 0 = EOF)
G ::: Examples 6 & 7 (return -1, errno set)
H ::: Example 5 (short-write loop)
I ::: Example 6 (missing file, no O_CREAT → ENOENT)
J ::: Example 7 (fd exhaustion → EMFILE)
K ::: Example 8 (concurrent O_APPEND logging)
L ::: Example 9 (mixed SEEK_CUR arithmetic)
Is page ke saath saath yeh bhi open rakhna chahoge: System calls and the kernel boundary, File descriptor table, File system & inodes, aur buffered contrast ke liye Buffered I/O (fopen/fread). Concurrency example Pipes and sockets se connect karta hai (jahan short reads norm hain, exception nahi).