Linux file descriptor

Source: Internet
Author: User

In the Linux general-purpose I/O model, I/OThe Action series function (System call) is expanded around an integer called a file descriptor. This begs the question: what does this integer mean? Does a value represent a file? Just pass an integer in the call, okay?

Original address: Https://learn-linux.readthedocs.io.
Welcome to our public number: Appetizer programming (Coding-fan)

To answer these questions, you need to learn more--file descriptor (descriptor).

Graphic

To understand the situation, you need to understand the 3 data structures maintained by the kernel:

    • Process-level file Descriptor Table (descriptor)
    • System-level open File Table
    • File system I-node tables (i-node table)

The relationship between these 3 data structures is as follows:

File Descriptor Descriptor

The kernel maintains a file descriptor table for each process, each of which records information about a single file descriptor, including:

    • Control Flag (flags), the current kernel defines only one, which is theclose-on-exec
    • Open File Description Body pointer
Open File Table

The kernel maintains a system-level Open File description table (Open File description table) for all open files, for short, opening files . The entries in the table are referred to as Open file descriptors (description), which stores all the information associated with an open file, including:

    • file Offset (file offset), call read() and write() Update, call lseek() directly modify
    • access Mode , open() set by invocation, for example: read-only, write-only, read-write, etc.
    • i-nodeObject pointers
I-node table

Each file system maintains a table for all files stored on it, including the directory i-node , and i-node contains the following information individually:

    • File Type (file type), which can be regular files, directories, sockets, orFIFO
    • Access rights
    • file Lock List (File locks)
    • File size
    • Wait a minute

i-nodestored on a disk device, the kernel maintains a copy in memory, where the i-node table is the latter. In addition to the original information, the copy includes the reference count (from the Open file description), the device number, and some temporary properties, such as file locks.

Scene parsing

, it describes in detail the many file descriptors for two processes, as well as their interrelationships.

File descriptor replication

In the process A , both the file descriptor 1 and the file descriptor 20 point to the same open File Description body (label 23). This is most likely formed by calling a dup() series function.

File descriptor replication, which is useful in some scenarios, such as: standard input/output redirection. In the shell next, it is very simple to complete this operation, most people will, but very few people think about the principle behind.

Describe some of the steps required, with the standard output (file descriptor 1) redirection as an example:

    1. Open the target file and return the file descriptor N;
    2. Close file descriptor 1;
    3. dupThe call copies the file descriptor n to 1;
    4. Close file descriptor N;
Child process Inheritance File descriptor

The file Descriptor A 2 of the process and B the file descriptor 2 of the process refer to the same open File Description Body (label 73). This situation is likely to occur fork() after a derived child process is called, such as A fork() a call derived B . At this point, B as a child process, the A file descriptor list is inherited from the parent process, which includes the file descriptor 2 indicated in the diagram. This is the 子进程继承父进程打开的文件 origin of this sentence.

Of course, the A process Unix passes a file descriptor through the socket to a B similar scenario, but the general file descriptor values are different. At the same time for 2 to happen very coincidentally.

Next

For more articles, visit: Learn Linux

Subscribe to the update, get more learning materials, please follow our public number?:

Linux file descriptor

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.