OSDN Git Service

(split) LDP_man-pages: update original to v3.34.
[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 .\" Permission is granted to make and distribute verbatim copies of this
5 .\" manual provided the copyright notice and this permission notice are
6 .\" preserved on all copies.
7 .\"
8 .\" Permission is granted to copy and distribute modified versions of this
9 .\" manual under the conditions for verbatim copying, provided that the
10 .\" entire resulting derived work is distributed under the terms of a
11 .\" permission notice identical to this one.
12 .\"
13 .\" Since the Linux kernel and libraries are constantly changing, this
14 .\" manual page may be incorrect or out-of-date.  The author(s) assume no
15 .\" responsibility for errors or omissions, or for damages resulting from
16 .\" the use of the information contained herein.  The author(s) may not
17 .\" have taken the same level of care in the production of this manual,
18 .\" which is licensed free of charge, as they might when working
19 .\" professionally.
20 .\"
21 .\" Formatted or processed versions of this manual, if unaccompanied by
22 .\" the source, must acknowledge the copyright and authors of this work.
23 .\"
24 .TH CLOCK_GETCPUCLOCKID 3 2010-09-10 "Linux" "Linux Programmer's Manual"
25 .SH NAME
26 clock_getcpuclockid \- obtain ID of a process CPU-time clock
27 .SH SYNOPSIS
28 .B #include <time.h>
29 .nf
30 .sp
31 .BI "int clock_getcpuclockid(pid_t " pid ", clockid_t *" clock_id );
32 .fi
33 .sp
34 Link with \fI\-lrt\fP.
35 .sp
36 .ad l
37 .in -4n
38 Feature Test Macro Requirements for glibc (see
39 .BR feature_test_macros (7)):
40 .in
41 .sp
42 .BR clock_getcpuclockid ():
43 .RS 4
44 _XOPEN_SOURCE\ >=\ 600 || _POSIX_C_SOURCE\ >=\ 200112L
45 .RE
46 .ad
47 .SH DESCRIPTION
48 The
49 .BR clock_getcpuclockid ()
50 function obtains the ID of the CPU-time clock of the process whose ID is
51 .IR pid ,
52 and returns it in the location pointed to by
53 .IR clock_id .
54 If
55 .I pid
56 is zero, then the clock ID of the CPU-time clock
57 of the calling process is returned.
58 .SH "RETURN VALUE"
59 On success,
60 .BR clock_getcpuclockid ()
61 returns 0;
62 on error, it returns one of the positive error numbers listed in ERRORS.
63 .SH ERRORS
64 .TP
65 .B ENOSYS
66 The kernel does not support obtaining the per-process
67 CPU-time clock of another process, and
68 .I pid
69 does not specify the calling process.
70 .TP
71 .B EPERM
72 The caller does not have permission to access
73 the CPU-time clock of the process specified by
74 .IR pid .
75 (Specified as an optional error in POSIX.1-2001;
76 does not occur on Linux unless the kernel does not support
77 obtaining the per-process CPU-time clock of another process.)
78 .TP
79 .B ESRCH
80 There is no process with the ID
81 .IR pid .
82 .SH VERSIONS
83 The
84 .BR clock_getcpuclockid ()
85 function is available in glibc since version 2.2.
86 .SH CONFORMING TO
87 POSIX.1-2001.
88 .SH NOTES
89 Calling
90 .BR clock_gettime (2)
91 with the clock ID obtained by a call to
92 .BR clock_getcpuclockid ()
93 with a
94 .I pid
95 of 0,
96 is the same as using the clock ID
97 .BR CLOCK_PROCESS_CPUTIME_ID .
98 .SH EXAMPLE
99 The example program below obtains the
100 CPU-time clock ID of the process whose ID is given on the command line,
101 and then uses
102 .BR clock_gettime (2)
103 to obtain the time on that clock.
104 An example run is the following:
105 .in +4n
106 .nf
107
108 .RB "$" " ./a.out 1" "                 # Show CPU clock of init process"
109 CPU-time clock for PID 1 is 2.213466748 seconds
110 .fi
111 .in
112 .SS Program source
113 \&
114 .nf
115 #define _XOPEN_SOURCE 600
116 #include <stdio.h>
117 #include <unistd.h>
118 #include <stdlib.h>
119 #include <time.h>
120
121 int
122 main(int argc, char *argv[])
123 {
124     clockid_t clockid;
125     struct timespec ts;
126
127     if (argc != 2) {
128         fprintf(stderr, "%s <process-ID>\\n", argv[0]);
129         exit(EXIT_FAILURE);
130     }
131
132     if (clock_getcpuclockid(atoi(argv[1]), &clockid) != 0) {
133         perror("clock_getcpuclockid");
134         exit(EXIT_FAILURE);
135     }
136
137     if (clock_gettime(clockid, &ts) == \-1) {
138         perror("clock_gettime");
139         exit(EXIT_FAILURE);
140     }
141
142     printf("CPU-time clock for PID %s is %ld.%09ld seconds\\n",
143             argv[1], (long) ts.tv_sec, (long) ts.tv_nsec);
144     exit(EXIT_SUCCESS);
145 }
146 .fi
147 .SH SEE ALSO
148 .BR clock_getres (2),
149 .BR timer_create (2),
150 .BR pthread_getcpuclockid (3),
151 .BR time (7)