Linux programming-Name a thread

Source: Internet
Author: User

To make it easy to distinguish each thread in a process, you can give each thread a name by Prctl () . In this way, in the process of executing a program that creates multiple threads, it is possible to know which thread a PID or tid corresponds to, and it is helpful for the debugger.

Prctl is a system call that can be used to read and change the properties of a thread. Its user-state interface is defined as follows:

#include <sys/prctl.h>int prctl (int option, unsigned long arg2, unsigned long arg3,   unsigned long arg4, unsigned Long Arg5);

The first parameter, option, tells Prctl what to do with the current thread, and the number of parameters required for different operations is different. The option to get and modify the name of the current thread is the following two:


pr_set_name: Sets the name of the current thread
pr_get_name: Gets the name of the current thread

All two options require only one parameter, which is the string used to store the thread name.

int prctl (int option, unsigned long arg2);

For arg2 There are the following requirements:
Pr_set_name:arg2 A character pointer that holds the name of the thread that will be set, that is, (char *) arg2. The length of the name is 15 bytes maximum and should be terminated with '/'. If the string passed in is longer than 15 bytes, the string is truncated.
Pr_get_name:arg2 needs to be a character pointer that has been allocated space and is not less than 16 in length. After Prctl successfully returns, ARG2 is assigned the current thread name, ending with '/'.
For example, set the thread name:

Char Tname[16];memset (tname, 0, +), snprintf (Tname, playctrl%u), Playid;p rctl (Pr_set_name, tname);

Get Thread Name:

Char tname[16];p rctl (Pr_get_name, tname);

Prctl () executed successfully returned 0, failed to return 1, and set errno.

Note: Prctl () can only set/get the name of the current thread, two extended interfaces pthread_setname_np () and PTHREAD_GETNAME_NP () are available in the version after GLIBC 2.12, and the names of other threads can be set and read in the process.

The thread name is stored in the kernel by the Comm member of the struct task_struct struct, and the Prctl () system invoke operation thread name is also implemented by manipulating the member.

#define  Task_comm_len 16struct task_struct {... char comm[task_comm_len];...};

From the TASK_STRUCT structure definition of the kernel and the implementation of the PRCTL system call, it can be seen that the thread name is up to 15 characters.

In the/proc/PID/task/directory, all active threads in the process are listed, each thread has the same name and process name as the default, and is CmdLine, which makes it easy to find the corresponding relationship between the thread name and the TID by prctl setting the thread name, if there is/ proc/PID/task/tid/comm file, which contains the thread name.

Linux programming-Name a thread

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.