Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Naming threads in Linux

1,287 views
Skip to first unread message

Noob

unread,
Apr 12, 2013, 4:51:45 AM4/12/13
to
Hello,

AFAICT, there are (at least) two ways to give a name to a thread
in a multi-threaded Linux program:

prctl(PR_SET_NAME, name);
http://man7.org/linux/man-pages/man2/prctl.2.html

pthread_setname_np
http://man7.org/linux/man-pages/man3/pthread_setname_np.3.html

Are there other ways in Linux? in POSIX in general?
(I am aware that _np means not portable)

What is the preferred way in Linux?

Given that prctl is in section 2, I assume it is a system call,
while pthread_setname_np is a library call, perhaps it even uses
prctl itself?

Regards.

Xavier Roche

unread,
Apr 12, 2013, 5:13:20 AM4/12/13
to
On 04/12/2013 10:51 AM, Noob wrote:
> AFAICT, there are (at least) two ways to give a name to a thread
> in a multi-threaded Linux program:

[ A bit off-topic on cup as it is Linux-specific ]

A strange behavior to be noticed: on Linux, if you change the "main"
thread's name (ie. the thread who ran main()) using any of these
methods, the top's "COMMAND" will be also replaced by the corresponding
name, and the "killall" Linux shell script command won't be able to find
the original program (but will be able to find the new title)

You may consider doing that:
if (!pthread_equal(pthread_self(), main_thread) != 0) {
return pthread_setname_np(pthread_self(), name);
} else {
...

> What is the preferred way in Linux?

Most probably the high-level one (pthread_setname_np()) which might be
standardized one day

> perhaps it even uses prctl itself?

Yes.

(glibc 2.17 nptl/sysdeps/unix/sysv/linux/pthread_setname.c extract)

int
pthread_setname_np (th, name)
pthread_t th;
const char *name;
{
const struct pthread *pd = (const struct pthread *) th;

/* Unfortunately the kernel headers do not export the TASK_COMM_LEN
macro. So we have to define it here. */
#define TASK_COMM_LEN 16
size_t name_len = strlen (name);
if (name_len >= TASK_COMM_LEN)
return ERANGE;

if (pd == THREAD_SELF)
return prctl (PR_SET_NAME, name) ? errno : 0;
..
}



Casper H.S. Dik

unread,
Apr 12, 2013, 5:19:44 AM4/12/13
to
Noob <ro...@127.0.0.1> writes:

>AFAICT, there are (at least) two ways to give a name to a thread
>in a multi-threaded Linux program:

>prctl(PR_SET_NAME, name);
>http://man7.org/linux/man-pages/man2/prctl.2.html

>pthread_setname_np
>http://man7.org/linux/man-pages/man3/pthread_setname_np.3.html

>Are there other ways in Linux? in POSIX in general?
>(I am aware that _np means not portable)

>What is the preferred way in Linux?

I would pick the library call over the system call;
it already exists in a number of other Unix systems.

Casper

Noob

unread,
Apr 12, 2013, 5:41:14 AM4/12/13
to
Thanks for finding the implementation!

I think I'll use the system call, because doing:

prctl(PR_SET_NAME, __func__);

will silently truncate after 15 or 16 characters, whereas
pthread_setname_np would return ERANGE, requiring me to
copy the name to a temp buffer.

Regards.

Casper H.S. Dik

unread,
Apr 12, 2013, 6:34:06 AM4/12/13
to
Noob <ro...@127.0.0.1> writes:

>Thanks for finding the implementation!

>I think I'll use the system call, because doing:

> prctl(PR_SET_NAME, __func__);

>will silently truncate after 15 or 16 characters, whereas
>pthread_setname_np would return ERANGE, requiring me to
>copy the name to a temp buffer.

It is unfortunately that they have decided to fail rather
ignoring the additional characters as some of the
other implementations do. Then again, there are
also other forms of pthread_setname_np() such as the
three argument version in NetBSD.

(_np is apparently not just "non POSIX" but more like
"not portable")

Casper
0 new messages