ps display thread name

前端 未结 3 826
梦谈多话
梦谈多话 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: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

提交回复
热议问题