pthread_setname_np.3: Minor fixes

Signed-off-by: Michael Kerrisk <mtk.manpages@gmail.com>
This commit is contained in:
Michael Kerrisk 2013-02-02 21:34:47 +01:00
parent 1621a0c820
commit e4a83b0119
1 changed files with 33 additions and 24 deletions

View File

@ -20,7 +20,7 @@
.\" Formatted or processed versions of this manual, if unaccompanied by .\" Formatted or processed versions of this manual, if unaccompanied by
.\" the source, must acknowledge the copyright and authors of this work. .\" the source, must acknowledge the copyright and authors of this work.
.\" .\"
.TH PTHREAD_SETNAME_NP 3 2012-10-24 "Linux" "Linux Programmer's Manual" .TH PTHREAD_SETNAME_NP 3 2013-02-04 "Linux" "Linux Programmer's Manual"
.SH NAME .SH NAME
pthread_setname_np, pthread_getname_np \- set/get the name of a thread pthread_setname_np, pthread_getname_np \- set/get the name of a thread
.SH SYNOPSIS .SH SYNOPSIS
@ -28,62 +28,66 @@ pthread_setname_np, pthread_getname_np \- set/get the name of a thread
.BR "#define _GNU_SOURCE" " /* See feature_test_macros(7) */" .BR "#define _GNU_SOURCE" " /* See feature_test_macros(7) */"
.B #include <pthread.h> .B #include <pthread.h>
.BI "int pthread_setname_np(pthread_t *" thread ", const char *" name "); .BI "int pthread_setname_np(pthread_t *" thread ", const char *" name ");
.BI "int pthread_getname_np(pthread_t *" thread ", const char *" name .BI "int pthread_getname_np(pthread_t *" thread ,
", size_t" len); .BI " const char *" name ", size_t " len );
.fi .fi
.sp .sp
Compile and link with \fI\-pthread\fP. Compile and link with \fI\-pthread\fP.
.SH DESCRIPTION .SH DESCRIPTION
By default, all the threads created using By default, all the threads created using
.BR pthread_create (3) .BR pthread_create ()
inherit the program name. inherit the program name.
Using The
.BR pthread_setname_np (3), .BR pthread_setname_np ()
users can specify a unique name for a thread, fuction can be used to set a unique name for a thread,
which can be particularly useful for debugging complicated which can be useful for debugging
multi-threaded applications. multi-threaded applications.
The thread name is a meaningful C language string, whose length is The thread name is a meaningful C language string, whose length is
restricted to 16 characters. restricted to 16 characters.
Anything greater than this would lead to an error.
The The
.BR pthread_getname_np (3) .BR pthread_getname_np ()
can be used to retrieve the name of the thread. function can be used to retrieve the name of the thread.
The buffer specified by name must be at least 16 characters in length. The buffer specified by
.I name
must be at least 16 characters in length.
The returned thread name in the output buffer will be null terminated. The returned thread name in the output buffer will be null terminated.
.SH RETURN VALUE .SH RETURN VALUE
On success, these functions return 0; On success, these functions return 0;
on error, they return a nonzero error number. on error, they return a nonzero error number.
.SH ERRORS .SH ERRORS
If the functions fail to open /proc/self/task/<TID>/comm, then the
call may fail with one of
the errors described in
.BR open (2).
.TP .TP
.B ERANGE .B ERANGE
The length of the string specified as second argument exceeds the allowed limit. .RB ( pthread_setname_np ())
The length of the string specified pointed to by
.I name
exceeds the allowed limit.
.PP
If either of these functions fails to open
.IR /proc/self/task/[tid]/comm ,
then the call may fail with one of the errors described in
.BR open (2).
.SH NOTES .SH NOTES
.BR pthread_setname_np (3) .BR pthread_setname_np ()
internally writes to the thread specific comm file under internally writes to the thread specific comm file under
.IR /proc .IR /proc
filesystem: filesystem:
.IR /proc/self/task/<tid>/comm. .IR /proc/self/task/[tid]/comm .
.BR pthread_getname_np (3) .BR pthread_getname_np ()
retreives it from the same location. retreives it from the same location.
.SH EXAMPLE .SH EXAMPLE
.PP .PP
The program below demonstrates the use of The program below demonstrates the use of
.BR pthread_setname_np (), .BR pthread_setname_np ()
as well as and
.BR pthread_getname_np (). .BR pthread_getname_np ().
The following shell session shows a sample run of the program: The following shell session shows a sample run of the program:
.in +4n .in +4n
.nf .nf
.RB "$" " ./a.out 1 2 4 7 14" .RB "$" " ./a.out"
Created a thread. Default name is: a.out Created a thread. Default name is: a.out
The thread name after setting it is THREADFOO. The thread name after setting it is THREADFOO.
Done Done
@ -123,17 +127,22 @@ int main(int argc, char **argv)
rc = pthread_create(&thread, NULL, threadfunc, NULL); rc = pthread_create(&thread, NULL, threadfunc, NULL);
errExitEN(rc, "pthread_create"); errExitEN(rc, "pthread_create");
rc = pthread_getname_np(thread, thread_name, NAMELEN); rc = pthread_getname_np(thread, thread_name, NAMELEN);
errExitEN(rc, "pthread_getname_np"); errExitEN(rc, "pthread_getname_np");
printf("Created a thread. Default name is: %s\\n", thread_name); printf("Created a thread. Default name is: %s\\n", thread_name);
rc = pthread_setname_np(thread, "THREADFOO"); rc = pthread_setname_np(thread, "THREADFOO");
errExitEN(rc, "pthread_setname_np"); errExitEN(rc, "pthread_setname_np");
sleep(2); sleep(2);
rc = pthread_getname_np(thread, thread_name, NAMELEN); rc = pthread_getname_np(thread, thread_name, NAMELEN);
errExitEN(rc, "pthread_getname_np"); errExitEN(rc, "pthread_getname_np");
printf("The thread name after setting it is %s.\\n", thread_name); printf("The thread name after setting it is %s.\\n", thread_name);
rc = pthread_join(thread, NULL); rc = pthread_join(thread, NULL);
errExitEN(rc, "pthread_join"); errExitEN(rc, "pthread_join");
printf("Done\\n"); printf("Done\\n");
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
} }