Skip to content

Programming

Understanding Linux File Descriptors

A practical explanation of file descriptors: the small integers behind every file, socket, and pipe on a Unix system.

3 min read

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.

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 input
  • 1 — standard output
  • 2 — 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.

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/fd exposes a process’s descriptors as symlinks, which makes debugging straightforward: ls -l /proc/<pid>/fd shows exactly what a process has open.
Terminal window
$ ls -l /proc/self/fd
total 0
lrwx------ 1 user user 64 Nov 26 02:00 0 -> /dev/pts/0
lrwx------ 1 user user 64 Nov 26 02:00 1 -> /dev/pts/0
lrwx------ 1 user user 64 Nov 26 02:00 2 -> /dev/pts/0

There 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.

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.

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”.