Introduction to the file descriptor under Linux

Source: Internet
Author: User
Tags inheritance

In the C program, the file is represented by a file pointer or a file descriptor. ISO C's Standard i/0 library functions (fopen, Fclose, Fread, Fwrite, fscanf, fprintf, etc.) use file pointers, Unix I/O functions (open, close, read, write, IOCTL) use file descriptors. The following is the focus of how the file descriptor works.

A file descriptor is equivalent to a logical handle, while functions such as Open,close associate a file or a physical device with a handle. The handle is an integer that can be understood as the index of the process-specific file descriptor. First, we introduce the following three concepts, after the open, close, and other operations, the file and file descriptor what the relationship, as well as fork file descriptor inheritance and so on.

File descriptor: A part of the user area that the program cannot access unless the function of the file descriptor is used. For each open file in the process, the file descriptor contains an entry.

System File Table: Shared for all processes in the system. For each active open, it contains an entry. Entries for each System file table include a file offset, access mode (read, write, or read-write), and an entry count for the file descriptor that points to it.

Memory Index node table: For each active file in the system (opened by a process), the in-Memory index node table contains an entry. Several system file table entries may correspond to the same Memory Index node table (different processes open the same file).

1, example: Execution MYFD = open ("/home/lucy/my.dat", o_rdonly); Later, the diagram of the above 3 tables is as follows:

The System File table contains an offset that gives the current location of the file. If 2 processes open at the same time a file (such as a,b above) do read operations, each process has its own offset from the file, and the entire file is read independently of another process, and if 2 processes open the same file for writing, the write operation is independent of each other, and each process can rewrite the content written by another process.

If the above process executes the close () function after open, the operating system deletes the fourth entry in the File descriptor table and the corresponding entry for the system file tables (if it is unique to its descriptor), minus 1 of the count in the Memory Index node table entry, if it becomes 0 after the reduction, Indicates that no other process is linking this file, the Index node table entry is also deleted, and the process B is open this file, so the Index node table entry is retained.

2, the file descriptor inheritance

When a child process is created by fork (), the child processes inherit a copy of the parent process environment and most of the context's contents, including the File descriptor table.

(1) For files opened before the parent process is fork (), the child process inherits and shares the same file offset as the parent process. As shown in the following figure (0-1-2 represents the standard input-output-error):

The System File table is in the system space and will not be replicated by fork (), but the entries in the System File table will hold the count of the file descriptor that points to it, and the count should be maintained when fork () to reflect the new file descriptor corresponding to the child process. When the program closes the file, it also removes the count from the System File table entries and deletes it when the count value is reduced to 0 o'clock.

(2) Conversely, if the parent process first processes fork and then opens My.dat, then the parent-child process's file descriptor for My.dat points to different system file table entries and no longer shares the file offset (Fork 2 processes are open and 2 entries are created in the System File table) But for standard input, standard output, standard error, parent-child process is shared.

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.