ps显示线程名称 [英] ps display thread name
问题描述
ps
(或类似工具)是否可以显示pthread的名称?我编写了以下简单程序:
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;
}
是否有一个命令/实用工具(如 ps
),可用于显示上述程序的线程及其名称.
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
我正在Ubuntu 12.10上运行我的程序.
I am running my program on Ubuntu 12.10.
推荐答案
With procps-ng (https://gitlab.com/procps-ng/procps )有输出选项 -L
和 -T
,它们将打印线程名称:
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 </code>长格式可以与它们一起使用:
-l
long format may be used with them:
$ ps -eLl
$ ps -eTl
但是 -f
选项将用完整的命令行替换线程名称,该名称对于所有线程都是相同的.
but -f
option will replace thread name with full command line which is the same for all threads.
这篇关于ps显示线程名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!