Programming
Understanding Linux File Descriptors
A practical explanation of file descriptors: the small integers behind every file, socket, and pipe on a Unix system.
Introduction
Section titled “Introduction”On Linux, almost everything a process does with input and output goes through a file descriptor: a small non-negative integer that identifies an open resource. Files, pipes, sockets, terminals, and event queues are all accessed the same way — through a descriptor. Understanding them explains a surprising amount of Unix behaviour, from shell redirection to how servers handle thousands of connections.
The core concept
Section titled “The core concept”Each process has its own table of open file descriptions, maintained by the kernel. A file descriptor is simply an index into that table. When a process calls open(), pipe(), or socket(), the kernel creates an entry and returns the lowest-numbered free descriptor.
Three descriptors exist from the moment a process starts:
0— standard input1— standard output2— standard error
This is why shell redirection works: program 2> errors.log asks the shell to open errors.log and place it at descriptor 2 before starting the program. The program itself never knows the difference.
How it works
Section titled “How it works”Descriptors are per-process, but the thing they point to can be shared. When a process forks, the child inherits a copy of the parent’s descriptor table, so parent and child can write to the same pipe or socket. When a process calls exec(), descriptors survive by default unless they were marked close-on-exec (O_CLOEXEC).
A few operations make the model powerful:
dup()/dup2()copy a descriptor, so two numbers refer to the same open file description — the mechanism behind redirection and descriptor juggling.close()removes one reference. The underlying resource is released only when the last reference closes./proc/self/fdexposes a process’s descriptors as symlinks, which makes debugging straightforward:ls -l /proc/<pid>/fdshows exactly what a process has open.
$ ls -l /proc/self/fdtotal 0lrwx------ 1 user user 64 Nov 26 02:00 0 -> /dev/pts/0lrwx------ 1 user user 64 Nov 26 02:00 1 -> /dev/pts/0lrwx------ 1 user user 64 Nov 26 02:00 2 -> /dev/pts/0There is a limit on how many descriptors a process may hold, controlled by RLIMIT_NOFILE (visible via ulimit -n). A server that accepts many simultaneous connections must raise this limit or it will start failing with EMFILE — “too many open files” — even though no files are involved.
Why it matters
Section titled “Why it matters”The descriptor abstraction is why Unix programs compose so well. A program that reads from descriptor 0 and writes to descriptor 1 works identically whether its input is a keyboard, a file, or the output of another program. Network servers exploit the same uniformity: a socket is just another descriptor, so the same read(), write(), and poll() calls handle files and network connections alike. Event-driven servers keep thousands of socket descriptors open and wait on all of them at once with epoll.
Misconceptions
Section titled “Misconceptions”A common one is that a descriptor is a file. It is not — it is a reference to an open file description, and several descriptors (in one process or several) can reference the same description, sharing a file offset. Closing a descriptor does not necessarily close the file; it only drops one reference. Conversely, deleting a file’s name with unlink() does not free its disk space while any descriptor still references it — which is why a log file can keep growing on disk after being “deleted”.