In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly explains "what does linux fd mean". Friends who are interested might as well take a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn "what does linux fd mean?"
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 to manage these opened files efficiently; it is actually a non-negative integer that refers to the opened file, and all system calls that perform fd O operations are implemented through the file descriptor.
The operating environment of this tutorial: linux5.9.8 system, Dell G3 computer.
In linux, the full name of fd is "File descriptor" and the Chinese name is "file descriptor". The file descriptor is a non-negative integer and is essentially an index value (which is very important).
File descriptor (fd) in Linux
We know that in the Linux system, everything can be regarded as a file, which can be divided into ordinary files, directory files, linked files and device files. When operating these so-called files, we find the name every time we operate, which takes a lot of time and efficiency. So Linux stipulates that each file corresponds to an index, so that when we want to manipulate the file, we can find the index directly and manipulate it.
The file descriptor (file descriptor) is the index created by the kernel to efficiently manage the files that have been opened. It is a non-negative integer (usually a small integer), which is used to refer to the opened file. All system calls that perform the Imacuro operation are implemented through the file descriptor. At the same time, it also stipulates that when the system is just started, 0 is standard input, 1 is standard output, and 2 is standard error. This means that if you open a new file at this time, its file descriptor will be 3, and another file descriptor will be 4. 5.
The Linux kernel has a file descriptor table for all open files, which stores the relationship between each file descriptor as an index and an open file. The simple understanding is the following array. The file descriptor (index) is the subscript of the file descriptor table array, and the contents of the array are pointers to each open file.
The above is just a simple understanding, in fact, with regard to file descriptors, the Linux kernel maintains three data structures:
Process-level file descriptor table
System-level open file descriptor table
I-node table of the file system
After a Linux process starts, a PCB control block is created in kernel space, and there is a file descriptor table (File descriptor table) inside PCB that records all available file descriptors for the current process, that is, all open files of the current process. Each entry of the process-level descriptor table records information about the file descriptors used by a single process, and processes are independent of each other. One process uses the file descriptor 3, and the other process can also use 3. In addition to the process-level file descriptor table, the system also needs to maintain two other tables: open file table and i-node table. These two tables store the open file handle (open file handle) for each open file. An open file handle stores all the information related to an open file.
System-level open file descriptor table:
Current file offset (updated when read () and write () are called, or modified directly using lseek ())
Identity when opening the file (flags parameter of open ())
File access mode (such as read-only mode, write-only mode, or read-write mode set when open () is called)
Settings related to signal driver
A reference to the file's i-node object, that is, the i-node pointer
I-node table of the file system:
File type (for example: regular file, socket, or FIFO) and access permission
A pointer to the list of locks held by the file
Various properties of the file, including file size and timestamps associated with different types of operations
The relationship between the file descriptor, the open file handle, and the i-node is shown in the following figure:
In process A, the file descriptors 1 and 20 both point to the same open file table entry, labeled 23 (pointing to the array elements in the open file table with the subscript 23), which may be formed by calling dup (), dup2 (), fcntl (), or calling the open () function multiple times on the same file.
Process A's file descriptor 2 and process B's file descriptor 2 both point to the same file, which may appear after calling fork () (that is, processes An and B are parent-child processes), or different processes alone call the open () function to open the same file. At this time, the internal descriptor of the process happens to be assigned to the same descriptor that other processes open the file.
Process A descriptor 0 and process B descriptor 3 point to different open file table entries, but these entries all point to the same entry in the i-node table (labeled 1976); in other words, they point to the same file. This happens because each process initiates an open () call to the same file. A similar situation occurs when the same process opens the same file twice.
This means that different file descriptors for the same process can point to the same file; different processes can have the same file descriptor; the same file descriptor for different processes can point to different files (generally, except for three special files: 0, 1, 2); different file descriptors for different processes can also point to the same file.
An example of opening a file on Linux
For example, on Linux, open a file with vim test.py, keep it open, open a new shell, enter the command pidof vim to get the pid number of the vim process, and then ll / proc/$pid/fd to view the list of file descriptors used by the vim process.
/ 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 for standard input 0, standard output 1, and standard error 2 all point to the virtual terminal console / dev/pts/6. Take a look at the following is the newly opened test.py file descriptor, unexpectedly is 4, agreed to start from 3?
I also perplexed this for a long time. After looking up all kinds of information, I finally found the reason in a forum with the help of a boss. Sometimes I still have to try English search if I can't find it in Chinese. Because the principle of the vim editor is to open the open source file and copy it first, then close the source file and then open its own copy, and rename the copy directly to overwrite the source file when you save the modified file. So open the source file with file descriptor 3, and then open your own copy it's time to use file descriptor 4, and then close the source file, file descriptor 3 is released, we look at only 4, here it points to the copy file created by vim. This is just a general idea. Let's take a closer look at how vim works, Ambassador Oort Nebula. Here is a screenshot of the forum I saw at that time. The link is here: StackOverFlow.
If you don't believe it, try another process, such as tail.
On Linux, open a file with tail-f test.py, keep it open, open a new shell, enter the command pidof tail to get the pid number of the tail process, and then ll / proc/$pid/fd to view the list of file descriptors used by the tail process. You can see that the file descriptor is indeed used from 3. Tail is not a source file that is opened directly by the file descriptor because the editor does not modify the file. You can actually use the ll / proc/$pid/fd command to get the file descriptor usage of any process that is currently running.
Extended knowledge: Linux configuration system maximum number of open file descriptors
(1) system-level restrictions
In theory, you can open as many file descriptors as the system memory has, but in practice, the kernel will do the corresponding processing. Generally, the maximum number of open files will be 10% of the system memory (calculated in terms of KB), which is called the system-level limit. This number can be viewed with 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 and permanent:
Temporary changes: the original settings will be restored after session is disconnected or the system is rebooted. Use the command sysctl-w fs.file-max=xxxx, where xxxx is the number you want to set.
Permanent change: vim edits the / etc/sysctl.conf file, followed by adding fs.file-max=xxxx, where xxxx is the number you want to set. After saving and exiting, 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 set a default limit on the maximum number of files opened by a single process, that is, user-level limit. The default value for a 32-bit system is generally 1024 ulimit. The default value for a 64-bit system is usually 65535, which can be viewed using the default-n command.
There are also two ways to change user-level restrictions: temporary and permanent:
Temporary changes: the original settings will be restored after session is disconnected or the system is rebooted. Use the command ulimit-SHn xxxx command to modify, where xxxx is the number you want to set.
Permanent change: vim edits the / etc/security/limits.conf file to modify the hard nofile xxxx and soft nofile xxxx in it, where xxxx is the number you want to set. Save and exit.
At this point, I believe you have a deeper understanding of "what linux fd refers to". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.