This is a classic operating system design question—especially relevant in systems like UNIX/Linux. Let’s break it down clearly:
🧠 Overview of File Descriptor Management in Unix-like OS
When you call something like write(fd, buf, 256), the kernel looks up various internal data structures to perform the write. These include:
Per-process open file table (also called the file descriptor table).
System-wide open file table (also just called the open file table or file object table).
Inode table (system-wide metadata table for files).
📁 1. Per-process Open File Table
Each process has its own file descriptor table, usually represented as an array indexed by integers (fd values).
🔐 What's stored?
An entry per open file.
Each entry maps a file descriptor (e.g., fd = 3) to a pointer to a file object in the system open file table.
🧭 This includes:
FieldDescriptionfdIndex into the per-process file descriptor table.file*Pointer to the system-wide file object (struct file).
Each process has its own table, so you can have the same fd = 3 in two processes pointing to different files.
🗃️ 2. System-wide Open File Table (File Objects)
This is shared across all processes and contains actual state of the open file.
📦 What's stored?
FieldDescriptionFile offsetCurrent read/write position (file->f_pos)Access modeRead/write/append flags (O_RDONLY, O_WRONLY, etc.)Pointer to inode (vnode)The actual file metadataReference countHow many FDs (or processes) share this file object
Example: Two processes can share a file object via dup() or fork(). They will then share the same offset.
📑 3. Inode Table
The inode or vnode represents the underlying file itself on disk.
📋 What's stored?
FieldDescriptionFile typeRegular file, directory, pipe, etc.File sizeByte sizePermissionsOwner, group, modeTimestampsAccess/mod/changeDisk block pointersWhere data lives on disk
🛠️ What happens during write(fd, buf, 256)?
Step-by-step:
Lookup in per-process file descriptor table using fd:
Find pointer to system-wide file object.
Access system open file table (file object):
Check access mode (O_WRONLY or O_RDWR) — error if invalid.
Get current file offset (f_pos).
Get pointer to the underlying inode (file metadata).
Check permissions:
Confirm that the file mode allows writing (via inode metadata and process credentials).
Call filesystem write implementation:
The vfs_write() function (or similar) invokes the underlying file system’s write routine.
Write data from buf into disk or memory cache (page cache).
Update file offset (f_pos) by 256 bytes.
Mark inode dirty (if using a journaling FS, this might also update logs).
🔄 Summary Table
LayerStructureKey Fields Used in write()Per-processFD Tablefd → file* pointerSystem-wideFile Objectf_pos, mode, inode*, refcountSystem-wideInode (vnode)Permissions, size, disk location
🔍 Bonus: Shared Offsets and Duplicates
dup(fd) or fork() → file object is shared → shared offset.
open() twice → two file objects → independent offsets.
Let me know if you want a diagram or want to step through read() or dup() similarly!