Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ps display thread name

Tags:

linux

pthreads

ps

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 <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 (like ps) that I can use to display the 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.

like image 911
Ahmed A Avatar asked Jul 07 '13 18:07

Ahmed A


People also ask

How do I see threads in Photoshop?

In ps command, "-T" option enables thread views. The following command list all threads created by a process with <pid>. The "SID" column represents thread IDs, and "CMD" column shows thread names.

How do I get thread ID?

ThreadId can be obtained from the Thread. currentThread() method.

How do I find the thread ID of a process in Linux?

Identifying the thread On Unix® and Linux® systems, you can use the top command: $ top -n 1 -H -p [pid]replacing [pid] with the process ID of the affected process. On Solaris®, you can use the prstat command: $ prstat -L -p [pid]replacing [pid] with the process ID of the affected process.


1 Answers

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.

like image 136
osgx Avatar answered Sep 21 '22 12:09

osgx