clock_nanosleep(2)
π€ clock_nanosleep(2) β your girl's guide to sleeping on purpose
okay bestie, gather round π«Ά this is the manpage for clock_nanosleep, a real one. It lives in the System Calls Manual, and it's the fancy sibling of nanosleep(2) that lets you pick which clock you're sleeping against. iconic behavior honestly.
π NAME
clock_nanosleep β high-resolution sleep with specifiable clock β¨
π LIBRARY
Standard C library (libc, -lc), since glibc 2.17 π
Before glibc 2.17? She was living in the Real-time library (librt, -lrt). humble beginnings, we love a glow-up.
π§Ύ SYNOPSIS
#include <time.h>
int clock_nanosleep(clockid_t clockid, int flags,
const struct timespec *t,
struct timespec *_Nullable remain);
Feature Test Macro Requirements for glibc (go look at feature_test_macros(7) if you're nosy):
clock_nanosleep():
_POSIX_C_SOURCE >= 200112L
πΈ DESCRIPTION
So like nanosleep(2), clock_nanosleep() lets the calling thread nap for an interval you specify with nanosecond precision β yes, nanosecond, we're fancy π
. But the glow-up is twofold: it lets the caller choose the clock the sleep interval gets measured against, and it lets you specify that interval as either an absolute or a relative value. options, hun, we love options.
The time values you pass in and get back out are specified using timespec(3) structures.
The clockid argument is the one that says which clock the sleep interval is measured against. It can be one of these lil guys:
CLOCK_REALTIMEβ a settable system-wide real-time clock.CLOCK_TAI(since Linux 3.10) β a system-wide clock derived from wall-clock time but counting leap seconds.CLOCK_MONOTONICβ a nonsettable, monotonically increasing clock that measures time since some unspecified point in the past that does not change after system startup.CLOCK_BOOTTIME(since Linux 2.6.39) β identical toCLOCK_MONOTONIC, except that it also includes any time that the system is suspended π΄.CLOCK_PROCESS_CPUTIME_IDβ a settable per-process clock that measures CPU time consumed by all threads in the process.
See clock_getres(2) for further details on these clocks. And plot twist: the CPU clock IDs returned by clock_getcpuclockid(3) and pthread_getcpuclockid(3) can also be passed in clockid. range, bestie.
If flags is 0, then the value specified in t is interpreted as an interval relative to the current value of the clock specified by clockid.
If flags is TIMER_ABSTIME, then t is interpreted as an absolute time as measured by the clock, clockid. And if t is less than or equal to the current value of the clock, then clock_nanosleep() returns immediately without suspending the calling thread. no nap for you, the nap already happened π.
clock_nanosleep() suspends the execution of the calling thread until either at least the time specified by t has elapsed, or a signal is delivered that causes a signal handler to be called or that terminates the process.
If the call is interrupted by a signal handler, clock_nanosleep() fails with the error EINTR. In addition, if remain is not NULL, and flags was not TIMER_ABSTIME, it returns the remaining unslept time in remain. You can then use that value to call clock_nanosleep() again and complete a (relative) sleep. it's giving "save your progress" π.
π RETURN VALUE
On successfully sleeping for the requested interval, clock_nanosleep() returns 0. slay. If the call is interrupted by a signal handler or encounters an error, then it returns one of the positive error number listed in ERRORS. (yes, positive β this function is built different, we'll get to that vibe.)
π ERRORS
EFAULTβtorremainspecified an invalid address.EINTRβ the sleep was interrupted by a signal handler; seesignal(7).EINVALβ the value in thetv_nsecfield was not in the range [0, 999999999] ortv_secwas negative.EINVALβclockidwas invalid. (CLOCK_THREAD_CPUTIME_IDis not a permitted value forclockid.) not her, not ever.ENOTSUPβ the kernel does not support sleeping against thisclockid. rude, honestly.
π STANDARDS
POSIX.1-2024.
π HISTORY
POSIX.1-2001. Linux 2.6, glibc 2.1. she's been around, she's got lore.
π NOTES
lowkey the most important part, don't skip
If the interval specified in t is not an exact multiple of the granularity underlying clock (see time(7)), then the interval will be rounded up to the next multiple. And even after the sleep completes, there may still be a delay before the CPU becomes free to once again execute the calling thread. patiences, bestie.
Using an absolute timer is useful for preventing timer drift problems of the type described in nanosleep(2). (Such problems are exacerbated in programs that try to restart a relative sleep that is repeatedly interrupted by signals.) To perform a relative sleep that avoids these problems, call clock_gettime(2) for the desired clock, add the desired interval to the returned time value, and then call clock_nanosleep() with the TIMER_ABSTIME flag. that's the whole lil recipe, no cap.
clock_nanosleep() is never restarted after being interrupted by a signal handler, regardless of the use of the sigaction(2) SA_RESTART flag. never ever. no restart era.
The remain argument is unused, and unnecessary, when flags is TIMER_ABSTIME. (An absolute sleep can be restarted using the same t argument.) girlie doesn't need it.
POSIX.1 specifies that clock_nanosleep() has no effect on signals dispositions or the signal mask.
POSIX.1 specifies that after changing the value of the CLOCK_REALTIME clock via clock_settime(2), the new clock value shall be used to determine the time at which a thread blocked on an absolute clock_nanosleep() will wake up; if the new clock value falls past the end of the sleep interval, then the clock_nanosleep() call will return immediately. β° abrupt wake-up, no apology.
POSIX.1 specifies that changing the value of the CLOCK_REALTIME clock via clock_settime(2) shall have no effect on a thread that is blocked on a relative clock_nanosleep(). relative girlies stay unbothered π
.
π SEE ALSO
clock_getres(2), nanosleep(2), restart_syscall(2), timer_create(2), sleep(3), timespec(3), usleep(3), time(7)
and that's the page!! β¨ this one's from the Linux man-pages 6.20, dated 2025-10-29 β go take a nap bestie, you've earned it π¦ππ§
π¦ 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