ps display thread name

前端 未结 3 707
梦谈多话
梦谈多话 2021-02-07 23:12

Is there a way for ps (or similar tool) to display the pthread\'s name? I wrote the following simple program:

// th_name.c
#include 
         


        
相关标签:
3条回答
  • 2021-02-07 23:34

    With procps-ng (https://gitlab.com/procps-ng/procps) there are output option -L and -T which will print threads names:

    $ ps -eL
    $ ps -eT
    

    -l long format may be used with them:

    $ ps -eLl
    $ ps -eTl
    

    but -f option will replace thread name with full command line which is the same for all threads.

    0 讨论(0)
  • 2021-02-07 23:37

    note the man page of pthread_setname_np(),which have showed how to get the threads' names:

    pthread_setname_np() internally writes to the thread specific comm file under /proc filesystem: /proc/self/task/[tid]/comm. pthread_getname_np() retrieves it from the same location.

    and

    Example

    The program below demonstrates the use of pthread_setname_np() and pthread_getname_np().

    The following shell session shows a sample run of the program:

    $ ./a.out

    Created a thread. Default name is: a.out

    The thread name after setting it is THREADFOO.

    ^Z #Suspend the program

    1+ Stopped ./a.out

    $ ps H -C a.out -o 'pid tid cmd comm'

    PID TID CMD COMMAND

    5990 5990 ./a.out a.out

    5990 5991 ./a.out THREADFOO

    $ cat /proc/5990/task/5990/comm

    a.out

    $ cat /proc/5990/task/5991/comm

    THREADFOO

    0 讨论(0)
  • 2021-02-07 23:49

    Show the thread IDs and names of the process with PID 12345:

    ps H -o 'tid comm' 12345
    
    0 讨论(0)
提交回复
热议问题