Background:
We often encounter the problem of handle leaks when developing Linux online servers. Because in the Linux system design to follow everything is the principle of the document, that is, disk files, directories, network sockets, disks, pipelines, and so on, all these are files, when we open it will return an FD, that is, the file handle. If you open the file frequently, or open a network socket and forget to release it, there will be a handle leak. In a Linux system, the number of file handles that a process can invoke is limited, by default the maximum number of handles that each process can invoke is 1024, and if this limit is exceeded, the process will not be able to get a new handle, and from the result it cannot open a new file or network socket. For online servers, a denial of service condition occurs.
To view and modify a handle:
The maximum number of handles per process limit can be viewed through ulimit–n in a Linux system, and the maximum number of handles for a process is modified by ULIMIT–HSN 10240. When the number of handles reaches the limit, the "too many files open" appears.
There are several ways to see how many handles a process occupies:
1) through CAT/PROC/PID/FD can view thread PID number open thread;
2) through the lsof command, the/usr/sbin/lsof-p 21404 command results are as follows:
COMMAND PID USER FD TYPE DEVICE SIZE NODE NAME
Vas 21404 root cwd DIR 8,3 4096 30195729/home/users/root/vas
Vas 21404 root RTD DIR 8,2 4096 2/
Vas 21404 root txt REG 8,3 112201650 30195914/home/users/root/vas
Vas 21404 root mem REG 0,0 0 [heap] (Stat:no such file or directory)
Vas 21404 root mem REG 8,2 105080 339377/lib64/ld-2.3.4.so
Vas 21404 root mem REG 8,2 1493186 339367/lib64/tls/libc-2.3.4.so
Vas 21404 root mem REG 8,2 17943 339392/lib64/libdl-2.3.4.so
Vas 21404 root mem REG 8,2 613297 339369/lib64/tls/libm-2.3.4.so
Vas 21404 root mem REG 8,2 79336 490463/usr/lib64/libz.so.1.2.1.2
COMMAND: The name of the process
PID: Process identifier
USER: Process Owner
FD: A file descriptor in which the application recognizes the file through a file descriptor. AsCWD,txtwait
TYPE: File types, such asDIR,REG,IPV4,FIEOwait
DEVICE: Specifies the name of the disk
SIZE: The size of the file
NODE: Index node (the identity of the file on disk)
NAME: Open the exact name of the file
In addition, the lsof command can also view the process that occupies the port:
/usr/sbin/lsof-i: 9001