clock_getcpuclockid(3)
๐ธ clock_getcpuclockid โ the manual page, bestie edition ๐ธ
okay so you found yourself staring at a clock_getcpuclockid(3) page โ that's a Library Functions Manual entry, section 3, aka the "library calls" section, and honestly? she's a little icon. let's get into it ๐
โจ NAME โจ
clock_getcpuclockid โ obtain ID of a process CPU-time clock ๐
๐ LIBRARY ๐
Standard C library (libc, -lc), and she's been there since glibc 2.17 ๐ซถ
But listen โ before glibc 2.17 you had to link the Real-time library: (librt, -lrt). throwback era fr.
๐ SYNOPSIS ๐
#include <time.h>
int clock_getcpuclockid(pid_t pid, clockid_t *clockid);
๐ฆ Feature Test Macro Requirements for glibc (see feature_test_macros(7)): ๐ฆ
clock_getcpuclockid():
_POSIX_C_SOURCE >= 200112L
like yes you have to ask for her. she doesn't just show up. ๐
๐ DESCRIPTION ๐
The clock_getcpuclockid() function goes and gets the ID of the CPU-time clock of the process whose ID is pid, and drops it into the location pointed to by clockid. that's it, that's the whole job, and she does it flawlessly โจ
And here's the cute part: if pid is zero, then you get back the clock ID of the CPU-time clock of the calling process โ i.e. you. main character energy, literally. ๐
โญ RETURN VALUE โญ
On success, clock_getcpuclockid() returns 0. slay. ๐
On error, it returns one of the positive error numbers listed down in ERRORS. not so slay. ๐ญ
๐ ERRORS ๐
ENOSYSโ the kernel does not support obtaining the per-process CPU-time clock of another process, andpiddoes not specify the calling process. so like, the kernel said "no" and you asked about somebody who isn't you. awkward.EPERMโ the caller does not have permission to access the CPU-time clock of the process specified bypid. (Specified in POSIX.1-2001; does not occur on Linux unless the kernel does not support obtaining the per-process CPU-time clock of another process.) so on Linux you're usually fine, but the standard still says it could happen ๐ซESRCHโ there is no process with the IDpid. bestie doesn't exist. ghosted, literally. ๐
๐ง ATTRIBUTES ๐ง
For an explanation of the terms used in this section, see attributes(7).
- Interface:
clock_getcpuclockid() - Attribute: Thread safety
- Value:
MT-Safe
so yes she's MT-Safe โ go off, take her to every thread, she will not betray you ๐ซถโจ
๐ STANDARDS ๐
POSIX.1-2008. certified. iconic. periodt.
๐ธ HISTORY ๐ธ
glibc 2.2. POSIX.1-2001. she's been around, she's seen things ๐
๐ NOTES ๐
Calling clock_gettime(2) with the clock ID you got from a call to clock_getcpuclockid() where the pid was 0, is the same as using the clock ID CLOCK_PROCESS_CPUTIME_ID.
...so you can do it the long way or the short way, they're literally the same thing. no cap. ๐ฅ
๐ EXAMPLES ๐
The example program below obtains the CPU-time clock ID of the process whose ID is given on the command line, and then uses clock_gettime(2) to obtain the time on that clock. An example run is the following:
$ ./a.out 1 # Show CPU clock of init process
CPU-time clock for PID 1 is 2.213466748 seconds
๐ฆ Program source ๐ฆ
#define _XOPEN_SOURCE 600
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
int
main(int argc, char *argv[])
{
clockid_t clockid;
struct timespec ts;
if (argc != 2) {
fprintf(stderr, "%s <process-ID>\n", argv[0]);
exit(EXIT_FAILURE);
}
if (clock_getcpuclockid(atoi(argv[1]), &clockid) != 0) {
perror("clock_getcpuclockid");
exit(EXIT_FAILURE);
}
if (clock_gettime(clockid, &ts) == -1) {
perror("clock_gettime");
exit(EXIT_FAILURE);
}
printf("CPU-time clock for PID %s is %jd.%09ld seconds\n",
argv[1], (intmax_t) ts.tv_sec, ts.tv_nsec);
exit(EXIT_SUCCESS);
}
๐ซถ SEE ALSO ๐ซถ
clock_getres(2), timer_create(2), pthread_getcpuclockid(3), time(7)
okay that's the whole page bestie, now go clock somebody's CPU time ๐
and hey โ this one came out of Linux man-pages 6.20, dated 2025-05-17. ty for reading, mwah ๐ โจ๐
๐ฆ where this page comes from
- arch rolling: man-pages 6.20-1 ยท BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
- nixpkgs unstable: man-pages 6.20
- ๐ the page's license: Linux-man-pages-copyleft
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms