File Descriptors
In Unix, a file descriptor is a process-unique identifier (handle) for a file or other input/output resource, such as a [Unix pipes](/wiki/Unix pipes/) or network socket.
File descriptors have non-negative integer values.
File descriptors index into a per-process file descriptor table maintained by the kernel, that in turns indexes into a system-wide table of files opened by all process, called the file table. This table records the mode with which the file was opened. It also indexes into a third table called the inode table that describes the actual underlying files.
To perform input or output, the process passes the file descriptor to the kernel through a system call, and the kernel will access the file on behalf of the process.
In other words, when you open a file, the operating system creates an entry to represent that file and store the information about that opened file. These entries are represented by integers. This entry number is the file descriptor. A network socket or a unix domain socket is also represented by integers, called socket descriptors. So it is just an integer that uniquely represents an opened file for the process.
When a file is closed, the descriptor associated gets freed and is available for further allotment. This is why you can run out of file descriptors, if you open a lot of files at once, which will prevent unix systems from running, since they open descriptors in /proc
Remember that FD are bound to a process ID.
References
#file #read #linux #computer_science #write #api #programming #descriptors #unix