File Descriptor
Basic Concepts
A file descriptor (FD) is an integer identifier used to represent an open file or other input/output resources in an operating system. It serves as a bridge between the operating system and applications for file operations.
In Linux, a file descriptor is a non-negative integer, and the operating system assigns a file descriptor to each open file or input/output (I/O) resource (such as pipes, network sockets, etc.). Whenever a process requests to open a file or resource, the operating system returns a file descriptor, and the process can then access that file or resource through this descriptor.
Common File Descriptors
All processes automatically open the following 3 file descriptors.
Descriptor | Name | Description |
---|---|---|
0 | STDIN | Standard Input |
1 | STDOUT | Standard Output |
2 | STDERR | Standard Error |
For example, command redirection directly uses the above file descriptors.
The above >
and 1>
are the same.
File Descriptors in Code
The operating system manages file operations through file descriptors. Common file operations (such as reading, writing, closing) usually require file descriptors. Below are some common operations and their use of file descriptors:
Opening a File
The open()
system call returns a file descriptor.
Reading a File
Read content through the file descriptor.
Writing to a File
Write data to a file through the file descriptor.
Closing a File
Close the file descriptor to release system resources.
File Descriptors in Process
Each process has a corresponding /proc/PID/fd/
directory, which contains symbolic links to all file descriptors opened by that process.
Summary
File descriptors provide an abstraction for processes.
Programs can use a unified interface for various I/O operations without worrying about the underlying physical device details.
For example, reading a file, receiving data from a network socket, and reading from a pipe,
can all be implemented using the same read()
function, which is all managed uniformly through file descriptors.