~/notes/file-descriptors

File Descriptors

Explains Linux File Descriptor

Jul 13, 2025

When we think of a file, we typically picture a regular file in data storage, a collection of bytes written to a disk.

However, in Unix/Linux, everything is treated as a file. This abstraction simplifies how the operating system interacts with various resources. Whether it’s a standard file, a network socket, a directory, a symbolic link, or a device, the OS can perform the same basic operations: open, read, write, and close.

A file descriptor is simply a reference to any open resource. When a process opens a resource, the kernel creates a file descriptor as an interface for communication, allowing the process to perform operations on it.

diagram
File 1File 2File 1File 3Global Inode Table

Inode number

Inode number

Global File Table

Open file entry
mode, permission, offset

Open file entry

Open file entry

Per-Process Table (fd Table)

Process 1
fd: 0 1 2 3

Process 2
fd: 0 1 2 3 4

Process 3
fd: 0 1 2 ...

Hard Disk
Data Blocks

File 1File 2File 1File 3Global Inode Table

Inode number

Inode number

Global File Table

Open file entry
mode, permission, offset

Open file entry

Open file entry

Per-Process Table (fd Table)

Process 1
fd: 0 1 2 3

Process 2
fd: 0 1 2 3 4

Process 3
fd: 0 1 2 ...

Hard Disk
Data Blocks