Linux 系统进程关联线程数统计及调整说明

  • A+
所属分类:Linux
高性能企业级服务器首台5折

进程关联线程数统计说明


Linux 系统下,可以通过以下三种方式查询应用程序(进程)创建了多少线程(thread)。

通过  /proc/$PID/status 进行查看

首先,通过 ps 等指令,查询目标进程的进程 ID(PID):

  1. 1
    <span class="com"><span class="hljs-comment"># 以 ssh 进程为例ps -ef | grep sshroot       874     1  0 May31 ?        00:00:01 /usr/sbin/sshd -Droot      3957   874  0 09:59 ?        00:00:00 sshd: root@pts/0root      3993  3959  0 09:59 pts/0    00:00:00 grep —color=auto ssh# 第二列即为相应进程的 PID 号</span></span>

然后,通过如下指令,结合相应进程的 PID 查询其已创建线程数:

  1. 1
    <span class="pun">格式:</span><span class="pln">cat </span><span class="pun">/</span><span class="pln">proc</span><span class="pun">/</span><span class="pln"><span class="hljs-variable">$PID</span></span><span class="pun">/</span><span class="pln">status</span><span class="pun">示例:</span><span class="pln">$ cat </span><span class="pun">/</span><span class="pln">proc</span><span class="pun">/</span><span class="lit">874</span><span class="pun">/</span><span class="pln">status </span><span class="pun">|</span><span class="pln"> grep </span><span class="typ">ThreadThreads</span><span class="pun">:</span>    <span class="lit">1</span><span class="com"><span class="hljs-comment"># 说明当前该进程创建的线程数为 1</span></span>

通过 /proc/$PID/task 目录下的目录数判断线程数量

进程每创建一个线程,系统会在 /proc/$PID/task 目录下创建一个子目录,目录名即为线程 ID。通过统计相应进程对应 task目录下的子目录数量即可统计出线程数量。操作说明如下。

首先,同样的,参阅前述说明,通过 ps 等指令,查询目标进程的进程 ID(PID)。

然后,通过如下指令,结合相应进程的 PID 查询其已创建线程数:

  1. 1
    <span class="pun">格式:</span><span class="pln">ls </span><span class="pun">/</span><span class="pln">proc</span><span class="pun">/</span><span class="pln"><span class="hljs-variable">$PID</span></span><span class="pun">/</span><span class="pln">task </span><span class="pun">|</span><span class="pln"> wc </span><span class="pun">-</span><span class="pln">l</span><span class="pun">示例:</span><span class="pln">$ ls </span><span class="pun">/</span><span class="pln">proc</span><span class="pun">/</span><span class="lit">874</span><span class="pun">/</span><span class="pln">task </span><span class="pun">|</span><span class="pln"> wc </span><span class="pun">-</span><span class="pln">l1</span><span class="com"><span class="hljs-comment"># 返回的数值即为相应进程创建的线程数。</span></span>

通过 ps 命令统计线程

通过 ps 指令也可以统计进程关联线程数。说明如下。

首先,同样的,参阅前述说明,通过 ps 等指令,查询目标进程的进程 ID(PID)。

然后,通过如下指令,结合相应进程的 PID 查询其已创建线程数:

  1. 1
    <span class="pun">格式:</span><span class="pln">ps hH p <span class="hljs-variable">$PID</span> </span><span class="pun">|</span><span class="pln"> wc </span><span class="pun">-</span><span class="pln">l</span><span class="pun">参数说明:-</span><span class="pln">h </span><span class="pun">表示不显示标题-</span><span class="pln">H </span><span class="pun">表示显示进程的层级关系</span><span class="pln"><span class="hljs-variable">$PID</span> </span><span class="pun">是相应进程的进程号示例:</span><span class="pln">$ ps hH p </span><span class="lit">874</span> <span class="pun">|</span><span class="pln"> wc </span><span class="pun">-</span><span class="pln">l1</span><span class="com"><span class="hljs-comment"># 返回的数值即为相应进程创建的线程数。</span></span>

Linux 系统无法创建线程错误原因及解决方法


  • 问题描述:
    业务软件或系统日志中可能出现类似如下日志信息:
    unable to create new native thread
  • 问题分析:
    出现该错误,通常是由于系统无法创建更多进程所致。可以使用如下指令查看当前系统最大可创建线程数。

    1. 1
      <span class="pun">指令:</span><span class="pln"><span class="hljs-built_in">ulimit</span> </span><span class="pun">-</span><span class="pln">a </span><span class="pun">|</span><span class="pln"> grep</span><span class="pun">示例输出:</span><span class="pln">$ <span class="hljs-built_in">ulimit</span> </span><span class="pun">-</span><span class="pln">a </span><span class="pun">|</span><span class="pln"> grep processesmax user processes              </span><span class="pun">(-</span><span class="pln">u</span><span class="pun">)</span> <span class="lit">7812</span><span class="com"><span class="hljs-comment"># max user processes  右侧的数值即表示当前单用户可创建的最大线程总数</span></span>
  • 处理办法:
    根据操作系统的不同,可以参阅该文档修改系统配置文件,调整最大可创建进程数。

 

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: