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
38 obtaining 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
48 attributes(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 <stdio.h>
75 #include <unistd.h>
76 #include <stdlib.h>
77 #include <time.h>
78
79 int
80 main(int argc, char *argv[])
81 {
82 clockid_t clockid;
83 struct timespec ts;
84
85 if (argc != 2) {
86 fprintf(stderr, "%s <process-ID>\n", argv[0]);
87 exit(EXIT_FAILURE);
88 }
89
90 if (clock_getcpuclockid(atoi(argv[1]), &clockid) != 0) {
91 perror("clock_getcpuclockid");
92 exit(EXIT_FAILURE);
93 }
94
95 if (clock_gettime(clockid, &ts) == -1) {
96 perror("clock_gettime");
97 exit(EXIT_FAILURE);
98 }
99
100 printf("CPU-time clock for PID %s is %ld.%09ld seconds\n",
101 argv[1], (long) ts.tv_sec, (long) ts.tv_nsec);
102 exit(EXIT_SUCCESS);
103 }
104
106 clock_getres(2), timer_create(2), pthread_getcpuclockid(3), time(7)
107
109 This page is part of release 5.07 of the Linux man-pages project. A
110 description of the project, information about reporting bugs, and the
111 latest version of this page, can be found at
112 https://www.kernel.org/doc/man-pages/.
113
114
115
116Linux 2020-06-09 CLOCK_GETCPUCLOCKID(3)