Changes: Ready for 5.13
[man-pages.git] / man3 / clock_getcpuclockid.3
blobf1fee0911486aa3d91ee5028d7c0f8ce2c5b5ae1
1 .\" Copyright (c) 2008, 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 CLOCK_GETCPUCLOCKID 3 2021-03-22 "Linux" "Linux Programmer's Manual"
27 .SH NAME
28 clock_getcpuclockid \- obtain ID of a process CPU-time clock
29 .SH SYNOPSIS
30 .B #include <time.h>
31 .nf
32 .PP
33 .BI "int clock_getcpuclockid(pid_t " pid ", clockid_t *" clockid );
34 .fi
35 .PP
36 Link with \fI\-lrt\fP (only for glibc versions before 2.17).
37 .PP
38 .ad l
39 .RS -4
40 Feature Test Macro Requirements for glibc (see
41 .BR feature_test_macros (7)):
42 .RE
43 .PP
44 .BR clock_getcpuclockid ():
45 .nf
46     _POSIX_C_SOURCE >= 200112L
47 .fi
48 .SH DESCRIPTION
49 The
50 .BR clock_getcpuclockid ()
51 function obtains the ID of the CPU-time clock of the process whose ID is
52 .IR pid ,
53 and returns it in the location pointed to by
54 .IR clockid .
56 .I pid
57 is zero, then the clock ID of the CPU-time clock
58 of the calling process is returned.
59 .SH RETURN VALUE
60 On success,
61 .BR clock_getcpuclockid ()
62 returns 0;
63 on error, it returns one of the positive error numbers listed in ERRORS.
64 .SH ERRORS
65 .TP
66 .B ENOSYS
67 The kernel does not support obtaining the per-process
68 CPU-time clock of another process, and
69 .I pid
70 does not specify the calling process.
71 .TP
72 .B EPERM
73 The caller does not have permission to access
74 the CPU-time clock of the process specified by
75 .IR pid .
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.)
79 .TP
80 .B ESRCH
81 There is no process with the ID
82 .IR pid .
83 .SH VERSIONS
84 The
85 .BR clock_getcpuclockid ()
86 function is available in glibc since version 2.2.
87 .SH ATTRIBUTES
88 For an explanation of the terms used in this section, see
89 .BR attributes (7).
90 .ad l
91 .nh
92 .TS
93 allbox;
94 lbx lb lb
95 l l l.
96 Interface       Attribute       Value
98 .BR clock_getcpuclockid ()
99 T}      Thread safety   MT-Safe
103 .sp 1
104 .SH CONFORMING TO
105 POSIX.1-2001, POSIX.1-2008.
106 .SH NOTES
107 Calling
108 .BR clock_gettime (2)
109 with the clock ID obtained by a call to
110 .BR clock_getcpuclockid ()
111 with a
112 .I pid
113 of 0,
114 is the same as using the clock ID
115 .BR CLOCK_PROCESS_CPUTIME_ID .
116 .SH EXAMPLES
117 The example program below obtains the
118 CPU-time clock ID of the process whose ID is given on the command line,
119 and then uses
120 .BR clock_gettime (2)
121 to obtain the time on that clock.
122 An example run is the following:
124 .in +4n
126 .RB "$" " ./a.out 1" "                 # Show CPU clock of init process"
127 CPU\-time clock for PID 1 is 2.213466748 seconds
130 .SS Program source
133 #define _XOPEN_SOURCE 600
134 #include <stdint.h>
135 #include <stdio.h>
136 #include <unistd.h>
137 #include <stdlib.h>
138 #include <time.h>
141 main(int argc, char *argv[])
143     clockid_t clockid;
144     struct timespec ts;
146     if (argc != 2) {
147         fprintf(stderr, "%s <process\-ID>\en", argv[0]);
148         exit(EXIT_FAILURE);
149     }
151     if (clock_getcpuclockid(atoi(argv[1]), &clockid) != 0) {
152         perror("clock_getcpuclockid");
153         exit(EXIT_FAILURE);
154     }
156     if (clock_gettime(clockid, &ts) == \-1) {
157         perror("clock_gettime");
158         exit(EXIT_FAILURE);
159     }
161     printf("CPU\-time clock for PID %s is %jd.%09ld seconds\en",
162             argv[1], (intmax_t) ts.tv_sec, ts.tv_nsec);
163     exit(EXIT_SUCCESS);
166 .SH SEE ALSO
167 .BR clock_getres (2),
168 .BR timer_create (2),
169 .BR pthread_getcpuclockid (3),
170 .BR time (7)