File descriptor issues between the C + + parent and child processes

Source: Internet
Author: User

In a C program, a 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 descriptors, Unix I/O functions (open, close, read, write, IOCTL). The following emphasis is on how the file descriptor works.

A file descriptor is equivalent to a logical handle, while a function such as Open,close associates a file or physical device with a handle. A handle is an integer that can be understood as the index of a process-specific file descriptor. This article introduces the following three concepts, followed by the open, close, and so on after the operation of the file and file descriptor, as well as the fork after the file descriptor inheritance and so on.

file Descriptor : Part of the user area, unless the program cannot access it by using a function of the file descriptor. For each open file in the process, the file descriptor list 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 contain the file offset, the access mode (read, write, or read-write), and the count of entries to the file descriptor for it.
Memory Index Node table: The In-Memory index node table contains an entry for each active file in the system (opened by a process). Several system file table entries may correspond to the same table of memory index nodes (different processes open the same file).
1, example: Execution MYFD = open ("/home/lucy/my.dat", o_rdonly); Later, the schematic 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 a file (such as a, b) to do the read operation, each process has its own relative to the file offset, and read into the entire file is independent of the other process; If 2 processes open the same file for write operations, the writes are independent of each other, and each process can rewrite what the other process writes.
If the above process executes the close () function after open, the operating system will delete the fourth entry of the File descriptor table and the corresponding entry for the system file tables (if it is unique to its descriptor table), and subtract 1 from the count in the Memory index node tables entry, if the decrement becomes 0. Description No other processes link This file, the Index node table entry is also deleted, and here Process B is also in the open file, so the Index node table entry is retained.

2. Inheritance of file descriptors when you create a child process by fork (), the child process inherits a copy of the parent process environment and most of the context, including the file descriptor table.

(1) For a file that the parent process opens before fork (), the child process inherits and shares the same file offset with the parent process. As shown (0-1-2 indicates standard input-output-error):

The System File table is in system space and will not be copied by fork (), but the entries in the System File table will hold a count of the file descriptor tables that point to it, and this count needs to be maintained when fork () to reflect the new file descriptor table corresponding to the child process. When a program closes a file, it also drops the count inside the System File table entry by one and deletes it when the count value is reduced to 0 o'clock.
(2) Conversely, if the parent process first fork, and then open My.dat, then the parent-child process about the My.dat file descriptor table points to different system file tables entries, also no longer share the file offset (fork after 2 processes open, create 2 entries in the System File table) But about standard input, standard output, standard error, parent-child process or 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.