lxyfirst

C++博客 首页 新随笔 联系 聚合 管理
  33 Posts :: 3 Stories :: 27 Comments :: 0 Trackbacks
在linux下开发的多线程系统中,每个线程的调试和监控一直比较麻烦,无法精准定位,现在有了解决办法了。
linux下的prctl库自kernel 2.6.9后支持PR_SET_NAME选项,用于设置进程名字,linux的进程一般使用lwp,所以这个函数可以设置线程名字。
api定义如下
int prctl( int option,unsigned long arg2,unsigned long arg3,unsigned long arg4,unsigned long arg5); 

PR_SET_NAME (since Linux 
2.6.9
Set the process name 
for the calling process, using the value in the location pointed to by (char *) arg2. The name can be up to 16 bytes long, and should be null-terminated if it contains fewer bytes.

PR_GET_NAME (since Linux 
2.6.11
Return the process name 
for the calling process, in the buffer pointed to by (char *) arg2. The buffer should allow space for up to 16 bytes; the returned string will be null-terminated if it is shorter than that.


简单实现代码:

int set_thread_title(const char* fmt, )
{
    
char title [16={0};
    va_list ap;
    va_start(ap, fmt);
    vsnprintf (title, 
sizeof (title) , fmt, ap);
    va_end (ap);

   
return prctl(PR_SET_NAME,title) ;

}

现在能够为线程设置名字了,那么如何看到呢
ps -eL -o pid,user,lwp,comm
top 
-



posted on 2011-03-07 16:11 star 阅读(7683) 评论(2)  编辑 收藏 引用

Feedback

# re: linux下设置线程的名字 2012-05-28 11:24 dhao123@sina.com
请问大侠: top -H 这个命令可以显示出来 之前设置好的thread 名字么?
这个选项字 -H 表示什么? 没有man到啊

就是说通过prctl () 这个接口设置thread 名字后,如何验证啊?
用top命令 或者pgrep 之类的
  回复  更多评论
  

# re: linux下设置线程的名字 2014-10-31 14:57 拉力
@dhao123@sina.com
查看 cd/proc/pid/task/tid/status 文件的第一个名字;
pid :进程号
tid : 线程号  回复  更多评论
  


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