1CLOCK_GETCPUCLOCKID(3) Linux Programmer's Manual CLOCK_GETCPUCLOCKID(3)
2
3
4
6 clock_getcpuclockid - obtain ID of a process CPU-time clock
7
9 #include <time.h>
10
11 int clock_getcpuclockid(pid_t pid, clockid_t *clock_id);
12
13 Link with -lrt.
14
15 Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
16
17 clock_getcpuclockid(): _XOPEN_SOURCE >= 600
18
20 The clock_getcpuclockid() function obtains the ID of the CPU-time clock
21 of the process whose ID is pid, and returns it in the location pointed
22 to by clock_id. If pid is zero, then the clock ID of the CPU-time
23 clock of the calling process is returned.
24
26 On success, clock_getcpuclockid() returns 0; on error, it returns a
27 positive error number.
28
30 ENOSYS The kernel does not support obtaining the per-process CPU-time
31 clock of another process, and pid does not specify the calling
32 process.
33
34 EPERM The caller does not have permission to access the CPU-time clock
35 of the process specified by pid. (Specified as an optional
36 error in POSIX.1-2001; does not occur on Linux unless the kernel
37 does not support obtaining the per-process CPU-time clock of
38 another process.)
39
40 ESRCH There is no process with the ID pid.
41
43 The clock_getcpuclockid() function is available in glibc since version
44 2.2.
45
47 POSIX.1-2001.
48
50 Calling clock_gettime(2) with the clock ID obtained by a call to
51 clock_getcpuclockid() with a pid of 0, is the same as using the clock
52 ID CLOCK_PROCESS_CPUTIME_ID.
53
55 The example program below obtains the CPU-time clock ID of the process
56 whose ID is given on the command line, and then uses clock_gettime(2)
57 to obtain the time on that clock. An example run is the following:
58
59 $ ./a.out 1 # Show CPU clock of init process
60 CPU-time clock for PID 1 is 2.213466748 seconds
61
62 Program source
63
64 #define _XOPEN_SOURCE 600
65 #include <stdio.h>
66 #include <unistd.h>
67 #include <stdlib.h>
68 #include <time.h>
69
70 int
71 main(int argc, char *argv[])
72 {
73 clockid_t clockid;
74 struct timespec ts;
75
76 if (argc != 2) {
77 fprintf(stderr, "%s <process-ID>\n", argv[0]);
78 exit(EXIT_FAILURE);
79 }
80
81 if (clock_getcpuclockid(atoi(argv[1]), &clockid) != 0) {
82 perror("clock_getcpuclockid");
83 exit(EXIT_FAILURE);
84 }
85
86 if (clock_gettime(clockid, &ts) == -1) {
87 perror("clock_gettime");
88 exit(EXIT_FAILURE);
89 }
90
91 printf("CPU-time clock for PID %s is %ld.%09ld seconds\n",
92 argv[1], (long) ts.tv_sec, (long) ts.tv_nsec);
93 exit(EXIT_SUCCESS);
94 }
95
97 clock_getres(2), timer_create(2), pthread_getcpuclockid(3), time(7)
98
100 This page is part of release 3.25 of the Linux man-pages project. A
101 description of the project, and information about reporting bugs, can
102 be found at http://www.kernel.org/doc/man-pages/.
103
104
105
106Linux 2009-02-20 CLOCK_GETCPUCLOCKID(3)