对于多线程应用程序,如果能够给每个线程命名,那么调试起来的便利是不言而喻的。

今天看LWN上的周报,看到有人正在给prctl添加给进程内其它线程命名的接口,并从中得知,给线程自身命名的接口已经存在,不由窃喜,遂写下以下验证代码:

#include
#include
#includevoid* tmain(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, tmain, NULL);pthread_join(tid, NULL);return 0;
}

编译并运行:

xiaosuo@gentux test $ gcc t_threadname.c -l pthread
xiaosuo@gentux test $ ./a.out
xx

在另一个终端,通过ps找到a.out的pid:

xiaosuo@gentux test $ ps aux | grep a.out
xiaosuo  29882  0.0  0.0  14144   544 pts/6    Sl+  16:23   0:00 ./a.out

看命名是否奏效:

xiaosuo@gentux test $ cd /proc/29882/task/
xiaosuo@gentux task $ ls
29882  29883
xiaosuo@gentux task $ cd 29883/
xiaosuo@gentux 29883 $ cat cmdline
./a.outxiaosuo@gentux 29883 $

有点儿郁闷,cmdline显示的竟然还是./a.out。通过运行时打印的xx和strace检查prctl的返回值确认prctl确实成功运行。怀疑这个名字只能通过prctl获得,有点儿失落,可心仍不甘。查看ps的man,并实验,终于找到了"xx":

xiaosuo@gentux 29883 $ ps -L -p 29882PID   LWP TTY          TIME CMD
29882 29882 pts/6    00:00:00 a.out
29882 29883 pts/6    00:00:00 xx

strace后知道这个“xx”竟然隐匿于stat和status:

xiaosuo@gentux 29883 $ cat stat
29883 (xx) S 7168 29882 7168 34822 29882 4202560 11 0 0 0 2 0 0 0 20 0 2 0 28515372 14483456 136 18446744073709551615 4194304 4196620 140735304261728 18446744073709551615 140435890519585 0 0 0 0 18446744071564503939 0 0 -1 1 0 0 0 0 0
xiaosuo@gentux 29883 $ cat status
Name:   xx
State:  S (sleeping)
Tgid:   29882
Pid:    29883
PPid:   7168
TracerPid:      0
Uid:    1000    1000    1000    1000
Gid:    1000    1000    1000    1000
FDSize: 256
Groups: 10 18 1000 1001 1005
VmPeak:    14144 kB
VmSize:    14144 kB
VmLck:         0 kB
VmHWM:       548 kB
VmRSS:       544 kB
VmData:     8388 kB
VmStk:        84 kB
VmExe:         4 kB
VmLib:      1528 kB
VmPTE:        32 kB
Threads:        2
SigQ:   1/40960
SigPnd: 0000000000000000
ShdPnd: 0000000000000000
SigBlk: 0000000000000000
SigIgn: 0000000000000000
SigCgt: 0000000180000000
CapInh: 0000000000000000
CapPrm: 0000000000000000
CapEff: 0000000000000000
CapBnd: fffffffffffffeff
voluntary_ctxt_switches:        4447
nonvoluntary_ctxt_switches:     0验毕!:)

prctl()函数应用相关推荐

  1. linux 编程--prctl()函数应用

    int prctl ( int option,unsigned long arg2,unsigned long arg3,unsigned long arg4,unsigned long arg5 ) ...

  2. prctl()函数给线程命名

    今天看代码看到一个prctl()函数函数,百度了一下才知道是给线程命名(当然还有其他的功能).下面简述函数的用法: 1.包含的头文件为 sys/prctl.h; 2.函数用法:int 用法:prctl ...

  3. 多线程编程——prctl()函数介绍

    今天在看项目代码时,发现了一个以前没有用过的函数:prctl().查阅相关资料有所得,现总结如下 >>>>>>>>>>>>> ...

  4. prctl()函数详解

    int prctl ( int option,unsigned long arg2,unsigned long arg3,unsigned long arg4,unsigned long arg5 ) ...

  5. prctl函数 linux,正确使用prctl()的方法

    prctl的原型是 int prctl(int option, unsigned long arg2, unsigned long arg3, unsigned long arg4, unsigned ...

  6. C语言:prctl函数的用法

    #include <sys/prctl.h> int prctl(int option, unsigned long arg2, unsigned long arg3, unsigned ...

  7. prctl函数的应用

    int prctl ( int option,unsigned long arg2,unsigned long arg3,unsigned long arg4,unsigned long arg5 ) ...

  8. linux prctl函数,prctl()函数 Unix/Linux

    prctl -在操作过程 内容简介 #include int prctl(intoption, unsigned longarg2, unsigned longarg3 , unsigned long ...

  9. prctl()为线程指定名字

    Llinu中使用prctl函数为线程指定名字 一.前言     有时候我们通过fork().pthread_create()创建出来的新任务其名字和创建任务的parent是一样的,如果有需要,该如何修 ...

最新文章

  1. android dialog 隐藏状态栏_Android开发不得不收集的工具类集合
  2. Linux sync命令的作用分析
  3. 保存单文件为mhtml
  4. 09年杀毒软件大比拼
  5. Ubuntu easypr的配置
  6. 微分方程模型——偏微分方程
  7. 机器人动力学(牛顿欧拉推导)
  8. 玛咖新品牌 卡皇玛卡4月上市
  9. 补题:HOJ吉林selection B-Bribing Eve(Gym-101174B) (象限极角排序)
  10. maven deploy jar包和源码包到私服
  11. 如何进入Github【亲测有效】
  12. 快速傅里叶变换 (FFT)基础
  13. html5鼠标移过切换图片,鼠标移动到图片上切换到另一张图片,移出时又切默认图片...
  14. CSS布局—网格布局Grid(一)
  15. 怎么用PHP抓取百度排名?
  16. Excel2007版的常用功能(17):Excel数学函数
  17. 打字速度对编程的影响大吗?
  18. 原生Array.isArray()判断对象是否为数组
  19. html酷炫电子时钟效果,逼真的HTML5 canvas模拟时钟特效
  20. Mysql 修改密码 (亲测)

热门文章

  1. 主页面加载书籍分类及图书绑定
  2. 降价力度小、没诚意,苹果手机中国市场难翻身
  3. spring+thymeleaf实现表单验证数据双向绑定
  4. 办公室布置(2013)
  5. 宿舍一张桌子怎么放3台笔记本
  6. numpy中*,np.multiply和np.dot辨析
  7. 谁是房租暴涨的真正推手?
  8. Linux 之 centos7与centos8区别
  9. 截取字符串:subString
  10. SpringBoot 多种方式配置错误页面