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 └──────────────────────┴───────────────┴─────────┘
56 POSIX.1-2001, POSIX.1-2008.
57
59 Calling clock_gettime(2) with the clock ID obtained by a call to
60 clock_getcpuclockid() with a pid of 0, is the same as using the clock
61 ID CLOCK_PROCESS_CPUTIME_ID.
62
64 The example program below obtains the CPU-time clock ID of the process
65 whose ID is given on the command line, and then uses clock_gettime(2)
66 to obtain the time on that clock. An example run is the following:
67
68 $ ./a.out 1 # Show CPU clock of init process
69 CPU-time clock for PID 1 is 2.213466748 seconds
70
71 Program source
72
73 #define _XOPEN_SOURCE 600
74 #include <stdint.h>
75 #include <stdio.h>
76 #include <unistd.h>
77 #include <stdlib.h>
78 #include <time.h>
79
80 int
81 main(int argc, char *argv[])
82 {
83 clockid_t clockid;
84 struct timespec ts;
85
86 if (argc != 2) {
87 fprintf(stderr, "%s <process-ID>\n", argv[0]);
88 exit(EXIT_FAILURE);
89 }
90
91 if (clock_getcpuclockid(atoi(argv[1]), &clockid) != 0) {
92 perror("clock_getcpuclockid");
93 exit(EXIT_FAILURE);
94 }
95
96 if (clock_gettime(clockid, &ts) == -1) {
97 perror("clock_gettime");
98 exit(EXIT_FAILURE);
99 }
100
101 printf("CPU-time clock for PID %s is %jd.%09ld seconds\n",
102 argv[1], (intmax_t) ts.tv_sec, ts.tv_nsec);
103 exit(EXIT_SUCCESS);
104 }
105
107 clock_getres(2), timer_create(2), pthread_getcpuclockid(3), time(7)
108
110 This page is part of release 5.10 of the Linux man-pages project. A
111 description of the project, information about reporting bugs, and the
112 latest version of this page, can be found at
113 https://www.kernel.org/doc/man-pages/.
114
115
116
117Linux 2020-11-01 CLOCK_GETCPUCLOCKID(3)