linux文件描述符open file descriptors与open files的区别

一个文件被打开,也可能没有文件描述符,比如current working diretories,memory mapped files and executable text files ;losf可以查岀某个进程打开的文件数目:

root@GitLab ~]# sh
sh-4.1# lsof |grep 1407
rpcbind   1407          rpc  cwd       DIR              253,0     4096          2 /
rpcbind   1407          rpc  rtd       DIR              253,0     4096          2 /
rpcbind   1407          rpc  txt       REG              253,0    54560     784900 /sbin/rpcbind
rpcbind   1407          rpc  mem       REG              253,0    65960    1439007 /lib64/libnss_files-2.12.so
rpcbind   1407          rpc  mem       REG              253,0    19536    1438997 /lib64/libdl-2.12.so
rpcbind   1407          rpc  mem       REG              253,0    36584    1438992 /lib64/libgssglue.so.1.0.0
rpcbind   1407          rpc  mem       REG              253,0   113432    1439001 /lib64/libnsl-2.12.so
rpcbind   1407          rpc  mem       REG              253,0  1923352    1438991 /lib64/libc-2.12.so
rpcbind   1407          rpc  mem       REG              253,0   142688    1439015 /lib64/libpthread-2.12.so
rpcbind   1407          rpc  mem       REG              253,0   166976    1438998 /lib64/libtirpc.so.1.0.10
rpcbind   1407          rpc  mem       REG              253,0    40792    1439080 /lib64/libwrap.so.0.7.6
rpcbind   1407          rpc  mem       REG              253,0   154664    1438979 /lib64/ld-2.12.so
rpcbind   1407          rpc    0u      CHR                1,3      0t0       3857 /dev/null
rpcbind   1407          rpc    1u      CHR                1,3      0t0       3857 /dev/null
rpcbind   1407          rpc    2u      CHR                1,3      0t0       3857 /dev/null
rpcbind   1407          rpc    3r      REG              253,0        0     133514 /var/run/rpcbind.lock
rpcbind   1407          rpc    4u     sock                0,6      0t0       9743 can't identify protocol
rpcbind   1407          rpc    5u     unix 0xffff88003742b480      0t0       9658 /var/run/rpcbind.sock
rpcbind   1407          rpc    6u     IPv4               9660      0t0        UDP *:sunrpc 
rpcbind   1407          rpc    7u     IPv4               9662      0t0        UDP *:734 
rpcbind   1407          rpc    8u     IPv4               9663      0t0        TCP *:sunrpc (LISTEN)
rpcbind   1407          rpc    9u     IPv6               9665      0t0        UDP *:sunrpc 
rpcbind   1407          rpc   10u     IPv6               9667      0t0        UDP *:734 
rpcbind   1407          rpc   11u     IPv6               9668      0t0        TCP *:sunrpc (LISTEN)
sh-4.1# lsof |grep 1407|wc -l
24

以下方法用于查询进程使用的文件 描述符的数目:

sh-4.1# ls -l /proc/1407/fd
total 0
lrwx------ 1 root root 64 Nov 23 10:26 0 -> /dev/null
lrwx------ 1 root root 64 Nov 23 10:26 1 -> /dev/null
lrwx------ 1 root root 64 Nov 23 10:26 10 -> socket:[9667]
lrwx------ 1 root root 64 Nov 23 10:26 11 -> socket:[9668]
lrwx------ 1 root root 64 Nov 23 10:26 2 -> /dev/null
lr-x------ 1 root root 64 Nov 23 10:26 3 -> /var/run/rpcbind.lock
lrwx------ 1 root root 64 Nov 23 10:26 4 -> socket:[9743]
lrwx------ 1 root root 64 Nov 23 10:26 5 -> socket:[9658]
lrwx------ 1 root root 64 Nov 23 10:26 6 -> socket:[9660]
lrwx------ 1 root root 64 Nov 23 10:26 7 -> socket:[9662]
lrwx------ 1 root root 64 Nov 23 10:26 8 -> socket:[9663]
lrwx------ 1 root root 64 Nov 23 10:26 9 -> socket:[9665]
sh-4.1# ls -l /proc/1407/fd|wc -l
13

进程1407总共打开24个文件,只占用了13个文件 描述符

查看文件描述符的设置:

sh-4.1# cat /proc/sys/fs/file-max 
1000000
--该参数可以动态修改
原文地址:https://www.cnblogs.com/bass6/p/6092756.html