UNIX io-talk about file descriptors

Source: Internet
Author: User
In a C program, a file is represented by a file pointer or file descriptor. Iso c 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 the file descriptor. Next we will focus on how file descriptors work. The file descriptor is equivalent to a logical handle, while functions such as open and close associate files or physical devices with the handle. A handle is an integer that can be understood as the index of a process-specific file descriptor table. First, we will introduce the following three concepts: What is the relationship between files and file descriptors after Open and Close operations, and the inheritance of file descriptors after fork. File descriptor table: A part of the user zone. Unless the file descriptor function is used, the program cannot access it. For each opened file in the process, the file descriptor table contains an entry. System File Table: Share all processes in the system. For each activity, open contains an entry. The entries in each system file table include the file offset, access mode (read, write, or read-write), and count the entries in the file descriptor table pointing to it. Memory index node table:For each active file in the system (opened by a process), the index node table in the memory 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: Execute myfd = open ("/home/Lucy/My. dat ", o_rdonly); later, the schematic diagram of the relationship between the three tables is as follows: Figure 1. The system file table contains an offset, showing the current position of the file. If two processes open a file (such as a and B) at the same time for read operations, each process has its own offset relative to the file, and reading the entire file is independent of the other process; if two processes open the same file for write operations, the write operations are independent of each other, and each process can rewrite the content written by another process. If the preceding process executes the close () function after open, the operating system deletes the fourth entry in the file descriptor table, the corresponding entry to the System File Table (if it is unique to its Descriptor Table), and the count in the table entry of the memory index node is reduced by 1. If it is changed to 0 after the auto-subtraction, no other process links to this file, and the index node table entries are also deleted. process B is also open in this file, so the index node table entries are retained. 2. When a child process is created through fork (), the child process inherits the copy of most of the content of the parent process environment and context, including the file descriptor table. (1) For files opened by the parent process before fork (), the child process will inherit and share the same file offset with the parent process. As shown in (0-1-2 indicates standard input-output-error): Open My before Figure 2 fork. the DAT system file table is located in the system space and won't be copied by fork (), but the entries in the system file table will save the count of the file descriptor table pointing to it, fork () you need to maintain the count to indicate that the new file descriptor table corresponding to the sub-process also points to it. When the program closes a file, it also removes the count in the system file table entry by one. When the Count value is reduced to 0, it is deleted. (2) On the contrary, if the parent process first processes fork and then opens my. dat, then the Parent and Child processes about my. the DAT file descriptor table points to different system file table entries and does not share the file offset (fork will open two processes separately and create two entries in the system file table ); however, for standard input, standard output, and standard errors, the Parent and Child processes are still shared. Figure 3 fork () after opening my. dat to: http://keren.blog.51cto.com/720558/170822

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.