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 *clockid);
12
13 Link with -lrt (only for glibc versions before 2.17).
14
15 Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
16
17 clock_getcpuclockid():
18 _POSIX_C_SOURCE >= 200112L
19
21 The clock_getcpuclockid() function obtains the ID of the CPU-time clock
22 of the process whose ID is pid, and returns it in the location pointed
23 to by clockid. If pid is zero, then the clock ID of the CPU-time clock
24 of the calling process is returned.
25
27 On success, clock_getcpuclockid() returns 0; on error, it returns one
28 of the positive error numbers listed in ERRORS.
29
31 ENOSYS The kernel does not support obtaining the per-process CPU-time
32 clock of another process, and pid does not specify the calling
33 process.
34
35 EPERM The caller does not have permission to access the CPU-time clock
36 of the process specified by pid. (Specified in POSIX.1-2001;
37 does not occur on Linux unless the kernel does not support ob‐
38 taining the per-process CPU-time clock of 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 For an explanation of the terms used in this section, see at‐
48 tributes(7).
49
50 ┌────────────────────────────────────────────┬───────────────┬─────────┐
51 │Interface │ Attribute │ Value │
52 ├────────────────────────────────────────────┼───────────────┼─────────┤
53 │clock_getcpuclockid() │ Thread safety │ MT-Safe │
54 └────────────────────────────────────────────┴───────────────┴─────────┘
55
57 POSIX.1-2001, POSIX.1-2008.
58
60 Calling clock_gettime(2) with the clock ID obtained by a call to
61 clock_getcpuclockid() with a pid of 0, is the same as using the clock
62 ID CLOCK_PROCESS_CPUTIME_ID.
63
65 The example program below obtains the CPU-time clock ID of the process
66 whose ID is given on the command line, and then uses clock_gettime(2)
67 to obtain the time on that clock. An example run is the following:
68
69 $ ./a.out 1 # Show CPU clock of init process
70 CPU-time clock for PID 1 is 2.213466748 seconds
71
72 Program source
73
74 #define _XOPEN_SOURCE 600
75 #include <stdint.h>
76 #include <stdio.h>
77 #include <unistd.h>
78 #include <stdlib.h>
79 #include <time.h>
80
81 int
82 main(int argc, char *argv[])
83 {
84 clockid_t clockid;
85 struct timespec ts;
86
87 if (argc != 2) {
88 fprintf(stderr, "%s <process-ID>\n", argv[0]);
89 exit(EXIT_FAILURE);
90 }
91
92 if (clock_getcpuclockid(atoi(argv[1]), &clockid) != 0) {
93 perror("clock_getcpuclockid");
94 exit(EXIT_FAILURE);
95 }
96
97 if (clock_gettime(clockid, &ts) == -1) {
98 perror("clock_gettime");
99 exit(EXIT_FAILURE);
100 }
101
102 printf("CPU-time clock for PID %s is %jd.%09ld seconds\n",
103 argv[1], (intmax_t) ts.tv_sec, ts.tv_nsec);
104 exit(EXIT_SUCCESS);
105 }
106
108 clock_getres(2), timer_create(2), pthread_getcpuclockid(3), time(7)
109
111 This page is part of release 5.13 of the Linux man-pages project. A
112 description of the project, information about reporting bugs, and the
113 latest version of this page, can be found at
114 https://www.kernel.org/doc/man-pages/.
115
116
117
118Linux 2021-03-22 CLOCK_GETCPUCLOCKID(3)