| 1 |
'\" t |
| 2 |
.\" Copyright, the authors of the Linux man-pages project |
| 3 |
.\" |
| 4 |
.\" SPDX-License-Identifier: Linux-man-pages-copyleft |
| 5 |
.\" |
| 6 |
.TH clock_getcpuclockid 3 2025-05-17 "Linux man-pages 6.18" |
| 7 |
.SH NAME |
| 8 |
clock_getcpuclockid \- obtain ID of a process CPU-time clock |
| 9 |
.SH LIBRARY |
| 10 |
Standard C library |
| 11 |
.RI ( libc ,\~ \-lc ), |
| 12 |
since glibc 2.17 |
| 13 |
.P |
| 14 |
Before glibc 2.17, |
| 15 |
Real-time library |
| 16 |
.RI ( librt ,\~ \-lrt ) |
| 17 |
.SH SYNOPSIS |
| 18 |
.B #include <time.h> |
| 19 |
.nf |
| 20 |
.P |
| 21 |
.BI "int clock_getcpuclockid(pid_t " pid ", clockid_t *" clockid ); |
| 22 |
.fi |
| 23 |
.P |
| 24 |
.ad l |
| 25 |
.RS -4 |
| 26 |
Feature Test Macro Requirements for glibc (see |
| 27 |
.BR feature_test_macros (7)): |
| 28 |
.RE |
| 29 |
.P |
| 30 |
.BR clock_getcpuclockid (): |
| 31 |
.nf |
| 32 |
_POSIX_C_SOURCE >= 200112L |
| 33 |
.fi |
| 34 |
.SH DESCRIPTION |
| 35 |
The |
| 36 |
.BR clock_getcpuclockid () |
| 37 |
function obtains the ID of the CPU-time clock of the process whose ID is |
| 38 |
.IR pid , |
| 39 |
and returns it in the location pointed to by |
| 40 |
.IR clockid . |
| 41 |
If |
| 42 |
.I pid |
| 43 |
is zero, then the clock ID of the CPU-time clock |
| 44 |
of the calling process is returned. |
| 45 |
.SH RETURN VALUE |
| 46 |
On success, |
| 47 |
.BR clock_getcpuclockid () |
| 48 |
returns 0; |
| 49 |
on error, it returns one of the positive error numbers listed in ERRORS. |
| 50 |
.SH ERRORS |
| 51 |
.TP |
| 52 |
.B ENOSYS |
| 53 |
The kernel does not support obtaining the per-process |
| 54 |
CPU-time clock of another process, and |
| 55 |
.I pid |
| 56 |
does not specify the calling process. |
| 57 |
.TP |
| 58 |
.B EPERM |
| 59 |
The caller does not have permission to access |
| 60 |
the CPU-time clock of the process specified by |
| 61 |
.IR pid . |
| 62 |
(Specified in POSIX.1-2001; |
| 63 |
does not occur on Linux unless the kernel does not support |
| 64 |
obtaining the per-process CPU-time clock of another process.) |
| 65 |
.TP |
| 66 |
.B ESRCH |
| 67 |
There is no process with the ID |
| 68 |
.IR pid . |
| 69 |
.SH ATTRIBUTES |
| 70 |
For an explanation of the terms used in this section, see |
| 71 |
.BR attributes (7). |
| 72 |
.TS |
| 73 |
allbox; |
| 74 |
lbx lb lb |
| 75 |
l l l. |
| 76 |
Interface Attribute Value |
| 77 |
T{ |
| 78 |
.na |
| 79 |
.nh |
| 80 |
.BR clock_getcpuclockid () |
| 81 |
T} Thread safety MT-Safe |
| 82 |
.TE |
| 83 |
.SH STANDARDS |
| 84 |
POSIX.1-2008. |
| 85 |
.SH HISTORY |
| 86 |
glibc 2.2. |
| 87 |
POSIX.1-2001. |
| 88 |
.SH NOTES |
| 89 |
Calling |
| 90 |
.BR clock_gettime (2) |
| 91 |
with the clock ID obtained by a call to |
| 92 |
.BR clock_getcpuclockid () |
| 93 |
with a |
| 94 |
.I pid |
| 95 |
of 0, |
| 96 |
is the same as using the clock ID |
| 97 |
.BR CLOCK_PROCESS_CPUTIME_ID . |
| 98 |
.SH EXAMPLES |
| 99 |
The example program below obtains the |
| 100 |
CPU-time clock ID of the process whose ID is given on the command line, |
| 101 |
and then uses |
| 102 |
.BR clock_gettime (2) |
| 103 |
to obtain the time on that clock. |
| 104 |
An example run is the following: |
| 105 |
.P |
| 106 |
.in +4n |
| 107 |
.EX |
| 108 |
.RB "$" " ./a.out 1" " # Show CPU clock of init process" |
| 109 |
CPU\-time clock for PID 1 is 2.213466748 seconds |
| 110 |
.EE |
| 111 |
.in |
| 112 |
.SS Program source |
| 113 |
\& |
| 114 |
.\" SRC BEGIN (clock_getcpuclockid.c) |
| 115 |
.EX |
| 116 |
#define _XOPEN_SOURCE 600 |
| 117 |
#include <stdint.h> |
| 118 |
#include <stdio.h> |
| 119 |
#include <stdlib.h> |
| 120 |
#include <time.h> |
| 121 |
#include <unistd.h> |
| 122 |
\& |
| 123 |
int |
| 124 |
main(int argc, char *argv[]) |
| 125 |
{ |
| 126 |
clockid_t clockid; |
| 127 |
struct timespec ts; |
| 128 |
\& |
| 129 |
if (argc != 2) { |
| 130 |
fprintf(stderr, "%s <process\-ID>\[rs]n", argv[0]); |
| 131 |
exit(EXIT_FAILURE); |
| 132 |
} |
| 133 |
\& |
| 134 |
if (clock_getcpuclockid(atoi(argv[1]), &clockid) != 0) { |
| 135 |
perror("clock_getcpuclockid"); |
| 136 |
exit(EXIT_FAILURE); |
| 137 |
} |
| 138 |
\& |
| 139 |
if (clock_gettime(clockid, &ts) == \-1) { |
| 140 |
perror("clock_gettime"); |
| 141 |
exit(EXIT_FAILURE); |
| 142 |
} |
| 143 |
\& |
| 144 |
printf("CPU\-time clock for PID %s is %jd.%09ld seconds\[rs]n", |
| 145 |
argv[1], (intmax_t) ts.tv_sec, ts.tv_nsec); |
| 146 |
exit(EXIT_SUCCESS); |
| 147 |
} |
| 148 |
.EE |
| 149 |
.\" SRC END |
| 150 |
.SH SEE ALSO |
| 151 |
.BR clock_getres (2), |
| 152 |
.BR timer_create (2), |
| 153 |
.BR pthread_getcpuclockid (3), |
| 154 |
.BR time (7) |