Use prctl to name the thread

Source: Internet
Author: User

For multi-threaded applications, if you can name each thread, the convenience of debugging is self-evident.
Today, I read the weekly report on lwn and saw someone adding the interface named by prctl to other threads in the process. I learned that the interface named for the thread already exists, then write the following verification code:

#include
#include
#include
void* 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;
}

Compile and run:

Xiaosuo @ gentux test $ GCC t_threadname.c-l pthread
Xiaosuo @ gentux test $./A. Out
Xx

On another terminal, find the PID of A. out through PS:

Xiaosuo @ gentux test $ PS aux | grep A. Out
Xiaosuo 29882 0.0 0.0 14144 pts/6 SL +./A. Out

Check whether the name works:

Xiaosuo @ gentux test $ CD/proc/29882/task/
Xiaosuo @ gentux task $ ls
29882 29883
Xiaosuo @ gentux task $ CD 29883/
Xiaosuo @ gentux 29883 $ cat ticket line
./A. outxiaosuo @ gentux 29883 $

A little depressing. The line display is still./A. Out. Check the prctl return value through the XX and strace printed at run time to confirm that the prctl is indeed running successfully. I suspect that this name can only be obtained through prctl. It is a bit lost, but it is still unwilling. View PS man and experiment, and finally find "XX ":

Xiaosuo @ gentux 29883 $ PS-l-P 29882
PID lwp tty time cmd
29882 29882 pts/6 00:00:00 A. Out
29882 29883 pts/6 00:00:00 xx

After strace knows that this "XX" is actually hidden in stat and status:

Xiaosuo @ gentux 29883 $ cat stat
29883 (XX) s 7168 29882 7168 34822 29882 11 0 0 0 2 0 0 20 0 2 0 4202560 28515372 14483456 136 18446744073709551615 4194304 4196620 140735304261728 18446744073709551615 0 0 0 0 140435890519585 0 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
Cappr: 0000000000000000
Capeff: 0000000000000000
Capbnd: fffffffffffffeff
Voluntary_ctxt_switches: 4447
Nonvoluntary_ctxt_switches: 0

Verified! :)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.