In the Linux general-purpose I/O model,
I/O
The Action series function (System call) is expanded around an integer called a file descriptor. This begs the question: what does this integer mean? Does a value represent a file? Just pass an integer in the call, okay?
Original address: Https://learn-linux.readthedocs.io.
Welcome to our public number: Appetizer programming (Coding-fan)
To answer these questions, you need to learn more--file descriptor (descriptor).
Graphic
To understand the situation, you need to understand the 3 data structures maintained by the kernel:
- Process-level file Descriptor Table (descriptor)
- System-level open File Table
- File system I-node tables (i-node table)
The relationship between these 3 data structures is as follows:
File Descriptor Descriptor
The kernel maintains a file descriptor table for each process, each of which records information about a single file descriptor, including:
- Control Flag (flags), the current kernel defines only one, which is the
close-on-exec
- Open File Description Body pointer
Open File Table
The kernel maintains a system-level Open File description table (Open File description table) for all open files, for short, opening files . The entries in the table are referred to as Open file descriptors (description), which stores all the information associated with an open file, including:
- file Offset (file offset), call
read()
and write()
Update, call lseek()
directly modify
- access Mode ,
open()
set by invocation, for example: read-only, write-only, read-write, etc.
i-node
Object pointers
I-node table
Each file system maintains a table for all files stored on it, including the directory i-node
, and i-node
contains the following information individually:
- File Type (file type), which can be regular files, directories, sockets, or
FIFO
- Access rights
- file Lock List (File locks)
- File size
- Wait a minute
i-node
stored on a disk device, the kernel maintains a copy in memory, where the i-node
table is the latter. In addition to the original information, the copy includes the reference count (from the Open file description), the device number, and some temporary properties, such as file locks.
Scene parsing
, it describes in detail the many file descriptors for two processes, as well as their interrelationships.
File descriptor replication
In the process A
, both the file descriptor 1 and the file descriptor 20 point to the same open File Description body (label 23). This is most likely formed by calling a dup()
series function.
File descriptor replication, which is useful in some scenarios, such as: standard input/output redirection. In the shell
next, it is very simple to complete this operation, most people will, but very few people think about the principle behind.
Describe some of the steps required, with the standard output (file descriptor 1) redirection as an example:
- Open the target file and return the file descriptor N;
- Close file descriptor 1;
dup
The call copies the file descriptor n to 1;
- Close file descriptor N;
Child process Inheritance File descriptor
The file Descriptor A
2 of the process and B
the file descriptor 2 of the process refer to the same open File Description Body (label 73). This situation is likely to occur fork()
after a derived child process is called, such as A
fork()
a call derived B
. At this point, B
as a child process, the A
file descriptor list is inherited from the parent process, which includes the file descriptor 2 indicated in the diagram. This is the 子进程继承父进程打开的文件
origin of this sentence.
Of course, the A
process Unix
passes a file descriptor through the socket to a B
similar scenario, but the general file descriptor values are different. At the same time for 2 to happen very coincidentally.
Next
For more articles, visit: Learn Linux
Subscribe to the update, get more learning materials, please follow our public number?:
Linux file descriptor