1clock_getcpuclockid(3) Library Functions Manual clock_getcpuclockid(3)
2
3
4
6 clock_getcpuclockid - obtain ID of a process CPU-time clock
7
9 Standard C library (libc, -lc), since glibc 2.17
10
11 Before glibc 2.17, Real-time library (librt, -lrt)
12
14 #include <time.h>
15
16 int clock_getcpuclockid(pid_t pid, clockid_t *clockid);
17
18 Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
19
20 clock_getcpuclockid():
21 _POSIX_C_SOURCE >= 200112L
22
24 The clock_getcpuclockid() function obtains the ID of the CPU-time clock
25 of the process whose ID is pid, and returns it in the location pointed
26 to by clockid. If pid is zero, then the clock ID of the CPU-time clock
27 of the calling process is returned.
28
30 On success, clock_getcpuclockid() returns 0; on error, it returns one
31 of the positive error numbers listed in ERRORS.
32
34 ENOSYS The kernel does not support obtaining the per-process CPU-time
35 clock of another process, and pid does not specify the calling
36 process.
37
38 EPERM The caller does not have permission to access the CPU-time clock
39 of the process specified by pid. (Specified in POSIX.1-2001;
40 does not occur on Linux unless the kernel does not support ob‐
41 taining the per-process CPU-time clock of another process.)
42
43 ESRCH There is no process with the ID pid.
44
46 For an explanation of the terms used in this section, see at‐
47 tributes(7).
48
49 ┌────────────────────────────────────────────┬───────────────┬─────────┐
50 │Interface │ Attribute │ Value │
51 ├────────────────────────────────────────────┼───────────────┼─────────┤
52 │clock_getcpuclockid() │ Thread safety │ MT-Safe │
53 └────────────────────────────────────────────┴───────────────┴─────────┘
54
56 POSIX.1-2008.
57
59 glibc 2.2. POSIX.1-2001.
60
62 Calling clock_gettime(2) with the clock ID obtained by a call to
63 clock_getcpuclockid() with a pid of 0, is the same as using the clock
64 ID CLOCK_PROCESS_CPUTIME_ID.
65
67 The example program below obtains the CPU-time clock ID of the process
68 whose ID is given on the command line, and then uses clock_gettime(2)
69 to obtain the time on that clock. An example run is the following:
70
71 $ ./a.out 1 # Show CPU clock of init process
72 CPU-time clock for PID 1 is 2.213466748 seconds
73
74 Program source
75
76 #define _XOPEN_SOURCE 600
77 #include <stdint.h>
78 #include <stdio.h>
79 #include <stdlib.h>
80 #include <time.h>
81 #include <unistd.h>
82
83 int
84 main(int argc, char *argv[])
85 {
86 clockid_t clockid;
87 struct timespec ts;
88
89 if (argc != 2) {
90 fprintf(stderr, "%s <process-ID>\n", argv[0]);
91 exit(EXIT_FAILURE);
92 }
93
94 if (clock_getcpuclockid(atoi(argv[1]), &clockid) != 0) {
95 perror("clock_getcpuclockid");
96 exit(EXIT_FAILURE);
97 }
98
99 if (clock_gettime(clockid, &ts) == -1) {
100 perror("clock_gettime");
101 exit(EXIT_FAILURE);
102 }
103
104 printf("CPU-time clock for PID %s is %jd.%09ld seconds\n",
105 argv[1], (intmax_t) ts.tv_sec, ts.tv_nsec);
106 exit(EXIT_SUCCESS);
107 }
108
110 clock_getres(2), timer_create(2), pthread_getcpuclockid(3), time(7)
111
112
113
114Linux man-pages 6.05 2023-07-20 clock_getcpuclockid(3)