OSDN Git Service

LDP: Update original to LDP v3.68
[linuxjm/LDP_man-pages.git] / original / 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 2013-07-04 "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 .sp
33 .BI "int clock_getcpuclockid(pid_t " pid ", clockid_t *" clock_id );
34 .fi
35 .sp
36 Link with \fI\-lrt\fP (only for glibc versions before 2.17).
37 .sp
38 .ad l
39 .in -4n
40 Feature Test Macro Requirements for glibc (see
41 .BR feature_test_macros (7)):
42 .in
43 .sp
44 .BR clock_getcpuclockid ():
45 .RS 4
46 _XOPEN_SOURCE\ >=\ 600 || _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 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.)
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 .SS Multithreading (see pthreads(7))
90 The
91 .BR clock_getcpuclockid ()
92 function is thread-safe.
93 .SH CONFORMING TO
94 POSIX.1-2001.
95 .SH NOTES
96 Calling
97 .BR clock_gettime (2)
98 with the clock ID obtained by a call to
99 .BR clock_getcpuclockid ()
100 with a
101 .I pid
102 of 0,
103 is the same as using the clock ID
104 .BR CLOCK_PROCESS_CPUTIME_ID .
105 .SH EXAMPLE
106 The example program below obtains the
107 CPU-time clock ID of the process whose ID is given on the command line,
108 and then uses
109 .BR clock_gettime (2)
110 to obtain the time on that clock.
111 An example run is the following:
112 .in +4n
113 .nf
114
115 .RB "$" " ./a.out 1" "                 # Show CPU clock of init process"
116 CPU-time clock for PID 1 is 2.213466748 seconds
117 .fi
118 .in
119 .SS Program source
120 \&
121 .nf
122 #define _XOPEN_SOURCE 600
123 #include <stdio.h>
124 #include <unistd.h>
125 #include <stdlib.h>
126 #include <time.h>
127
128 int
129 main(int argc, char *argv[])
130 {
131     clockid_t clockid;
132     struct timespec ts;
133
134     if (argc != 2) {
135         fprintf(stderr, "%s <process\-ID>\\n", argv[0]);
136         exit(EXIT_FAILURE);
137     }
138
139     if (clock_getcpuclockid(atoi(argv[1]), &clockid) != 0) {
140         perror("clock_getcpuclockid");
141         exit(EXIT_FAILURE);
142     }
143
144     if (clock_gettime(clockid, &ts) == \-1) {
145         perror("clock_gettime");
146         exit(EXIT_FAILURE);
147     }
148
149     printf("CPU-time clock for PID %s is %ld.%09ld seconds\\n",
150             argv[1], (long) ts.tv_sec, (long) ts.tv_nsec);
151     exit(EXIT_SUCCESS);
152 }
153 .fi
154 .SH SEE ALSO
155 .BR clock_getres (2),
156 .BR timer_create (2),
157 .BR pthread_getcpuclockid (3),
158 .BR time (7)
159 .SH COLOPHON
160 This page is part of release 3.68 of the Linux
161 .I man-pages
162 project.
163 A description of the project,
164 information about reporting bugs,
165 and the latest version of this page,
166 can be found at
167 \%http://www.kernel.org/doc/man\-pages/.