Linux File Sharing - Process and Open File

  

Linux supports sharing open files between different processes. To illustrate file sharing, let's first explain the data structure that the kernel uses for all I/O. The relationship between them determines the impact that one process may have on another in terms of file sharing.

0.1 and file related objects
(1) inode (i node): store general information of a file, each inode has an inode number, in the file system, an inode number can be uniquely Identify a file.
(2) dentry (directory item object): Each dentry represents a specific part of the path. Save link information for a directory. Personal understanding: Describe the correspondence between a file and a name.
(3) file: A relationship between a file that is opened and a process. The file object represents a file that has been opened by the process. In memory, the object is not a physical file. It is created by the corresponding open() system call and destroyed by the close() system call (only destroyed if its reference count is 0). Multiple processes can open and manipulate the same file, so there may be multiple corresponding file objects in the same file. It is the information in the apue "directory table entry", the connotation file offset and so on.

0.2 and process-related objects
(1) files_struct: Each process uses a files_struct structure to record the use of file descriptors, this structure is called the user open file table (with descriptor table) . It is the private data of the process. It is the "open file descriptor table" in apue.
(2) fs_struct: The fs_struct structure describes the relationship between a process and a file system.
The relationship of these objects is shown below.


0.3 Apue file object interpretation
Let's take a look at the relationship between files and processes described in apue. The following figure (Figure 3-1) shows the three tables of the process. Relationship between. The process has two different open files — one file opens as standard input (file descriptor 0) and the other opens as standard output (file descriptor 1).


(1) Each process has a record item in the process table (open file descriptor table), the record item contains a descriptor table of open files, which can be It is treated as a vector, and each descriptor occupies one item.

Each file descriptor is associated with:
· file descriptor flag (close_on_exec).
· Pointer to a file table entry.

Note: The files (files_struct type) in the Linux process descriptor (tast_struct) are described here.

(2) The kernel maintains a file table for all open files. Each file entry contains:
· file status flags (including read/write, fill, sync, non-blocking, etc.)
· current file offset
· points to the file v-node entry Pointer.

Note: The "file entry" described here is the file object under the linux system.

(3) Each open file has a v-node structure. The v-node contains pointers to file types and functions that perform various operations on this file. For most files, the v-node also includes the i-node of the file. This information is read into disk from disk when the file is opened again. These files are read from disk into memory, so these parameters can be used quickly.

Note: The v node described here is the i node in linux, corresponding to the inode object.

After our discussion, we mainly focused on these three objects.

0.4 Reference Counting
There are reference counts in several of the file-related objects we discuss, and their meanings are different. Understanding these reference counts is critical to our discussion below.
(1) file_struct reference count: indicates the number of processes (threads) sharing this structure. (We will discuss thread sharing files.)
(2) fs_struct reference count: indicates the number of processes (threads) sharing this structure. (We will discuss thread sharing files)
(3) file object reference count: Open the file reference count, refer to the number of file object descriptors. (dup and fork will increase this count, the first open will make this count 1, close will reduce this count, 0 will destroy the file object)
(4) dentry reference count: each count corresponds to a file Object.
(5) inode reference count: each count corresponds to a dentry.
Extensions:
(1) close_on_exec flag
As can be seen from the figure, this is marked in the files_struct of the process, is a bitmap flag of all file descriptors (file handles) of a process, each bit Corresponding to an open file descriptor, used to determine the file descriptor that needs to be closed when calling the system call execve(), which can be set with fcntl.
(2) Appendix: i-node structure
struct dinode{ ushort di_mode; /* file type + user authority * /short di_nlink; /* file link number * /ushort di_uid; /* owner user id * /Ushort di_gid; /* belongs to the primary user group id*/off_t di_size; /*file size*/char di_addr[40]; /*file data area starting address*/time_t di_atime; /*last access time*/time_t di_mtime; /* Last modified time */time_t di_ctime; /*Create time*/};

Copyright © Windows knowledge All Rights Reserved