在linux下開發(fā)的多線程系統(tǒng)中,每個(gè)線程的調(diào)試和監(jiān)控一直比較麻煩,無法精準(zhǔn)定位,現(xiàn)在有了解決辦法了。
linux下的prctl庫自kernel 2.6.9后支持
api定義如下
簡(jiǎn)單實(shí)現(xiàn)代碼:
現(xiàn)在能夠?yàn)榫€程設(shè)置名字了,那么如何看到呢
linux下的prctl庫自kernel 2.6.9后支持
PR_SET_NAME選項(xiàng),用于設(shè)置進(jìn)程名字
,linux的進(jìn)程一般使用lwp,所以這個(gè)函數(shù)可以設(shè)置線程名字。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.
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.
簡(jiǎn)單實(shí)現(xiàn)代碼:
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) ;
}

{
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) ;
}
現(xiàn)在能夠?yàn)榫€程設(shè)置名字了,那么如何看到呢
ps -eL -o pid,user,lwp,comm
top -H
top -H