4.2.32Operating Systems

File operations — open, read, write, seek, close

2,030 words9 min readdifficulty · medium

WHY do we need these operations at all?

The 5 core operations form a lifecycle:

open  →  (read / write / seek)*  →  close
  • open — set up the bookkeeping, get a handle.
  • read/write — transfer bytes between your buffer and the file.
  • seek — move where in the file the next read/write happens.
  • close — tear down the bookkeeping, flush data.

WHAT exactly is a file descriptor?


Figure — File operations — open, read, write, seek, close

HOW each call works (derived from first principles)

1. open

2. read

3. write

4. seek (lseek)

5. close


Worked examples


Common mistakes


Recall Feynman: explain to a 12-year-old

Imagine a library where you can't go inside. You ask the librarian (the OS) for a book by name — she gives you a numbered claim-ticket (the file descriptor). With that ticket you say "read me the next page" or "write this page" and she remembers which page you're on (the offset). If you say "skip to page 50," that's seek — she just flips a bookmark, super fast, no reading. When you're done you hand the ticket back (close) so someone else can use it.


Active recall

What does open return on success, and on failure?
A non-negative file descriptor (smallest unused) on success; -1 (with errno set) on failure.
What does read returning 0 mean?
End of file (EOF) — not an error. Errors return -1.
Why might read/write transfer fewer bytes than requested?
EOF nearing, pipe/socket has less data ready, disk full, or interrupting signal → so you must check the return value and loop.
What state does the open file description hold?
The current byte offset, access mode, and a pointer to the file's inode.
Compute lseek(fd, -4, SEEK_END) for a 20-byte file.
newpos = size + offset = 20 + (-4) = 16.
Does lseek transfer data or move the disk head?
No — it only updates the in-kernel offset integer; it's effectively free.
Which flag makes every write go to end-of-file atomically?
O_APPEND.
Which flags are needed to create a file if absent and what extra argument matters?
O_CREAT; the mode (e.g. 0644) permission argument is only used when creating.
What happens if you never close fds in a long-running program?
You leak descriptors; eventually new open calls fail (ulimit/EMFILE).
What is a sparse file and how is it produced?
A file with "holes" of implicit zero bytes, created by seeking past EOF and then writing.
Convention: what are fds 0, 1, 2?
0 = stdin, 1 = stdout, 2 = stderr.

Connections

  • File system & inodesopen resolves path → inode.
  • System calls and the kernel boundary — these are all syscalls.
  • File descriptor table — per-process table the fd indexes.
  • Buffered I/O (fopen/fread) — stdio wrappers built on top of these.
  • Pipes and sockets — also use read/write but are non-seekable.
  • ulimit and resource limits — why closing matters.

Concept Map

cannot poke disk, uses syscalls

manages

returns smallest unused

indexes

stores

advances

advances

sets manually

uses

uses

flushes and frees

Process in RAM

Kernel as librarian

File descriptor int

Open file description

Current offset

open

read

write

lseek

close

File as byte array

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Dekho, file disk pe ek simple byte ka array hoti hai — kuch magic nahi. Tum apne program se disk ko directly touch nahi kar sakte, kyunki disk shared hardware hai aur OS uska gatekeeper hai. Isliye tum OS ko bolte ho open("file"), aur OS tumhe ek chhota number deta hai — file descriptor (fd). Yeh fd basically ek claim-ticket hai jisse aage ke saare kaam hote hain. fd 0,1,2 by default stdin, stdout, stderr hote hain, aur naya open hamesha sabse chhota free number deta hai.

Ab fd ke peeche OS ek offset yaad rakhta hai — yani abhi tum file me kis byte pe ho. read karoge to offset se aage ke bytes aate hain aur offset apne aap aage badh jaata hai. write ulta — buffer se file me bytes likhte ho. Important baat: read/write jitna tumne maanga utna hi return kare yeh zaroori nahi — short read/write hota hai, isliye hamesha return value check karo. read ka 0 matlab EOF (file khatam), error nahi; error -1 hota hai. Yeh galti sabse common hai exams aur interviews dono me.

seek (yani lseek) ek smart cheez hai: yeh data move nahi karta, sirf offset ka number badalta hai — isliye yeh almost free hai, koi disk head physically move karne wali baat nahi. SEEK_SET, SEEK_CUR, SEEK_END se tum absolute ya relative jump kar sakte ho — isi se random access milta hai. Aur jab kaam ho jaaye to close zaroor karo, warna fd leak hoga aur ek time pe naye file open hona band ho jaayenge (ulimit limit).

Yaad rakhne ka tareeka: Open → (Read/Write/Seek baar baar) → Close. Bookend do hi cheezein — open aur close — beech ka teen kuch bhi repeat ho sakta hai. Yeh lifecycle dimaag me fit ho gaya to file I/O ka 80% kaam clear hai.

Go deeper — visual, from zero

Test yourself — Operating Systems

Connections