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