【fd】为什么 /proc/sys/fs/file-nr和 /p

2022-05-04  本文已影响0人  Bogon

我想检查系统实际使用了多少个文件描述符:

cat /proc/sys/fs/file-nr 
12750   0   753795

file-nr的值由3部分组成:
1,已经分配的文件描述符数;
2,已经分配但未使用的文件描述符数;
3,内核最大能分配的文件描述符数

众所周知,在相应进程的/proc/$pid/fd 目录下存放了此进程所有打开的fd。

我想知道为什么和以下命令中的数字(占用的文件描述符)对不上?

for pid in $(lsof | awk '{ print $2 }' | uniq); do find /proc/$pid/fd/ -type l 2>&1 | grep -v "No"; done | wc -l
11069

lsof仅列出进程ID.要获取有关线程的信息,您应该使用ps -eLf.根据 man proc:

/proc/[pid]/task (since Linux 2.6.0-test6)
          This is a directory that contains one subdirectory for each
          thread in the process.  The name of each subdirectory is the
          numerical thread ID ([tid]) of the thread (see gettid(2)).
          Within each of these subdirectories, there is a set of files
          with the same names and contents as under the /proc/[pid]
          directories.  For attributes that are shared by all threads,
          the contents for each of the files under the task/[tid]
          subdirectories will be the same as in the corresponding file
          in the parent /proc/[pid] directory (e.g., in a multithreaded
          process, all of the task/[tid]/cwd files will have the same
          value as the /proc/[pid]/cwd file in the parent directory,
          since all of the threads in a process share a working
          directory).  For attributes that are distinct for each thread,
          the corresponding files under task/[tid] may have different
          values (e.g., various fields in each of the task/[tid]/status
          files may be different for each thread).

          In a multithreaded process, the contents of the
          /proc/[pid]/task directory are not available if the main
          thread has already terminated (typically by calling
          pthread_exit(3)).

我会通过运行来计算打开文件描述符的数量:

ps -eL | awk 'NR > 1 { print $1, $2 }' | \
while read x; do \
    find /proc/${x% *}/task/${x#* }/fd/ -type l; \
done | wc -l

结果是17270

让我们看看自启动以来分配了多少描述符:

cat /proc/sys/fs/file-nr 
12750   0   753795

Why there is an excess of number of file descriptors in /proc/[pid]/task/[tid]/fd over the number of allocated file handles in /proc/sys/fs/file-nr?

I suppose that they are created by forked child processes.

image.png

现在应该明了了,有些文件描述符可能不是本进程自己打开的,如通过fork()从父进程继承而来的。

参考

Number of file descriptors: different between /proc/sys/fs/file-nr and /proc/$pid/fd?
https://serverfault.com/questions/485262/number-of-file-descriptors-different-between-proc-sys-fs-file-nr-and-proc-pi

Linux中最大文件描述符数
https://leokongwq.github.io/2016/11/09/linux-max-fd.html

How do linux file descriptor limits work?
https://stackoverflow.com/questions/3991223/how-do-linux-file-descriptor-limits-work

limits.conf(5) - Linux man page
https://linux.die.net/man/5/limits.conf

Why can't I tail -f /proc/$pid/fd/1?
https://unix.stackexchange.com/questions/152773/why-cant-i-tail-f-proc-pid-fd-1

Linux查看进程运行输出(/proc/<pid>/fd)
https://blog.csdn.net/u014756245/article/details/120023188

上一篇 下一篇

猜你喜欢

热点阅读