1 .\" Copyright (c) 2008, Linux Foundation, written by Michael Kerrisk
2 .\" <mtk.manpages@gmail.com>
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.
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.
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
22 .\" Formatted or processed versions of this manual, if unaccompanied by
23 .\" the source, must acknowledge the copyright and authors of this work.
26 .TH CLOCK_GETCPUCLOCKID 3 2013-07-04 "Linux" "Linux Programmer's Manual"
28 clock_getcpuclockid \- obtain ID of a process CPU-time clock
33 .BI "int clock_getcpuclockid(pid_t " pid ", clockid_t *" clock_id );
36 Link with \fI\-lrt\fP (only for glibc versions before 2.17).
40 Feature Test Macro Requirements for glibc (see
41 .BR feature_test_macros (7)):
44 .BR clock_getcpuclockid ():
46 _XOPEN_SOURCE\ >=\ 600 || _POSIX_C_SOURCE\ >=\ 200112L
51 .BR clock_getcpuclockid ()
52 function obtains the ID of the CPU-time clock of the process whose ID is
54 and returns it in the location pointed to by
58 is zero, then the clock ID of the CPU-time clock
59 of the calling process is returned.
62 .BR clock_getcpuclockid ()
64 on error, it returns one of the positive error numbers listed in ERRORS.
68 The kernel does not support obtaining the per-process
69 CPU-time clock of another process, and
71 does not specify the calling process.
74 The caller does not have permission to access
75 the CPU-time clock of the process specified by
77 (Specified as an optional error in POSIX.1-2001;
78 does not occur on Linux unless the kernel does not support
79 obtaining the per-process CPU-time clock of another process.)
82 There is no process with the ID
86 .BR clock_getcpuclockid ()
87 function is available in glibc since version 2.2.
89 .SS Multithreading (see pthreads(7))
91 .BR clock_getcpuclockid ()
92 function is thread-safe.
98 with the clock ID obtained by a call to
99 .BR clock_getcpuclockid ()
103 is the same as using the clock ID
104 .BR CLOCK_PROCESS_CPUTIME_ID .
106 The example program below obtains the
107 CPU-time clock ID of the process whose ID is given on the command line,
109 .BR clock_gettime (2)
110 to obtain the time on that clock.
111 An example run is the following:
115 .RB "$" " ./a.out 1" " # Show CPU clock of init process"
116 CPU-time clock for PID 1 is 2.213466748 seconds
122 #define _XOPEN_SOURCE 600
129 main(int argc, char *argv[])
135 fprintf(stderr, "%s <process\-ID>\\n", argv[0]);
139 if (clock_getcpuclockid(atoi(argv[1]), &clockid) != 0) {
140 perror("clock_getcpuclockid");
144 if (clock_gettime(clockid, &ts) == \-1) {
145 perror("clock_gettime");
149 printf("CPU-time clock for PID %s is %ld.%09ld seconds\\n",
150 argv[1], (long) ts.tv_sec, (long) ts.tv_nsec);
155 .BR clock_getres (2),
156 .BR timer_create (2),
157 .BR pthread_getcpuclockid (3),
160 This page is part of release 3.79 of the Linux
163 A description of the project,
164 information about reporting bugs,
165 and the latest version of this page,
167 \%http://www.kernel.org/doc/man\-pages/.