This article focuses on the Linux operating system process (thread) and file descriptor, file relationship, specific to the kernel part is task_struct, files_struct, file and inode relationship.
As we know from Linux user-mode development, opening a file returns a file descriptor, and each process has a limited number of open files. What exactly is the cause of this?
If we delve into the kernel of the Linux operating system, we can see why. In Linux, each user-mode process has a corresponding kernel process (thread) in the kernel state, which is identified by the task_struct structure, which is used by the kernel to schedule the process. In the kernel, file access is achieved through file and inode structures, which contain key information (such as access offsets) and methods (such as reading and writing file operations) to access the file.
Fd = open (“/home/zhf/zhf/c_prj/itworld123.com “O_RDWR);
Relationship between process and file
The following is a typical process/file diagram, in which the process has two different files open. There is a files_struct member in the task_struct that holds the array, whose offset is the file descriptor, and whose members are Pointers to the file structure. In this way, through the user mode of integer file descriptor can be very convenient to find the structure of the management file (file) to achieve the operation of the file. In this article, the files_struct structure is simplified for the sake of illustration, and in fact the structure feed is much more complex.
Struct (task_struct) and inode (inode); As shown in the figure above, each file has a structure corresponding to an inode. Both actually correspond to a specific file on a disk, but there are differences. File actually corresponds to an instance of an open file, while inode corresponds to a disk file. This means that a disk file may have multiple files in memory, whereas an inode may have only one file. We’ll explain the implementation in more detail later.
The relationship between parent and child processes and files
We know that in Linux processes have some sort of parent-child relationship, and that the child inherits a lot from the parent. If we fork a child, the child will inherit the parent’s files. If we use the fork system call, the child creates a new files_struct instance and migrates the contents of the parent process. Migration, not copying, is not the original memory copy, but some processing. For example, if the parent process points to the file structure, the child process also points to the file structure, and the file descriptor is consistent. In addition, the reference count of the file structure instance is increased to ensure the correctness of the usage relationship.
For now, this article will continue to discuss the relationship of the file structure to inode and disk data.