Linux ps 显示线程名称
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17514771/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ps display thread name
提问by Ahmed A
Is there a way for ps
(or similar tool) to display the pthread's name?
I wrote the following simple program:
有没有办法ps
(或类似工具)显示 pthread 的名称?我编写了以下简单程序:
// 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.
是否有ps
我可以用来显示上述程序的线程及其名称的命令/实用程序(如)。
$ /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.
我在 Ubuntu 12.10 上运行我的程序。
回答by vvy
note the man page of pthread_setname_np(),which have showed how to get the threads' names:
请注意pthread_setname_np()的手册页,其中显示了如何获取线程的名称:
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.
pthread_setname_np() 在内部写入 /proc 文件系统下的线程特定通信文件:/proc/self/task/[tid]/comm。pthread_getname_np() 从同一位置检索它。
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
例子
下面的程序演示了 pthread_setname_np() 和 pthread_getname_np() 的使用。
以下 shell 会话显示了程序的示例运行:
$ ./a.out
创建了一个线程。默认名称为:a.out
设置后的线程名称是THREADFOO。
^Z#暂停程序
1+ 停止 ./a.out
$ ps H -C a.out -o 'pid tid cmd comm'
PID TID CMD 命令
5990 5990 ./a.out a.out
5990 5991 ./a.out THREADFOO
$ cat /proc/5990/task/5990/comm
a.输出
$ cat /proc/5990/task/5991/comm
线程
回答by osgx
With procps-ng (https://gitlab.com/procps-ng/procps) there are output option -L
and -T
which will print threads names:
随着procps的-NG(https://gitlab.com/procps-ng/procps)有输出选项-L
和-T
将打印线程的名字:
$ ps -eL
$ ps -eT
-l
long format may be used with them:
-l
长格式可以与它们一起使用:
$ ps -eLl
$ ps -eTl
but -f
option will replace thread name with full command line which is the same for all threads.
但-f
选项将用完整的命令行替换线程名称,这对于所有线程都是相同的。
回答by Velkan
Show the thread IDs and names of the process with PID 12345:
显示 PID 为 12345 的进程的线程 ID 和名称:
ps H -o 'tid comm' 12345