File descriptor
A file descriptor is a nonnegative integer used by a process to refer to an open file description for operations such as reading, writing or closing.
A descriptor number is a process-local reference, not a persistent identifier for a pathname or inode. Separate opens can create different open file descriptions for the same file; dup creates another reference to the existing description. [1] [2]
Duplicated descriptors share the file offset and status flags, but not descriptor flags such as close-on-exec. Closing one alias does not close the other. After release, the same number can identify a different file, so a saved integer alone does not prove ownership of the current descriptor. [2] [3]
On Linux, a successful dup2 with distinct source and target numbers replaces the target atomically; separate close then dup leaves an allocation race. Successful replacement clears the target FD_CLOEXEC flag while leaving the source descriptor flag unchanged. A valid dup2(fd, fd) instead does nothing, preserving descriptor flags. An invalid source fails without closing the target. Atomic replacement is not durable storage, and dup2 does not report implicit target-close errors. [2]
Relations
- related
- open-file-description, close, inode, pwrite
Limitations
- Descriptor numbers can be reused after release.
- Sharing a descriptor or open file description does not provide a durable-commit guarantee.
Citations
- Linux man-pages 6.19: open(2), descriptors and open file descriptions
- Linux man-pages 6.19: dup(2), shared state and atomic replacement
- POSIX.1-2024: close(), descriptor deallocation