Make a little progress every day the relationship between the file description descriptor and the open file in--linux

Source: Internet
Author: User
Tags unix domain socket

Reprint please indicate source: http://blog.csdn.net/cywosp/article/details/38965239


1. OverviewIn the Linux system, everything can be considered as a file, and the file can be divided into: Ordinary files, folder files, linked files and device files. The file Description Descriptor (descriptor) is the index created by the kernel to efficiently manage files that have been opened. It is a non-negative integer (typically a small integer) that is used to refer to the file being opened. All system calls that run I/O operations are described by a file descriptor. When the program just started. 0 is the standard input, 1 is the standard output, and 2 is the standard error.

Suppose to open a new file at this time, its file descriptor will be 3.

the POSIX standard requires that each time the file is opened (with a socket) to use the current process The smallest available file describes the descriptor number, so a little attention during network communication can cause a string of words . The standard document descriptive narration metalized For example the following:


The file description describes the corresponding model with open files for example with:

2. Description Limitation of documentationWhen writing software for file manipulation or network communication, people who have just started learning may encounter the problem of "Too many open files". This is mainly due to the fact that the descriptive descriptor of a file is an important resource of the system. Although it says how much of the system memory is able to open how many file descriptive descriptors . However, in the actual implementation process, the kernel will do the corresponding processing, the general maximum number of open files will be 10% of the system memory (in kilobytes) (known as system-level restrictions). Viewing the maximum number of open files at the system level can use Sysctl-a | grep fs.file-max command to view.

At the same time, the kernel does not allow a process to consume all of its file resources. It also defaults to the maximum number of open files for a single process (called a user-level limit), which is typically 1024, and can be viewed using the ulimit-n command. In webserver, it is one of the most common ways to optimize server by changing the maximum value of the system default file description descriptor. See http://blog.csdn.net/kumu_linux/article/details/7877770 for detailed optimization methods.


3. Document description In accordance with the relationship between open filesEach file description descriptor corresponds to an open file. At the same time. Different file description Descriptors also 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 Description descriptor table for each process, and the value of the table starts at 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.

Detailed analysis of the details. To understand the details of its profile, you need to look at the 3 data structures maintained by the kernel.

1. Process-level File Description Descriptor Table2. System-Level Open file Descriptive descriptor Table3. I-node table for file system
Each entry in the descriptive descriptor of the process level records information about a single file descriptive descriptor. 1. A set of flags that control the operation of a file description descriptor.

(For now, this type of flag only defines one, i.e. the CLOSE-ON-EXEC flag)

2. References to open file handles
The kernel maintains a system-level descriptive Descriptor table (open File description table) for file maintenance on all open files. 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, such as the following:

1. Current file offset (call read () and write () update, or use Lseek () to change directly)2. The status identifier used when opening the file (that is, the flags for Open ())3. File access mode (e.g., read-only mode, write-only mode, or read-write mode when calling open ()4. Signal-driven related settings5. References to the I-node object of the file6. File type (e.g. general file, socket or FIFO) and access Permissions7. A pointer to the list of locks held by the file8. Various properties of the file, including file size and timestamps associated with different types of operations
shows the relationship between the file description descriptor, the open file handle, and the I-node. Diagram. Two processes have many open file descriptive descriptors.

in Process A. The file description descriptors 1 and 30 both 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. process A's file descriptor 2 and Process B's file Description Descriptor 2 All point to the same open file handle (label 73). This can happen after you call fork () (that is, process A, B is a parent-child process relationship). Or it can happen when a process passes an open file description descriptor through a UNIX domain socket to another process.

And then there are different processes that call the Open function on its own, which opens the same file, where the descriptive descriptor inside the process is allocated just like the descriptive descriptor that opened the file to other processes.

In addition, process A's descriptive descriptor 0 and process B's descriptive narrator 3 points to different open file handles, respectively. These handles, however, point to the same entry for the I-node Table (1976), in other words, to the same file. This occurs because each process initiates an open () call to the same file. The same process opens the same file two times. A similar situation can occur.


4. Summary 1. Because the process-level file describes the existence of a descriptive descriptor, the same file-descriptive narrative appears in different processes. They may point to the same file, or they may point to a different file 2. Two different file description descriptors, if you point to the same open file handle, the same file offset will be shared. So, assuming that the file offset is altered by one of the file descriptors (called read (), write (), or Lseek (), the change will also be observed from a descriptive descriptor, regardless of whether the two file descriptors belong to a different process or the same process. This is the case.

3. To get and change the open file flags (for example: O_append, O_nonblock, and O_async), you can run the F_GETFL and F_SETFL operations of Fcntl (), which have a similar constraint on the scope as the previous one. 4. Document descriptive descriptor flag (ie. CLOSE-ON-EXEC) is private for process and file descriptive descriptors. Changes to this flag will not affect the same process or other file description descriptors in different processes





References[1] http://blog.chinaunix.net/uid-20633888-id-2747146.html[2] http://www.cppblog.com/guojingjia2006/archive/2012/11/21/195450.html[3] http://blog.csdn.net/kumu_linux/article/details/7877770[4] "Linux/unix system Programming Manual"



Make a little progress every day the relationship between the file description descriptor and the open file in--linux

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.