No picture, no truth, first:
Each process stores a file descriptor table in the PCB. The file descriptor is the index of this table. Each table item has a pointer to an opened file, an opened file is represented by a file struct in the kernel,
The pointer in the file descriptor table points to the file struct. Maintain the File status flag in the file struct (f_flags, a member of the file struct) and the current read/write location (Body Member f_pos ).
Process 1 and process 2 open the same file, but they correspond to different file structures.This can have different file status flag and read/write locations. Important members in the file struct
F_count, indicatingReference count, dup, fork, and other system calls may cause multiple file descriptions
Point to the same file struct. For example, if both fd1 and fd2 reference the same file struct, then its reference count is
Yes 2. When close (fd1) is used, the file structure is not released, but the reference count is reduced to 1. If close (fd2) is used ),
The reference count will be reduced to 0 and the file structure will be released. This will actually close the file.
Each file structure points to a file_operations struct. All the members of this struct are function pointers pointing
Kernel functions for various file operations. For example, if you read a file descriptor in a user program, read enters
Kernel, find the file struct pointed to by this file descriptor, and find
The file_operations struct that calls the kernel function pointed to by its read members to complete user requests. In the user program
Lseek, read, write, ioctl, open, and other functions are called by the kernel.
The pointing kernel function completes the user request. The release Member in the file_operations struct is used to complete the user program.
The reason why the close request is called release rather than close is that it does not necessarily close the file, but reduces the reference
To close the file only when the reference count is reduced to 0. For general files opened on the same file system
The steps and methods for file operations such as read and write should be the same, and the called functions should be the same.
To point to the same file_operations struct. If you open a character device file
The read and write operations are certainly different from those of conventional files. They are not read/write data blocks on disks, but read/write hardware settings.
Therefore, the file struct should point to different file_operations structs.
Slave driver implementation.
Each file structure has a pointer to the dentry struct. "dentry" is the contraction of directory entry
Write. The parameters passed to functions such as open and stat are a path, for example,/home/akaedu/.
File inode. To reduce the number of disk reads, the kernel caches the tree structure of the Directory, which is called the dentry cache.
A node is a dentry struct. You only need to search for the dentry of each part of the path and find the home directory from the root directory,
Find the akaedu directory and find file. Dentry cache only saves the recently accessed directory items.
If the directory item is not in the cache, it must be read from the disk to the memory.
Each dentry structure has a pointer pointing to the inode structure. Inode struct stores the messages read from the disk inode.
. In the example, there are two dentry, indicating/home/akaedu/a and/home/akaedu/B respectively.
The same inode indicates that the two files are hard links. Inode structure stores a letter from the inode of the disk partition.
Information, such as the owner, file size, file type, and permission bit. Each inode structure has
Returns a pointer to the inode_operations struct. The latter is also a set of function pointers pointing to kernel functions that have completed file directory operations.
Number. Unlike file_operations, inode_operations does not point to a letter that operates on a file.
Number, but a function that affects the layout of files and directories, such as adding and deleting files and directories, and tracking symbolic links.
Inode struct of a file system can point to the same inode_operations struct.
The inode struct has a pointer to the super_block struct. Super_block struct stores the ultra-
Level block read information, such as the file system type and block size. The s_root Member of the super_block struct refers to
The pointer to dentry indicates where the root directory of the file system is mounted. In the example, this partition
Mounted to the/home directory.
The structures file, dentry, inode, and super_block constitute the core concepts of VFS.
The above content is taken from the C language for going to school on the Linux platform