search

what is linux fd

May 09, 2022 pm 04:24 PM
linux

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!

Statement
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
Linux Maintenance Mode: Understanding the PurposeLinux Maintenance Mode: Understanding the PurposeApr 28, 2025 am 12:01 AM

Maintenance mode is used for system maintenance and repair, allowing administrators to work in a simplified environment. 1. System Repair: Repair corrupt file system and boot loader. 2. Password reset: reset the root user password. 3. Package management: Install, update or delete software packages. By modifying the GRUB configuration or entering maintenance mode with specific keys, you can safely exit after performing maintenance tasks.

Linux Operations: Networking and Network ConfigurationLinux Operations: Networking and Network ConfigurationApr 27, 2025 am 12:09 AM

Linux network configuration can be completed through the following steps: 1. Configure the network interface, use the ip command to temporarily set or edit the configuration file persistence settings. 2. Set up a static IP, suitable for devices that require a fixed IP. 3. Manage the firewall and use the iptables or firewalld tools to control network traffic.

Maintenance Mode in Linux: A System Administrator's GuideMaintenance Mode in Linux: A System Administrator's GuideApr 26, 2025 am 12:20 AM

Maintenance mode plays a key role in Linux system management, helping to repair, upgrade and configuration changes. 1. Enter maintenance mode. You can select it through the GRUB menu or use the command "sudosystemctlisolaterscue.target". 2. In maintenance mode, you can perform file system repair and system update operations. 3. Advanced usage includes tasks such as resetting the root password. 4. Common errors such as not being able to enter maintenance mode or mount the file system, can be fixed by checking the GRUB configuration and using the fsck command.

Maintenance Mode in Linux: When and Why to Use ItMaintenance Mode in Linux: When and Why to Use ItApr 25, 2025 am 12:15 AM

The timing and reasons for using Linux maintenance mode: 1) When the system starts up, 2) When performing major system updates or upgrades, 3) When performing file system maintenance. Maintenance mode provides a safe and controlled environment, ensuring operational safety and efficiency, reducing impact on users, and enhancing system security.

Linux: Essential Commands and OperationsLinux: Essential Commands and OperationsApr 24, 2025 am 12:20 AM

Indispensable commands in Linux include: 1.ls: list directory contents; 2.cd: change working directory; 3.mkdir: create a new directory; 4.rm: delete file or directory; 5.cp: copy file or directory; 6.mv: move or rename file or directory. These commands help users manage files and systems efficiently by interacting with the kernel.

Linux Operations: Managing Files, Directories, and PermissionsLinux Operations: Managing Files, Directories, and PermissionsApr 23, 2025 am 12:19 AM

In Linux, file and directory management uses ls, cd, mkdir, rm, cp, mv commands, and permission management uses chmod, chown, and chgrp commands. 1. File and directory management commands such as ls-l list detailed information, mkdir-p recursively create directories. 2. Permission management commands such as chmod755file set file permissions, chownuserfile changes file owner, and chgrpgroupfile changes file group. These commands are based on file system structure and user and group systems, and operate and control through system calls and metadata.

What is Maintenance Mode in Linux? ExplainedWhat is Maintenance Mode in Linux? ExplainedApr 22, 2025 am 12:06 AM

MaintenanceModeinLinuxisaspecialbootenvironmentforcriticalsystemmaintenancetasks.Itallowsadministratorstoperformtaskslikeresettingpasswords,repairingfilesystems,andrecoveringfrombootfailuresinaminimalenvironment.ToenterMaintenanceMode,interrupttheboo

Linux: A Deep Dive into Its Fundamental PartsLinux: A Deep Dive into Its Fundamental PartsApr 21, 2025 am 12:03 AM

The core components of Linux include kernel, file system, shell, user and kernel space, device drivers, and performance optimization and best practices. 1) The kernel is the core of the system, managing hardware, memory and processes. 2) The file system organizes data and supports multiple types such as ext4, Btrfs and XFS. 3) Shell is the command center for users to interact with the system and supports scripting. 4) Separate user space from kernel space to ensure system stability. 5) The device driver connects the hardware to the operating system. 6) Performance optimization includes tuning system configuration and following best practices.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function