Debian Codegraph @

upstream/debian-trixie/man3/clock_getcpuclockid.3

raw
1 '\" t
2 .\" Copyright (c) 2008, Linux Foundation, written by Michael Kerrisk
3 .\" <[email protected]>
4 .\"
5 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
6 .\"
7 .TH clock_getcpuclockid 3 2024-06-15 "Linux man-pages 6.9.1"
8 .SH NAME
9 clock_getcpuclockid \- obtain ID of a process CPU-time clock
10 .SH LIBRARY
11 Standard C library
12 .RI ( libc ", " \-lc ),
13 since glibc 2.17
14 .P
15 Before glibc 2.17,
16 Real-time library
17 .RI ( librt ", " \-lrt )
18 .SH SYNOPSIS
19 .B #include <time.h>
20 .nf
21 .P
22 .BI "int clock_getcpuclockid(pid_t " pid ", clockid_t *" clockid );
23 .fi
24 .P
25 .ad l
26 .RS -4
27 Feature Test Macro Requirements for glibc (see
28 .BR feature_test_macros (7)):
29 .RE
30 .P
31 .BR clock_getcpuclockid ():
32 .nf
33 _POSIX_C_SOURCE >= 200112L
34 .fi
35 .SH DESCRIPTION
36 The
37 .BR clock_getcpuclockid ()
38 function obtains the ID of the CPU-time clock of the process whose ID is
39 .IR pid ,
40 and returns it in the location pointed to by
41 .IR clockid .
42 If
43 .I pid
44 is zero, then the clock ID of the CPU-time clock
45 of the calling process is returned.
46 .SH RETURN VALUE
47 On success,
48 .BR clock_getcpuclockid ()
49 returns 0;
50 on error, it returns one of the positive error numbers listed in ERRORS.
51 .SH ERRORS
52 .TP
53 .B ENOSYS
54 The kernel does not support obtaining the per-process
55 CPU-time clock of another process, and
56 .I pid
57 does not specify the calling process.
58 .TP
59 .B EPERM
60 The caller does not have permission to access
61 the CPU-time clock of the process specified by
62 .IR pid .
63 (Specified in POSIX.1-2001;
64 does not occur on Linux unless the kernel does not support
65 obtaining the per-process CPU-time clock of another process.)
66 .TP
67 .B ESRCH
68 There is no process with the ID
69 .IR pid .
70 .SH ATTRIBUTES
71 For an explanation of the terms used in this section, see
72 .BR attributes (7).
73 .TS
74 allbox;
75 lbx lb lb
76 l l l.
77 Interface Attribute Value
78 T{
79 .na
80 .nh
81 .BR clock_getcpuclockid ()
82 T} Thread safety MT-Safe
83 .TE
84 .SH STANDARDS
85 POSIX.1-2008.
86 .SH HISTORY
87 glibc 2.2.
88 POSIX.1-2001.
89 .SH NOTES
90 Calling
91 .BR clock_gettime (2)
92 with the clock ID obtained by a call to
93 .BR clock_getcpuclockid ()
94 with a
95 .I pid
96 of 0,
97 is the same as using the clock ID
98 .BR CLOCK_PROCESS_CPUTIME_ID .
99 .SH EXAMPLES
100 The example program below obtains the
101 CPU-time clock ID of the process whose ID is given on the command line,
102 and then uses
103 .BR clock_gettime (2)
104 to obtain the time on that clock.
105 An example run is the following:
106 .P
107 .in +4n
108 .EX
109 .RB "$" " ./a.out 1" " # Show CPU clock of init process"
110 CPU\-time clock for PID 1 is 2.213466748 seconds
111 .EE
112 .in
113 .SS Program source
114 \&
115 .\" SRC BEGIN (clock_getcpuclockid.c)
116 .EX
117 #define _XOPEN_SOURCE 600
118 #include <stdint.h>
119 #include <stdio.h>
120 #include <stdlib.h>
121 #include <time.h>
122 #include <unistd.h>
123 \&
124 int
125 main(int argc, char *argv[])
126 {
127 clockid_t clockid;
128 struct timespec ts;
129 \&
130 if (argc != 2) {
131 fprintf(stderr, "%s <process\-ID>\[rs]n", argv[0]);
132 exit(EXIT_FAILURE);
133 }
134 \&
135 if (clock_getcpuclockid(atoi(argv[1]), &clockid) != 0) {
136 perror("clock_getcpuclockid");
137 exit(EXIT_FAILURE);
138 }
139 \&
140 if (clock_gettime(clockid, &ts) == \-1) {
141 perror("clock_gettime");
142 exit(EXIT_FAILURE);
143 }
144 \&
145 printf("CPU\-time clock for PID %s is %jd.%09ld seconds\[rs]n",
146 argv[1], (intmax_t) ts.tv_sec, ts.tv_nsec);
147 exit(EXIT_SUCCESS);
148 }
149 .EE
150 .\" SRC END
151 .SH SEE ALSO
152 .BR clock_getres (2),
153 .BR timer_create (2),
154 .BR pthread_getcpuclockid (3),
155 .BR time (7)