what is linux fd

青灯夜游
Release: 2023-04-13 09:24:17
Original
9264 people have browsed it

In Linux, the full name of fd is "File descriptor", and the Chinese name is "file descriptor". It is an index created by the kernel in order to efficiently manage these files that have been opened; it is actually a non- A negative integer used to refer to an opened file. All system calls that perform I/O operations are implemented through file descriptors.

what is linux fd

#The operating environment of this tutorial: linux7.3 system, Dell G3 computer.

In Linux, the full name of fd is "File descriptor", and the Chinese name is "file descriptor". A file descriptor is a non-negative integer, essentially an index value (this sentence is very important).

File descriptor (fd) in Linux

We know that everything in the Linux system can be regarded as a file, and files can be divided into: ordinary files, Directory files, link files, and device files. When operating these so-called files, we have to find the name every time we operate, which consumes a lot of time and efficiency. Therefore, Linux stipulates that each file corresponds to an index, so when we want to operate a file, we can directly find the index and operate it.

The file descriptor (file descriptor) is an index created by the kernel in order to efficiently manage these opened files. It is a non-negative integer (usually a small integer) used to refer to the opened file. , all system calls that perform I/O operations are implemented through file descriptors. At the same time, it is also stipulated that when the system is just started, 0 is the standard input, 1 is the standard output, and 2 is the standard error. This means that if you open a new file at this time, its file descriptor will be 3, and if you open another file, the file descriptor will be 4...

The Linux kernel treats all open files There is a file descriptor table, which stores the relationship between each file descriptor as an index and an open file. A simple understanding is an array as shown below. The file descriptor (index) is the bottom of the array of the file descriptor table. The contents of the array are pointers to open files one by one.

what is linux fd

The above is just a simple understanding. In fact, regarding file descriptors, the Linux kernel maintains 3 data structures:

  • Process-level file descriptor table
  • System-level open file descriptor table
  • I-node table of the file system

After a Linux process is started, A PCB control block will be created in the kernel space. There is a file descriptor table inside the PCB, which records all available file descriptors of the current process, that is, all open files of the current process. Each entry in the process-level descriptor table records information about the file descriptor used by a single process. The processes are independent of each other. If one process uses file descriptor 3, another process can also use it. 3. In addition to the process-level file descriptor table, the system also needs to maintain two other tables: the open file table and the i-node table. These two tables store the open file handle of each open file. An open file handle stores all information related to an open file.

System-level open file descriptor table:

  • Current file offset (updated when calling read() and write(), or using lseek () direct modification)
  • Identification when opening the file (flags parameter of open())
  • File access mode (such as read-only mode, write-only mode set when calling open() Or read-write mode)
  • Settings related to signal driver
  • Reference to the i-node object of the file, that is, i-node table pointer

i-node table of the file system:

  • File type (for example: regular file, socket or FIFO) and access permissions
  • A pointer pointing to the file List of locks held
  • Various attributes of the file, including file size and timestamps associated with different types of operations

File descriptors, open file handles And the relationship between i-nodes is as shown below:

what is linux fd

  • In process A, file descriptors 1 and 20 both point to the same open file table entry, labeled 23 (pointing to the array element with index 23 in the open file table). This may be through It is caused by calling dup(), dup2(), fcntl() or calling the open() function multiple times on the same file.
  • File descriptor 2 of process A and file descriptor 2 of process B both point to the same file. This may occur after calling fork() (that is, processes A and B are parent-child process relationships) , or different processes call the open() function alone to open the same file. At this time, the descriptor inside the process is assigned to the same descriptor used by other processes to open the file.
  • Descriptor 0 of process A and descriptor 3 of process B point to different open file table entries respectively, but these table entries all point to the same entry of the i-node table (labeled 1976); in other words, They point to the same file. This happens because each process makes its own open() call to the same file. A similar situation will occur if the same process opens the same file twice.

This means: different file descriptors of the same process can point to the same file; different processes can have the same file descriptor; the same file descriptor of different processes can point to Different files (this is generally the case, except for the three special files 0, 1, and 2); different file descriptors of different processes can also point to the same file.

Example of opening a file on Linux

For example, use vim test.py to open a file on Linux , keep it open, then open a new shell, enter the command pidof vim to get the pid number of the vim process, and then ll /proc/$pid/fd View the pid number used by the vim process list of file descriptors.

/dev/pts is the directory where the console device files created after remote login (telnet, ssh, etc.) are located. Because I logged in remotely through Xshell, the file descriptors of standard input 0, standard output 1, and standard error 2 all point to the virtual terminal console /dev/pts/6. Looking at the file descriptor of the newly opened test.py, it turns out to be 4. Did you agree to start from 3?

I have been troubled by this for a long time. After checking various information, I finally found the reason in a forum with the help of a big boss. Sometimes if you can’t find it in Chinese, you still have to try an English search. Because the principle of an editor like vim is to first open the source file and copy it, then close the source file and then open your own copy. After modifying the file and saving it, directly rename the copy and overwrite the source file. So when opening the source file, use the file descriptor 3, and then open your own copy. It’s time to use the file descriptor 4, and then close the source file, file descriptor3 is released, and when we check it, only 4 is left, where it points to the copy file created by vim. This is just a general idea. To delve deeper into the implementation principle of vim - Oort Nebula Ambassador, here is a screenshot of the information on the forum I saw at that time, the link is here: StackOverFlow.

what is linux fd

If you don’t believe it, you can try another process, such as tail.

Use tail -f test.py Open a file on Linux, keep it open, then open a new shell and enter the command pidof tail Get tail The pid number of the process, and then ll /proc/$pid/fd Check the list of file descriptors used by the tail process. You can see that the file descriptors are indeed used starting from 3 . Tail does not modify the file in the editor, so the source file is opened directly using the file descriptor. In fact, you can use the ll /proc/$pid/fd command to obtain the file descriptor usage of any process currently running.

Extended knowledge: Linux configuration system maximum number of open file descriptors

( 1) System-level restrictions

Theoretically, you can open as many file descriptors as there is system memory, but in practice, the kernel will handle it accordingly. Generally, the maximum number of open files will be 10% of the system memory (calculated in KB), which is called This is a system-level restriction. This number can be viewed through the cat /proc/sys/fs/file-max or sysctl -a | grep fs.file-max command.

There are two ways to change system-level restrictions: temporary changes and permanent changes:

  • Temporary changes: session disconnection or system restart The original settings will be restored later. Use the command sysctl -w fs.file-max=xxxx, where xxxx is the number to be set.

  • Permanent change: vim edit /etc/sysctl.conf file, add fs.file-max=xxxx at the end, where xxxx It is the number to be set. After saving and exiting, you must also use the sysctl -p command to make it effective.

(2) User-level restrictions

At the same time, in order to control the file resources consumed by each process, the kernel will also open the maximum file for a single process The default limit is the user-level limit. The default value for 32-bit systems is generally 1024, and the default value for 64-bit systems is generally 65535. You can use the ulimit -n command to view it.

There are two ways to change user-level restrictions: temporary changes and permanent changes:

  • Temporary changes: session disconnection or system restart The original settings will be restored later. Use the command ulimit -SHn xxxx to modify it, where xxxx is the number to be set.

  • Permanent changes: vim edit /etc/security/limits.conf file, modify the hard nofile xxxx and soft nofile xxxx, where xxxx is the number to be set. Save and exit. Regarding the difference between hard and soft, please refer to the 5th reference link below.

Related recommendations: "Linux Video Tutorial"

The above is the detailed content of what is linux fd. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!