用prctl给线程命名 【源码实现】

来源:互联网 发布:经济学博士申请 知乎 编辑:程序博客网 时间:2024/06/03 09:26

用prctl给线程命名

分类: Linux 系统编程
      对于多线程应用程序,如果能够给每个线程命名,那么调试起来的便利是不言而喻的。
      可以用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


转自:http://www.cppblog.com/beautykingdom/archive/2009/11/08/100419.html

0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 在家被蜘蛛咬了怎么办 被蜘蛛咬了很痒怎么办 被蜘蛛咬了红肿怎么办 宝宝被蜘蛛咬了怎么办 红鹦鹉鱼产卵了怎么办 掉灰的墙面怎么办简单 辣椒上的白粉虱怎么办 巨门在父母宫怎么办 空腹吃柿子胃胀怎么办 吃了山药过敏痒怎么办 瓷砖活动了怎么办的下 家里进了大蛾子怎么办 沙漠玫瑰烂根了怎么办 打麻药的地方疼怎么办 做月子奶不够吃怎么办 坐月子手痛怎么办才好 产后胖了20斤怎么办 手动挡汽车闯动怎么办 孕妇风热感冒了怎么办 孕妇得风热感冒怎么办 小鸡站不起来了怎么办 感冒了流清鼻涕怎么办 哈士奇又吐又拉怎么办 怀孕乳房长妊娠纹怎么办 乳房长妊娠纹了怎么办 宝宝忌奶晚上哭怎么办 20岁胸下垂松软怎么办 断奶时乳房有肿块怎么办 孩子断奶后乳房变小怎么办 断奶了月经不来怎么办 钥匙在门上拔不出来怎么办 钥匙拔不下来了怎么办 养了几天鱼死了怎么办 乌龟的眼睛肿了怎么办 手被鱼刺扎了怎么办 被鱼刺扎手肿了怎么办 手被桂鱼扎了怎么办 三岁宝宝卡鱼刺怎么办 一岁宝宝卡鱼刺怎么办 鱼刺卡在胸口了怎么办 婴儿被鱼刺卡了怎么办