Linux - what is a file descriptor

Table of contents

Preamble

First, why are there file descriptors

Second, what is a file descriptor

2.1 File operation interface

 2.2 File descriptors

 Three, the principle of the file descriptor

Fourth, the allocation rules of file descriptors


Preamble

This article mainly explains the file descriptor in detail. We start from 1. Why? 2. What is it? 3. The implementation principle is explained in three aspects.

First, why are there file descriptors

In the learning of language and system, the learning of IO flow is inseparable. In it, we will learn operations such as opening, closing, reading, and writing of files. In this case, when a file is opened by the system, we must How to manage these open files? At this time, file descriptors are needed to manage these opened files.

Second, what is a file descriptor

2.1 File operation interface

First of all, there are a series of file operation functions in the C language library, such as fopen (open), fclose (close), fputs (write), fgets (read) , as we all know, the library at the language level is provided by encapsulating the interface provided by the operating system To complete the operation of the underlying hardware , and the file operation function of the C language is no exception. The corresponding underlying interfaces are open (open), close (close), write (write), read (read)

 

 2.2 File descriptors

 One of the careful classmates has already put it down. In the above system interface, I marked the variable fd with a red line. In fact, this is the file descriptor. After we call open to open a file or create a new file, the system will return a file descriptor. Reading and writing files also requires the use of file descriptors to specify the file to be read and written.

A file descriptor is formally a non-negative integer. In fact, it is an array subscript pointing to the file descriptor table of the file opened by the process maintained by the system for each process.

Next, let's write a simple code to observe the file descriptor:

Here we found that we only opened one file, but the file operator is 3 instead of 0, 1, 2, why is this? First of all, in Linux, we need to understand the first essence of everything is a file. Input from the keyboard and output from the monitor actually open the corresponding device file for input and output, namely stdin, stdout, and error output stderr. These three are called For standard input, standard output, and standard error, the three default file operators are 0, 1, and 2, respectively.

 Three, the principle of the file descriptor

 First, the user creates a process. We create a process structure task_struct to represent the attributes of the process. There is a File pointer pointing to files_struct, which stores the file descriptor table arr[]. When we open a file, a file_struct will be created in memory. To store the attributes of each open file, and then these file_structs are mapped to the file descriptor table arr[] according to the rules of the file descriptor, so when we want to modify or read the log.txt file, it is through the log. The file descriptor of txt finds its corresponding file_struct structure pointer in the file descriptor table arr[], and then accesses the file.

Fourth, the allocation rules of file descriptors

Rules: Find an unused subscript in file_struct from small to large, as a new file descriptor.

We can write a piece of code to prove it.

 Observing the above figure, we found that when we close 0, which is the stdin file, the file descriptor of the newly opened file will start from subscript 0

Guess you like

Origin blog.csdn.net/zcxmjw/article/details/130937599