linux – 文件描述符的数量:/ proc / sys / fs / file-nr和/ proc / $pid / fd之间有什么不同?

前端之家收集整理的这篇文章主要介绍了linux – 文件描述符的数量:/ proc / sys / fs / file-nr和/ proc / $pid / fd之间有什么不同?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想检查实际使用了多少个文件描述符:
cat /proc/sys/fs/file-nr 
12750   0   753795

第一列(12750)表示自引导以来分配的文件描述符的数量.

我想知道为什么以下命令中的数字是不同的(假设这一个班轮正在返回正确的值:

for pid in $(lsof | awk'{print $2}’| uniq);找到/ proc / $pid / fd / -type l 2>& 1 | grep -v“不”;完成| 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 07001).
          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.,var@R_403_437@s 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 07002).

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

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 
11616   0   398855

为什么/ proc / [pid] / task / [tid] / fd中的文件描述符数量超过/ proc / sys / fs / file-nr中分配的文件句柄数?我想它们是由分叉的子进程创建的:

man fork

The child inherits copies of the parent’s set of open file
descriptors.

man pthreads

POSIX.1 also requires that threads share a range of other
attributes (i.e.,these attributes are process-wide rather than
per-thread):
– process ID

  • parent process ID

  • process group ID and session ID

  • controlling terminal

  • user and group IDs

  • open file descriptors

原文链接:https://www.f2er.com/linux/401542.html

猜你在找的Linux相关文章