mirror of
https://github.com/LCTT/TranslateProject.git
synced 2025-02-25 00:50:15 +08:00
Merge pull request #3206 from strugglingyouth/master
翻译完成 20150817 Linux FAQs with Answers--How to count the number of threads in a process on Linux.md
This commit is contained in:
commit
d0d60c4d0e
@ -1,51 +0,0 @@
|
|||||||
translation by strugglingyouth
|
|
||||||
Linux FAQs with Answers--How to count the number of threads in a process on Linux
|
|
||||||
================================================================================
|
|
||||||
> **Question**: I have an application running, which forks a number of threads at run-time. I want to know how many threads are actively running in the program. What is the easiest way to check the thread count of a process on Linux?
|
|
||||||
|
|
||||||
If you want to see the number of threads per process in Linux environments, there are several ways to do it.
|
|
||||||
|
|
||||||
### Method One: /proc ###
|
|
||||||
|
|
||||||
The proc pseudo filesystem, which resides in /proc directory, is the easiest way to see the thread count of any active process. The /proc directory exports in the form of readable text files a wealth of information related to existing processes and system hardware such as CPU, interrupts, memory, disk, etc.
|
|
||||||
|
|
||||||
$ cat /proc/<pid>/status
|
|
||||||
|
|
||||||
The above command will show detailed information about the process with <pid>, which includes process state (e.g., sleeping, running), parent PID, UID, GID, the number of file descriptors used, and the number of context switches. The output also indicates **the total number of threads created in a process** as follows.
|
|
||||||
|
|
||||||
Threads: <N>
|
|
||||||
|
|
||||||
For example, to check the thread count of a process with PID 20571:
|
|
||||||
|
|
||||||
$ cat /proc/20571/status
|
|
||||||
|
|
||||||

|
|
||||||
|
|
||||||
The output indicates that the process has 28 threads in it.
|
|
||||||
|
|
||||||
Alternatively, you could simply count the number of directories found in /proc/<pid>/task, as shown below.
|
|
||||||
|
|
||||||
$ ls /proc/<pid>/task | wc
|
|
||||||
|
|
||||||
This is because, for every thread created within a process, there is a corresponding directory created in /proc/<pid>/task, named with its thread ID. Thus the total number of directories in /proc/<pid>/task represents the number of threads in the process.
|
|
||||||
|
|
||||||
### Method Two: ps ###
|
|
||||||
|
|
||||||
If you are an avid user of the versatile ps command, this command can also show you individual threads of a process (with "H" option). The following command will print the thread count of a process. The "h" option is needed to hide the header in the top output.
|
|
||||||
|
|
||||||
$ ps hH p <pid> | wc -l
|
|
||||||
|
|
||||||
If you want to monitor the hardware resources (CPU & memory) consumed by different threads of a process, refer to [this tutorial][1].(注:此文我们翻译过)
|
|
||||||
|
|
||||||
--------------------------------------------------------------------------------
|
|
||||||
|
|
||||||
via: http://ask.xmodulo.com/number-of-threads-process-linux.html
|
|
||||||
|
|
||||||
作者:[Dan Nanni][a]
|
|
||||||
译者:[译者ID](https://github.com/译者ID)
|
|
||||||
校对:[校对者ID](https://github.com/校对者ID)
|
|
||||||
|
|
||||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创翻译,[Linux中国](https://linux.cn/) 荣誉推出
|
|
||||||
|
|
||||||
[a]:http://ask.xmodulo.com/author/nanni
|
|
||||||
[1]:http://ask.xmodulo.com/view-threads-process-linux.html
|
|
@ -0,0 +1,51 @@
|
|||||||
|
|
||||||
|
Linux 有问必答 - 如何在 Linux 中统计一个进程的线程数
|
||||||
|
================================================================================
|
||||||
|
> **问题**: 我正在运行一个程序,它在运行时会派生出多个线程。我想知道程序在运行时会有多少线程。在 Linux 中检查进程的线程数最简单的方法是什么?
|
||||||
|
|
||||||
|
如果你想看到 Linux 中每个进程的线程数,有以下几种方法可以做到这一点。
|
||||||
|
|
||||||
|
### 方法一: /proc ###
|
||||||
|
|
||||||
|
proc 伪文件系统,它驻留在 /proc 目录,这是最简单的方法来查看任何活动进程的线程数。 /proc 目录以可读文本文件形式输出,提供现有进程和系统硬件相关的信息如 CPU, interrupts, memory, disk, 等等.
|
||||||
|
|
||||||
|
$ cat /proc/<pid>/status
|
||||||
|
|
||||||
|
上面的命令将显示进程 <pid> 的详细信息,包括过程状态(例如, sleeping, running),父进程 PID,UID,GID,使用的文件描述符的数量,以及上下文切换的数量。输出也包括**进程创建的总线程数**如下所示。
|
||||||
|
|
||||||
|
Threads: <N>
|
||||||
|
|
||||||
|
例如,检查 PID 20571进程的线程数:
|
||||||
|
|
||||||
|
$ cat /proc/20571/status
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
输出表明该进程有28个线程。
|
||||||
|
|
||||||
|
或者,你可以在 /proc/<pid>/task 中简单的统计目录的数量,如下所示。
|
||||||
|
|
||||||
|
$ ls /proc/<pid>/task | wc
|
||||||
|
|
||||||
|
这是因为,对于一个进程中创建的每个线程,在 /proc/<pid>/task 中会创建一个相应的目录,命名为其线程 ID。由此在 /proc/<pid>/task 中目录的总数表示在进程中线程的数目。
|
||||||
|
|
||||||
|
### 方法二: ps ###
|
||||||
|
|
||||||
|
如果你是功能强大的 ps 命令的忠实用户,这个命令也可以告诉你一个进程(用“H”选项)的线程数。下面的命令将输出进程的线程数。“h”选项需要放在前面。
|
||||||
|
|
||||||
|
$ ps hH p <pid> | wc -l
|
||||||
|
|
||||||
|
如果你想监视一个进程的不同线程消耗的硬件资源(CPU & memory),请参阅[此教程][1]。(注:此文我们翻译过)
|
||||||
|
|
||||||
|
--------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
via: http://ask.xmodulo.com/number-of-threads-process-linux.html
|
||||||
|
|
||||||
|
作者:[Dan Nanni][a]
|
||||||
|
译者:[strugglingyouth](https://github.com/strugglingyouth)
|
||||||
|
校对:[校对者ID](https://github.com/校对者ID)
|
||||||
|
|
||||||
|
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创翻译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||||
|
|
||||||
|
[a]:http://ask.xmodulo.com/author/nanni
|
||||||
|
[1]:http://ask.xmodulo.com/view-threads-process-linux.html
|
Loading…
Reference in New Issue
Block a user