UNIX systems support sharing open files between different processes.
The kernel uses three data structures to indicate opened files. The relationship between them determines the possible impact of one process on another process in terms of file sharing.
(1) Each process has a record item in the process table. The record item contains a file descriptor table that can be viewed as a vector, and each descriptor occupies one item. Associated with each file descriptor is:
(A) file descriptor flag (close_on_exec ). Each open descriptor in a process has a close_on_exec flag. If this flag is set, it is disabled when exec is executed; otherwise, the descriptor is still open. Unless this flag is specifically set with fcntl, the system will keep this descriptor open after exec is executed by default.
(B) pointer to a file table item.
(2) The kernel maintains a file table for all open files. Each file table item includes:
(A) File status signs (reading, writing, adding, synchronization, and non-blocking ).
(B) offset of the current file.
(C) pointer to the table entry of the file v node.
(3) Each open file (or device) has a v-node structure. The v node contains the file type and pointer to the functions that perform various operations on the file. For most files, the v node also contains the I node (index node) of the file ). This information is read into the memory from the disk when the file is opened, so all information about the file is quick to use. For example, the I node contains the file owner, the file length, the device where the file is located, and the pointer to the actual data block of the file on the disk.
Note: Linux does not use the v node, but uses the General I node structure. Although the two implementations are different, in terms of concept, the v node is the same as the I node. Both point to the unique I node Structure of the file system.
Figure 3-1 shows the relationship between three tables of a process. This process has two different open files: one file is opened as the standard input (file descriptor 0), and the other open file is the standard output (file descriptor 1 ). Since the early versions of UNIX systems, the basic relationships between the three tables have been maintained to date. This arrangement is very important for the way files are shared between different processes.
The purpose of creating a v node structure is to support multiple file system types on a computer system. This work was done independently by Peter Weinberger (Bell Labs) and Bill Joy (Sun. Sun calls this File System a Virtual File System, And I nodes unrelated to the File System type are v nodes.
Linux does not divide the relevant data structures into I and v nodes, but uses an I node independent of the file system and an I node dependent on the file system.
Note: The difference between the file descriptor mark and the file state mark in terms of scope. The former is only used for one descriptor of a process, the latter applies to all descriptors in any process pointing to the given file table item.
For a more straightforward explanation of the differences between the file descriptor flag and the file state flag, see: http://blog.csdn.net/bayji/article/details/6020459