OSDN Git Service

(split) LDP: Update original to LDP v3.50.
[linuxjm/LDP_man-pages.git] / original / man3 / pthread_getcpuclockid.3
1 .\" Copyright (c) 2009 Linux Foundation, written by Michael Kerrisk
2 .\"     <mtk.manpages@gmail.com>
3 .\"
4 .\" %%%LICENSE_START(VERBATIM)
5 .\" Permission is granted to make and distribute verbatim copies of this
6 .\" manual provided the copyright notice and this permission notice are
7 .\" preserved on all copies.
8 .\"
9 .\" Permission is granted to copy and distribute modified versions of this
10 .\" manual under the conditions for verbatim copying, provided that the
11 .\" entire resulting derived work is distributed under the terms of a
12 .\" permission notice identical to this one.
13 .\"
14 .\" Since the Linux kernel and libraries are constantly changing, this
15 .\" manual page may be incorrect or out-of-date.  The author(s) assume no
16 .\" responsibility for errors or omissions, or for damages resulting from
17 .\" the use of the information contained herein.  The author(s) may not
18 .\" have taken the same level of care in the production of this manual,
19 .\" which is licensed free of charge, as they might when working
20 .\" professionally.
21 .\"
22 .\" Formatted or processed versions of this manual, if unaccompanied by
23 .\" the source, must acknowledge the copyright and authors of this work.
24 .\" %%%LICENSE_END
25 .\"
26 .TH PTHREAD_GETCPUCLOCKID 3 2009-02-08 "Linux" "Linux Programmer's Manual"
27 .SH NAME
28 pthread_getcpuclockid \- retrieve ID of a thread's CPU time clock
29 .SH SYNOPSIS
30 .nf
31 .B #include <pthread.h>
32 .B #include <time.h>
33
34 .BI "int pthread_getcpuclockid(pthread_t " thread ", clockid_t *" clock_id);
35 .sp
36 Compile and link with \fI\-pthread\fP.
37 .fi
38 .SH DESCRIPTION
39 The
40 .BR pthread_getcpuclockid ()
41 function returns the clock ID for the CPU time clock of the thread
42 .IR thread .
43 .\" The clockid is constructed as follows:
44 .\" *clockid = CLOCK_THREAD_CPUTIME_ID | (pd->tid << CLOCK_IDFIELD_SIZE)
45 .\" where CLOCK_IDFIELD_SIZE is 3.
46 .SH RETURN VALUE
47 On success, this function returns 0;
48 on error, it returns a nonzero error number.
49 .SH ERRORS
50 .TP
51 .B ENOENT
52 .\" CLOCK_THREAD_CPUTIME_ID not defined
53 Per-thread CPU time clocks are not supported by the system.
54 .\"
55 .\" Looking at nptl/pthread_getcpuclockid.c an ERANGE error would
56 .\" be possible if kernel thread IDs took more than 29 bits (which
57 .\" they currently cannot).
58 .TP
59 .B ESRCH
60 No thread with the ID
61 .I thread
62 could be found.
63 .SH VERSIONS
64 This function is available in glibc since version 2.2.
65 .SH CONFORMING TO
66 POSIX.1-2001.
67 .SH NOTES
68 When
69 .I thread
70 refers to the calling thread,
71 this function returns an identifier that refers to the same clock
72 manipulated by
73 .BR clock_gettime (2)
74 and
75 .BR clock_settime (2)
76 when given the clock ID
77 .BR CLOCK_THREAD_CPUTIME_ID .
78 .SH EXAMPLE
79 The program below creates a thread and then uses
80 .BR clock_gettime (2)
81 to retrieve the total process CPU time,
82 and the per-thread CPU time consumed by the two threads.
83 The following shell session shows an example run:
84 .in +4n
85 .nf
86 $ \fB./a.out\fP
87 Main thread sleeping
88 Subthread starting infinite loop
89 Main thread consuming some CPU time...
90 Process total CPU time:    1.368
91 Main thread CPU time:      0.376
92 Subthread CPU time:        0.992
93 .fi
94 .in
95 .SS Program source
96 \&
97 .nf
98 /* Link with "\-lrt" */
99
100 #include <time.h>
101 #include <stdio.h>
102 #include <stdlib.h>
103 #include <unistd.h>
104 #include <pthread.h>
105 #include <string.h>
106 #include <errno.h>
107
108 #define handle_error(msg) \\
109         do { perror(msg); exit(EXIT_FAILURE); } while (0)
110
111 #define handle_error_en(en, msg) \\
112         do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0)
113
114 static void *
115 thread_start(void *arg)
116 {
117     printf("Subthread starting infinite loop\\n");
118     for (;;)
119         continue;
120 }
121
122 static void
123 pclock(char *msg, clockid_t cid)
124 {
125     struct timespec ts;
126
127     printf("%s", msg);
128     if (clock_gettime(cid, &ts) == \-1)
129         handle_error("clock_gettime");
130     printf("%4ld.%03ld\\n", ts.tv_sec, ts.tv_nsec / 1000000);
131 }
132
133 int
134 main(int argc, char *argv[])
135 {
136     pthread_t thread;
137     clockid_t cid;
138     int j, s;
139
140     s = pthread_create(&thread, NULL, thread_start, NULL);
141     if (s != 0)
142         handle_error_en(s, "pthread_create");
143
144     printf("Main thread sleeping\\n");
145     sleep(1);
146
147     printf("Main thread consuming some CPU time...\\n");
148     for (j = 0; j < 2000000; j++)
149         getppid();
150
151     pclock("Process total CPU time: ", CLOCK_PROCESS_CPUTIME_ID);
152
153     s = pthread_getcpuclockid(pthread_self(), &cid);
154     if (s != 0)
155         handle_error_en(s, "pthread_getcpuclockid");
156     pclock("Main thread CPU time:   ", cid);
157
158     /* The preceding 4 lines of code could have been replaced by:
159        pclock("Main thread CPU time:   ", CLOCK_THREAD_CPUTIME_ID); */
160
161     s = pthread_getcpuclockid(thread, &cid);
162     if (s != 0)
163         handle_error_en(s, "pthread_getcpuclockid");
164     pclock("Subthread CPU time: 1    ", cid);
165
166     exit(EXIT_SUCCESS);         /* Terminates both threads */
167 }
168 .fi
169 .SH SEE ALSO
170 .BR clock_gettime (2),
171 .BR clock_settime (2),
172 .BR timer_create (2),
173 .BR clock_getcpuclockid (3),
174 .BR pthread_self (3),
175 .BR pthreads (7),
176 .BR time (7)