對于多線程應用程序,如果能夠給每個線程命名,那么調試起來的便利是不言而喻的。
可以用prctl給進程內其它線程命名的接口,測試代碼如下:
#include <stdio.h> #include <pthread.h> #include <sys/prctl.h> #include <unistd.h>
void* pFunc(void *arg) { char name[32]; prctl(PR_SET_NAME, (unsigned long)"xx"); prctl(PR_GET_NAME, (unsigned long)name); printf("%s\n", name); while (1) sleep(1); }
int main(void) { pthread_t tid; pthread_create(&tid, NULL, pFunc, NULL); pthread_join(tid, NULL); return 0; }
|
makefile:
.PHONY: all
all: thread
thread : thread.cpp
g++ -ggdb -Wall -lpthread -o thread thread.cpp
clean:
rm -f thread *.swp
看thread進程,但是還看不到線程信息
beauty@linux-gznp:~/code/test> ps aux | grep thread | grep beauty | grep -v grep
beauty 8364 0.0 0.3 10872 904 pts/2 Sl 03:24 0:00 ./thread
再給ps加幾個參數就ok了。
ps -L -p `ps aux | grep thread | grep $USER | grep -v grep | awk '{print $2}'`
PID LWP TTY TIME CMD
8364 8364 pts/2 00:00:00 thread
8364 8365 pts/2 00:00:00 xx
這里的-L,也可以使用-T,只是打出的詳細信息有點兒不同。具體如下:
-L Show threads, possibly with LWP and NLWP columns
-T Show threads, possibly with SPID column
posted on 2009-11-08 22:16
chatler 閱讀(5029)
評論(2) 編輯 收藏 引用 所屬分類:
Linux_Coding