zoukankan      html  css  js  c++  java
  • How to count the number of threads in 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.

  • 相关阅读:
    Calling Convention的总结
    形参传递关键点
    linux input输入子系统分析《四》:input子系统整体流程全面分析
    22.Linux-块设备驱动之框架详细分析(详解)
    spring: 使用profiles选择数据源(结合嵌入式数据源EmbeddedDatabaseBuilder)
    spring: 使用嵌入式数据源 EmbeddedDatabaseBuilder
    jsp:jstl标签之控制流程
    jsp: jstl标签库
    jsp:tld标签
    spring boot: 组合注解与元注解
  • 原文地址:https://www.cnblogs.com/zhaoxinshanwei/p/6560307.html
Copyright © 2011-2022 走看看