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 2021-03-22 "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 *" clockid );
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 _POSIX_C_SOURCE >= 200112L
50 .BR clock_getcpuclockid ()
51 function obtains the ID of the CPU-time clock of the process whose ID is
53 and returns it in the location pointed to by
57 is zero, then the clock ID of the CPU-time clock
58 of the calling process is returned.
61 .BR clock_getcpuclockid ()
63 on error, it returns one of the positive error numbers listed in ERRORS.
67 The kernel does not support obtaining the per-process
68 CPU-time clock of another process, and
70 does not specify the calling process.
73 The caller does not have permission to access
74 the CPU-time clock of the process specified by
76 (Specified in POSIX.1-2001;
77 does not occur on Linux unless the kernel does not support
78 obtaining the per-process CPU-time clock of another process.)
81 There is no process with the ID
85 .BR clock_getcpuclockid ()
86 function is available in glibc since version 2.2.
88 For an explanation of the terms used in this section, see
96 Interface Attribute Value
98 .BR clock_getcpuclockid ()
99 T} Thread safety MT-Safe
105 POSIX.1-2001, POSIX.1-2008.
108 .BR clock_gettime (2)
109 with the clock ID obtained by a call to
110 .BR clock_getcpuclockid ()
114 is the same as using the clock ID
115 .BR CLOCK_PROCESS_CPUTIME_ID .
117 The example program below obtains the
118 CPU-time clock ID of the process whose ID is given on the command line,
120 .BR clock_gettime (2)
121 to obtain the time on that clock.
122 An example run is the following:
126 .RB "$" " ./a.out 1" " # Show CPU clock of init process"
127 CPU\-time clock for PID 1 is 2.213466748 seconds
133 #define _XOPEN_SOURCE 600
141 main(int argc, char *argv[])
147 fprintf(stderr, "%s <process\-ID>\en", argv[0]);
151 if (clock_getcpuclockid(atoi(argv[1]), &clockid) != 0) {
152 perror("clock_getcpuclockid");
156 if (clock_gettime(clockid, &ts) == \-1) {
157 perror("clock_gettime");
161 printf("CPU\-time clock for PID %s is %jd.%09ld seconds\en",
162 argv[1], (intmax_t) ts.tv_sec, ts.tv_nsec);
167 .BR clock_getres (2),
168 .BR timer_create (2),
169 .BR pthread_getcpuclockid (3),