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, and pid does 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 by pid. (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 ID pid. 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