加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Linux > 正文

linux – 文件描述符的数量:/ proc / sys / fs / file-nr和/ pr

发布时间:2020-12-13 18:17:15 所属栏目:Linux 来源:网络整理
导读:我想检查实际使用了多少个文件描述符: cat /proc/sys/fs/file-nr 12750 0 753795 第一列(12750)表示自引导以来分配的文件描述符的数量. 我想知道为什么以下命令中的数字是不同的(假设这一个班轮正在返回正确的值: for pid in $(lsof | awk'{print $2}’| u
我想检查实际使用了多少个文件描述符:
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.,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 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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读