File descriptor
Файловый дескриптор
A program on Linux or another Unix-like system uses a small integer to refer to an open file, socket, or pipe. That number is called a file descriptor.
What it is
A file descriptor (FD) is a number that identifies an entry in a process's descriptor table. Once a program opens a file, it can pass that number to read() or write() instead of supplying the path every time. Sockets and pipes also have descriptors, even when they have no filename. The number is local to the process: FD 4 might be a log file in one process and a network connection in another.
The number and what it points to
A successful open() returns the lowest available descriptor number. The table entry points to a kernel object called an open file description. For a regular file, this object holds the current file offset and status flags. Opening the same path twice creates two open file descriptions, so reading through one descriptor does not move the other's offset. By contrast, dup() creates a second descriptor for the same description, so both descriptors share the offset.
Calling close() frees the descriptor number, not necessarily the underlying resource: another descriptor can still refer to it. The number can also be reused, so keeping a copy of the integer does not make it safe to use after closing it.
Example: sending output to a file
Programs launched from a shell normally start with FD 0 for standard input, FD 1 for standard output, and FD 2 for standard error. Try this in Bash:
ls /tmp > files.txt 2>&1
The shell first redirects FD 1 to files.txt, then makes FD 2 refer to the same open file description as FD 1. Both normal output and errors go into the file. Order matters: with 2>&1 > files.txt, FD 2 gets a copy of the original output destination before FD 1 is redirected.
Why operators care
A server can run out of descriptors without opening many disk files. Each accepted connection uses a socket descriptor, and the server may already have listening sockets, pipes, and logs open. When the process hits its descriptor limit, calls such as open() and accept() can fail with EMFILE, commonly reported as “Too many open files.” On Linux, inspect /proc/<PID>/fd to see what is open and /proc/<PID>/limits to check the running service's limit. ulimit -n reports the current shell's soft limit, which may differ from the service's.
Compare descriptor usage with traffic over time. A higher count during a traffic spike can be normal; a count that keeps climbing under steady load can indicate that files or connections are not being closed. Raising the limit can handle legitimate demand, but it only delays the next failure if there is a leak.
In interviews
A common question is what happens to descriptors across fork() and exec(). After fork(), parent and child have separate descriptor tables, but inherited entries refer to the same open file descriptions. Closing an inherited descriptor in the child does not close the parent's copy. A successful exec() keeps descriptors open unless they are marked close-on-exec (FD_CLOEXEC), which is how a program prevents an unrelated executable from inheriting resources it does not need.