The relationship between file descriptors and open files in Linux
Guide |
The kernel (kernel) accesses files using file descriptors (descriptor). The file descriptor is a non-negative integer. When you open an existing file or create a new file, the kernel returns a file descriptor. Read-write files also need to use file descriptors to specify which files to read and write. |
1. Overview
In the Linux system, everything can be considered as a file, and the file can be divided into: Ordinary files, directory files, linked files and device files. File descriptor is the index created by the kernel in order to efficiently manage files that have been opened, which is a nonnegative integer (usually a small integer) used to refer to the file being opened and all system calls that perform I/O operations through the file descriptor. When the program just started, 0 is the standard input, 1 is the standard output, and 2 is the standard error. If you open a new file at this point, it will have a file descriptor of 3. the POSIX standard requires that each time the file is opened (with a socket) to use the current process the smallest available file descriptor number, so a bit of attention during network communication can cause a string of words . The standard file description metalized is as follows:
The file description corresponds to the open file model such as:
2. File Description Restrictions
In the writing of file operations or network communication software, beginners may generally encounter the "Too many open files" problem. This is mainly because the file descriptor is an important resource of the system, although the number of system memory can open how many file descriptors , but in the actual implementation process of the kernel will do the corresponding processing, the general maximum open files will be 10% of the system memory (in kilobytes) (referred to as system-level throttling), viewing the maximum number of open files at the system level you can use Sysctl-a | grep fs.file-max command to view. At the same time, the kernel, in order not to let a process consume all the file resources, it will be the maximum number of open files per process default processing (called User-level restrictions), the default value is generally 1024, using the Ulimit-n command can be viewed. In the Web server, optimizing the server by changing the maximum value of the system default file descriptor is one of the most common ways to optimize the way you see http://blog.csdn.net/kumu_linux/article/details/7877770.
3. File description conforms to the relationship between open files
Each file descriptor corresponds to an open file, and the different file descriptors point to the same file. The same files can be opened by different processes and can be opened multiple times in the same process. The system maintains a file descriptor table for each process, the value of which is 0, so in different processes you will see the same file descriptor, in which case the same file descriptor may point to the same file, or it may point to a different file. The specific situation to be specific analysis, to understand how the specific situation, you need to see the kernel maintained by the 3 data structures.
1. Process-level file descriptor
2. System-Level Open file descriptor
3. I-node table for file system
Each entry for the process-level descriptor Descriptor records information about a single file descriptor.
1. A set of flags that control file descriptor operations. (Currently, this type of flag only defines one, that is, the CLOSE-ON-EXEC flag)
2. References to open file handles
The kernel maintains a system-level descriptor table for file maintenance on all open files (open File description table). Sometimes it is also called an open File table, and the entries in the table are referred to as open file handles (handle). An open file handle stores all the information associated with an open file, as follows:
1. Current file offset (call read () and write () update, or use Lseek () to modify directly)
2. The status ID used when opening the file (that is, the flags parameter of Open ())
3. File access mode (e.g., read-only mode, write-only mode, or read-write mode when calling open ()
4. Signal-Driven related settings
5. References to the I-node object of the file
6. File type (for example: regular file, Socket, or FIFO) and access rights
7. A pointer to the list of locks held by the file
8. Various properties of the file, including file size and timestamps associated with different types of operations
Shows the relationship between file descriptors, open file handles, and I-node, in which two processes have many open file descriptors.
In process A, both file descriptors 1 and 30 point to the same open file handle (label 23). This may be formed by invoking the DUP (), dup2 (), Fcntl (), or by invoking the open () function multiple times on the same file.
The file descriptor 2 of process A and the file descriptor 2 of Process B all point to the same open file handle (label 73). This situation may occur after you call fork () (that is, process A, B is a parent-child process relationship), or when a process passes an open file descriptor to another process through a UNIX domain socket. And then there are different processes calling the open function on its own, the same file is opened, and the inside descriptor of the process is assigned exactly to the same descriptor as the other process opening the file.
In addition, the descriptor 0 of process A and the descriptor 3 of process B point to different open file handles, but these handles point to the same entry for the I-node Table (1976), in other words, to the same file. This happens because each process initiates an open () call to the same file. A similar situation can occur when the same file is opened two times with the same process.
4. Summary
1. Because of the presence of a process-level file descriptor, the same file descriptors appear in different processes, either pointing to the same file or pointing to a different file
2. Two different file descriptors, if you point to the same open file handle, the same file offset will be shared. Therefore, if you modify the file offset by one of the file descriptors (by calling read (), write (), or Lseek (), the change is observed from another descriptor, regardless of whether the two file descriptors belong to a different process or the same process.
3. To get and modify open file flags (for example: O_append, O_nonblock, and O_async), you can perform F_GETFL and F_SETFL operations of Fcntl (), which are similar to the previous one for scope constraints.
4. The file descriptor flags (that is, close-on-exec) are private to the process and file descriptors. Modifications to this flag will not affect the same process or other file descriptors in different processes
Free to provide the latest Linux technology tutorials Books, for open-source technology enthusiasts to do more and better: http://www.linuxprobe.com/
The relationship between file descriptors and open files in Linux