From Process to Kernel: How Unix File Descriptors Become Your Best Assistant?

In Unix and Unix-like operating systems, an file descriptor is a unique identifier for a process that is specifically designed to handle files or other input/output resources, such as pipes or network sockets. These file descriptors are usually non-negative integer values, with negative values ​​used to indicate "no value" or error conditions. This technology has become part of the POSIX API. For every Unix process, except for some daemons, it should have three standard POSIX file descriptors. These three file descriptors correspond to standard input, standard Output and standard error streams.

In traditional Unix implementations, archive descriptors index into a per-process archive descriptor table maintained by the kernel, which in turn indexes into a system-wide table of archives opened by all processes, called the archive table .

The file table records the opening mode of files or other resources, including read, write, append and other modes. Additionally, it indexes to a third table called the inode table, which describes the actual underlying archive. To perform input or output, the process passes a file descriptor to the kernel through a system call, and the kernel accesses the file on the process' behalf. It should be noted that processes cannot directly access files or inode tables. In Linux, the file descriptor opened by a process can be accessed under the path /proc/PID/fd/, where PID is the process identification code.

/proc/PID/fd/0 is standard input, /proc/PID/fd/1 is standard output, and /proc/PID/fd/2 is standard error output.

In the displayed directory, any running process can also access its own file descriptor through the /proc/self/fd and /dev/fd folders. A file descriptor in a Unix-like system can point to any named Unix file type in the file system. This includes not only ordinary files, but also directories, block devices, and character devices (also called special files), Unix domain sockets, and Named pipes.

File descriptors can also point to objects that do not normally exist in the file system, such as anonymous pipes and network sockets. In Unix-like systems, the FILE data structure in the C standard I/O library usually contains a low-level archive descriptor, which provides additional abstraction and is called an archive handle.

File descriptor operations

On modern Unix-like systems, operations on file descriptors can be divided into several categories. Most of these functions are declared in the <unistd.h> header, but some are defined in the <fcntl.h> header.

Create file descriptor

Commonly used functions for creating file descriptors include open(), socket(), pipe(), etc. These functions allow users to create new files or resources for operations based on their needs.

Operation of single file descriptor

Common single file descriptor operations include read(), write(), lseek(), etc. These operations allow users to read or write archive data, or modify the read and write locations of the archive.

Operation of multiple file descriptors

In situations where multiple file descriptors need to be processed, functions such as select() and poll() can be used for monitoring. This is particularly important in network communications and multi-process applications. .

Capability characteristics of file descriptors

Unix file descriptors exhibit capabilities in many ways. They can be passed between processes via Unix domain sockets, using the sendmsg() system call. However, what is actually passed is a reference to an "open file descriptor", which has state (such as file offsets and file status flags), which complicates the safe use of these file descriptors.

The file descriptor table of a Unix process is an example of a capability list (C-list).

Overall, file descriptors are not only an important means for the system to manage resources, but also demonstrate their central role in inter-process collaboration.

Of course, all this inspires an important question: With future technological developments, will the role of archive descriptors evolve as we capture new types of data flows and more complex system architectures?

Trending Knowledge

The mysterious world of Unix systems: How to use epoll and kqueue to manage a large number of file descriptors?
In UNIX and UNIX-like operating systems, a file descriptor (FD) is a process-unique identifier used to identify a file or other input/output resource (such as a pipe or network socket). T
The magic of file descriptors: How to easily manipulate files and resources in Unix systems?
In Unix and Unix-like operating systems, a file descriptor (FD) is a unique identifier used to identify a file or other input/output resource. They make file operations in the system more convenient a
Unix file descriptors revealed: Why are standard input, standard output, and standard error so important?
In Unix and Unix-like operating systems, a file descriptor (FD) is a unique identifier for each process that identifies a file or other input/output resource such as a pipe or network socket. These de

Responses