🧠 BrainStuffer
Linux File Descriptors
Every I/O resource as a number — types, operations & redirection

File Descriptors — everything is a file

A file descriptor (FD) is just a small non-negative integer that your process uses as a handle to any open I/O resource. The kernel keeps a table mapping those integers to the actual underlying objects — whether that's a file on disk, a pipe, a network socket, a terminal, or a special device.

When you call open(), socket(), pipe(), or accept(), the kernel finds the lowest available slot in your process's FD table and returns that number. You pass it to read(), write(), close(), etc.

Core concepts

📜

Process FD table

Every process has its own array of FD slots (default limit 1024, raised with ulimit -n). Slot 0, 1, 2 are pre-filled by the shell with stdin, stdout, stderr. You fill the rest with open() calls.

💾

Open file table

Each FD slot points to a kernel-side open file description (OFD). The OFD stores the file offset and open flags. Two FDs (even in different processes) can share one OFD — that's what dup() and fork() create.

📄

Inode / resource

The OFD points to the inode (for files) or kernel object (for pipes, sockets, etc.). Multiple OFDs can reference the same inode — when all FDs pointing to it are closed, the kernel frees the resource.

📋

Everything is a file

Linux extends the UNIX philosophy: sockets, pipes, terminals, /dev/null, /proc entries, timerfd, eventfd — all appear as FDs. Your code calls the same read()/write() regardless of what lies underneath.

Three-tier structure — click a row to trace the link

Every FD goes through two indirections before reaching the actual resource. Click any row to highlight the chain.

Click a row above to see how the FD connects through to the underlying resource.

FD type colour legend — used throughout this page

● stdin ● stdout ● stderr ● regular file ● pipe ● socket ● tty/terminal ● /dev/null ● epoll/event

Pick an FD type to explore

Interactive FD Table Simulator

Simulate a process's file descriptor table. Use the buttons on the right to open resources, create pipes, and close FDs. Watch how the kernel fills the lowest available slot.

fd
type
resource
Operations

Key FD Operations — Step Through

Step 1 / 8
FD table

Shell Redirection — how it works under the hood

When you write ./prog > out.txt, the shell does NOT pass a flag to the program. Instead it manipulates the FD table using open() and dup2() between fork() and exec(). The program runs completely unaware; it just writes to fd 1 as always.

Click any redirection to see exactly what FD moves happen.

Quiz — Linux File Descriptors