Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the handle to the Linux file

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the relevant knowledge of "what is the Linux file handle". Xiaobian shows you the operation process through actual cases. The operation method is simple and fast, and the practicality is strong. I hope this article "what is the Linux file handle" can help you solve the problem.

In file I/O, to read data from a file, the application first calls an operating system function passing the file name and selects a path to the file to open it. This function retrieves a sequence number, the file handle, that uniquely identifies the open file. To read a block of data from a file, the application calls the ReadFile function and passes the address of the file handle in memory and the number of bytes to copy to the operating system.

Linux handles

All things or resources in Linux exist in the form of files, such as messages, shared memory, connections, etc., and handles can be understood as pointers to these files.

For these handles, Linux has a limit on the number of handles that a single process can open by default, which can be viewed with the following command:

$ ulimit -a

To view the maximum number of handles for a process, the command is:

$ ulimit -a PID

Modification of the number of handles per process (default value will be restored after system restart)

$ ulimit -n Number of handles

Permanently modify the number of single process handles

$ cat >> /etc/security/limits.conf

Note: After modification, you need to reopen the terminal and use ulimit -a to view the number of handles.

Query the number of handles occupied by the specified process in the system

$ lsof -n | grep [PID| Process name]| wc -l

Query is the number of handles occupied by processes in the system

$ lsof -n | awk '{print $2}' | sort | uniq -c | sort -nr | more

There are several file types: DIR: Indicates a directory. CHR: Indicates the character type. BLK: Block device type. UNIX: UNIX domain socket. FIFO: First-in-first-out (FIFO) queue. IPv4: Internet Protocol (IP) Sockets.

LINUX places a limit on the number of file handles that can be opened. Constraints are divided into three levels: system level, user level and process level. If a process opens more file handles than it can handle, an error is reported if the file is opened again. Therefore, when programming, if the process is going to occupy a large number of file handles, it is necessary to see whether to adjust the maximum number of file handles that can be opened, and also pay attention to close to release unused file handles.

system level

$ cat /proc/sys/fs/file-max

user level

$ ulimit -n

Process level

$ cat /proc/PID/limits The content of "What is a Linux file handle" is introduced here. Thank you for reading it. If you want to know more about industry-related knowledge, you can pay attention to the industry information channel. Xiaobian will update different knowledge points for you every day.

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report