posts - 297,  comments - 15,  trackbacks - 0
      对于多线程应用程序,如果能够给每个线程命名,那么调试起来的便利是不言而喻的。
      可以用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 阅读(4871) 评论(2)  编辑 收藏 引用 所属分类: Linux_Coding

FeedBack:
# re: 用prctl给线程命名
2012-05-27 21:52 | dhao123@sina.com
请问大侠: 用top命令的时候可以显示修改后的线程名么?如何做呢?  回复  更多评论
  
# re: 用prctl给线程命名
2012-06-02 11:34 | none
用top -H 可以的。
[root@linux-x86 test]# top -H
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
27036 root 25 0 16184 524 420 R 52.7 0.1 0:11.45 xx
26832 root 15 0 90116 3644 2896 R 5.3 0.5 0:00.44 sshd
1 root 15 0 10348 744 624 S 0.0 0.1 0:00.75 init
2 root RT -5 0 0 0 S 0.0 0.0 0:01.43 migration/0
  回复  更多评论
  

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


<2024年3月>
252627282912
3456789
10111213141516
17181920212223
24252627282930
31123456

常用链接

留言簿(10)

随笔分类(307)

随笔档案(297)

algorithm

Books_Free_Online

C++

database

Linux

Linux shell

linux socket

misce

  • cloudward
  • 感觉这个博客还是不错,虽然做的东西和我不大相关,觉得看看还是有好处的

network

OSS

  • Google Android
  • Android is a software stack for mobile devices that includes an operating system, middleware and key applications. This early look at the Android SDK provides the tools and APIs necessary to begin developing applications on the Android platform using the Java programming language.
  • os161 file list

overall

搜索

  •  

最新评论

阅读排行榜

评论排行榜