NANOSLEEP(2)
π€ NANOSLEEP(2) π€
ok so this is the nanosleep page from the System Calls Manual β section 2, bestie, we're in syscall territory now. it's giving "put me to bed but make it precise" energy β¨
πΈ NAME πΈ
nanosleep β high resolution sleep π΄β¨
π LIBRARY π
Standard C Library (libc, -lc) π«Ά
π SYNOPSIS π
like, here's the whole vibe:
#include <time.h>
int
clock_nanosleep(clockid_t clock_id, int flags,
const struct timespec *rqtp, struct timespec *rmtp);
int
nanosleep(const struct timespec *rqtp, struct timespec *rmtp);
β¨ DESCRIPTION β¨
okay so first up, clock_nanosleep(). if the TIMER_ABSTIME flag is NOT set in the flags argument, then clock_nanosleep() pauses the calling thread's whole existence until either:
- the time interval given by the
rqtpargument has fully elapsed, or - a signal gets delivered to the calling process and its action is to invoke a signal-catching function or to terminate the process π
and which clock we're measuring all this with? that's what the clock_id argument is for, bestie π°οΈ
now if TIMER_ABSTIME IS set in flags, the plot twist is that clock_nanosleep() suspends the calling thread until either:
- the value of the clock specified by
clock_idreaches the absolute time specified byrqtp, or - a signal gets delivered to the calling process and its action is to invoke a signal-catching function or to terminate the process π
and here's the tea: if, at the moment of the call, the time value in rqtp is less than or equal to the time value of the specified clock, then clock_nanosleep() just returns immediately and the calling thread is not suspended at all. no nap for you π
the suspension time may lowkey be longer than requested, because the system is out here scheduling other activity. main character problems. also, an unmasked signal will terminate the sleep early, regardless of the SA_RESTART value on the interrupting signal. and yes, the rqtp and rmtp arguments can point to the same object β they're chill like that π«Ά
these are the supported clock_id values, no cap:
CLOCK_MONOTONIC
CLOCK_MONOTONIC_FAST
CLOCK_MONOTONIC_PRECISE
CLOCK_REALTIME
CLOCK_REALTIME_FAST
CLOCK_REALTIME_PRECISE
CLOCK_SECOND
CLOCK_UPTIME
CLOCK_UPTIME_FAST
CLOCK_UPTIME_PRECISE
and nanosleep()? it behaves exactly like clock_nanosleep() with a clock_id argument of CLOCK_REALTIME and without the TIMER_ABSTIME flag in the flags argument. it's the simplified version, the basic bitch of sleep calls (affectionate) π
π₯ RETURN VALUES π₯
these functions return zero when the requested time has actually elapsed. slay.
but if they return for any other reason? then clock_nanosleep() will directly return the error number, and nanosleep() will return -1 with the global variable errno set to indicate the error. periodt.
and ooh, important: if a relative sleep gets interrupted by a signal and rmtp is non-NULL, the timespec structure it references gets updated to contain the unslept amount β that's the request time minus the time actually slept. so you know exactly how much beauty sleep you got robbed of π
π ERRORS π
these functions can fail with the following errors. buckle up:
[EFAULT]β eitherrqtporrmtppoints to memory that is not a valid part of the process address space. rude.[EINTR]β the function was interrupted by the delivery of a signal.[EINVAL]β therqtpargument specified a nanosecond value less than zero or greater than or equal to1000million. pick a real number bestie.[EINVAL]β theflagsargument contained an invalid flag.[EINVAL]β theclock_idargument wasCLOCK_THREAD_CPUTIME_IDor an unrecognized value.[ENOTSUP]β theclock_idargument was valid but not supported by this implementation ofclock_nanosleep().
ngl the EINVAL trio is giving "you had one job" energy. double check your args before you come crying to the kernel π
π SEE ALSO π
clock_gettime(2), sigaction(2), sleep(3)
π STANDARDS π
these functions conform to IEEE Std 1003.1-2008 ("POSIX.1"). iconic behaviour, honestly.
and that's the whole page, bestie π¦ this one's from the Debian crew, dated March 17, 2017 β so yeah, a lil vintage but still absolutely serving. go forth and sleep (your threads, specifically) πβ¨π
π¦ where this page comes from
- debian bookworm: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- debian trixie: freebsd-manpages 12.2-2 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu jammy: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu noble: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu questing: freebsd-manpages 12.2-2
- ubuntu resolute: freebsd-manpages 12.2-2build1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu stonking: freebsd-manpages 12.2-2build1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- Copyright (c) 1986, 1991, 1993 The Regents of the University of California. All rights reserved
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms