Manual Pages for Linux CentOS command on man pthread_getname_np
MyWebUniversity

Manual Pages for Linux CentOS command on man pthread_getname_np

PTHREADSETNAMENP(3) Linux Programmer's Manual PTHREADSETNAMENP(3)

NAME

pthreadsetnamenp, pthreadgetnamenp - set/get the name of a thread SYNOPSIS

#define GNUSOURCE /* See featuretestmacros(7) */

#include int pthreadsetnamenp(pthreadt thread, const char *name); int pthreadgetnamenp(pthreadt thread, const char *name, sizet len);

Compile and link with -pthread. DESCRIPTION By default, all the threads created using pthreadcreate() inherit the program name. The pthreadsetnamenp() function can be used to set a unique name for a thread, which can be useful for debugging multi‐ threaded applications. The thread name is a meaningful C language string, whose length is restricted to 16 characters, including the ter‐ minating null byte ('\0'). The thread argument specifies the thread whose name is to be changed; name specifies the new name. The pthreadgetnamenp() function can be used to retrieve the name of the thread. The thread argument specifies the thread whose name is to be retrieved. The buffer name is used to return the thread name; len specifies the number of bytes available in name. The buffer specified by name should be at least 16 characters in length. The returned thread name in the output buffer will be null terminated. RETURN VALUE On success, these functions return 0; on error, they return a nonzero error number. ERRORS The pthreadsetnamenp() function can fail with the following error: ERANGE The length of the string specified pointed to by name exceeds the allowed limit. The pthreadgetnamenp() function can fail with the following error: ERANGE The buffer specified by name and len is too small to hold the thread name. If either of these functions fails to open /proc/self/task/[tid]/comm, then the call may fail with one of the errors described in open(2). VERSIONS These functions first appeared in glibc in version 2.12. CONFORMING TO These functions are nonstandard GNU extensions. NOTES pthreadsetnamenp() internally writes to the thread specific comm file under /proc filesystem: /proc/self/task/[tid]/comm. pthreadget‐ namenp() retrieves it from the same location. EXAMPLE The program below demonstrates the use of pthreadsetnamenp() and pthreadgetnamenp(). The following shell session shows a sample run of the program:

$ ./a.out Created a thread. Default name is: a.out The thread name after setting it is THREADFOO.

^Z # Suspend the program [1]+ Stopped ./a.out

$ ps H -C a.out -o 'pid tid cmd comm' PID TID CMD COMMAND 5990 5990 ./a.out a.out 5990 5991 ./a.out THREADFOO

$ cat /proc/5990/task/5990/comm a.out

$ cat /proc/5990/task/5991/comm THREADFOO Program source

#define GNUSOURCE

#include

#include

#include

#include

#include

#include

#define NAMELEN 16

#define errExitEN(en, msg) \ do { errno = en; perror(msg); exit(EXITFAILURE); \ } while (0) static void * threadfunc(void *parm) { sleep(5); // allow main program to set the thread name return NULL; } int main(int argc, char **argv) { pthreadt thread; int rc; char threadname[NAMELEN]; rc = pthreadcreate(&thread, NULL, threadfunc, NULL); if (rc != 0) errExitEN(rc, "pthreadcreate"); rc = pthreadgetnamenp(thread, threadname, NAMELEN); if (rc != 0) errExitEN(rc, "pthreadgetnamenp");

printf("Created a thread. Default name is: %s\n", threadname); rc = pthreadsetnamenp(thread, (argc > 1) ? argv[1] : "THREADFOO"); if (rc != 0) errExitEN(rc, "pthreadsetnamenp"); sleep(2); rc = pthreadgetnamenp(thread, threadname, (argc > 2) ? atoi(argv[1]) : NAMELEN); if (rc != 0) errExitEN(rc, "pthreadgetnamenp");

printf("The thread name after setting it is %s.\n", threadname); rc = pthreadjoin(thread, NULL); if (rc != 0) errExitEN(rc, "pthreadjoin"); printf("Done\n"); exit(EXITSUCCESS); } SEE ALSO prctl(2), pthreadcreate(3), pthreads(7) COLOPHON

This page is part of release 3.53 of the Linux man-pages project. A description of the project, and information about reporting bugs, can

be found at http://www.kernel.org/doc/man-pages/.

Linux 2013-06-21 PTHREADSETNAMENP(3)




Contact us      |      About us      |      Term of use      |       Copyright © 2000-2019 MyWebUniversity.com ™