Ps show stream name

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

 // th_name.c #include <stdio.h> #include <pthread.h> void * f1() { printf("f1 : Starting sleep\n"); sleep(30); printf("f1 : Done sleep\n"); } int main() { pthread_t f1_thread; pthread_create(&f1_thread, NULL, f1, NULL); pthread_setname_np(f1_thread, "f1_thread"); printf("Main : Starting sleep\n"); sleep(40); printf("Main : Done sleep\n"); return 0; } 

Is there a command / utility (e.g. ps ) that I can use to display threads for the above program along with their name.

 $ /tmp/th_name > /dev/null & [3] 2055 $ ps -eLf | egrep "th_name|UID" UID PID PPID LWP C NLWP STIME TTY TIME CMD aal 31088 29342 31088 0 2 10:01 pts/4 00:00:00 /tmp/th_name aal 31088 29342 31089 0 2 10:01 pts/4 00:00:00 /tmp/th_name aal 31095 29342 31095 0 1 10:01 pts/4 00:00:00 egrep th_name|UID 

I am running my program on Ubuntu 12.10.

+6
source share
3 answers

pay attention to the pthread_setname_np () man page, which shows how to get thread names:

pthread_setname_np () internally writes to a commit file under the / proc: / proc / self / task / [tid] / comm file system. pthread_getname_np () retrieves it from the same place.

and

Example

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

The following shell session shows an example of starting a program:

$. / a.out

Created a thread. Default Name: a.out

The stream name after installation is THREADFOO.

^ Z # Free the program

1 + Stopped. /a.out

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

TID DIGITAL POWER TEAM

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

+9
source

With procps-ng ( https://gitlab.com/procps-ng/procps ) there is an output option -L and -T that will print the stream names:

 $ ps -eL $ ps -eT 

-L long format can be used with them:

 $ ps -eLl $ ps -eTl 

but the -f option will replace the stream name with the full command line, which will be the same for all threads.

+3
source

Show thread IDs and process names with PID 12345:

 ps H -o 'tid comm' 12345 
0
source

Source: https://habr.com/ru/post/948898/


All Articles