nanosleep(2)
โจ nanosleep(2) โ the high-resolution sleep bestie โจ
okay so you found the nanosleep(2) page in the System Calls Manual and honestly? iconic behavior. it's giving "i need to pause but make it precise". let's get into it ๐ซถ
๐ค NAME
nanosleep โ high-resolution sleep
๐ LIBRARY
Standard C library (libc, -lc)
๐ SYNOPSIS
#include <time.h>
int nanosleep(const struct timespec *duration,
struct timespec *_Nullable rem);
Feature Test Macro Requirements for glibc (peep feature_test_macros(7) for the tea โ):
nanosleep():_POSIX_C_SOURCE >= 199309L
๐ฆ DESCRIPTION
so nanosleep() literally suspends the execution of the calling thread until either at least the time specified in *duration has elapsed, or a signal gets delivered that triggers the invocation of a handler in the calling thread, or that terminates the process. whichever hits first, bestie ๐ธ
if the call gets interrupted by a signal handler, nanosleep() returns -1, sets errno to EINTR, and writes the remaining time into the structure pointed to by rem โ unless rem is NULL. the value of *rem can then be used to call nanosleep() again and complete the specified pause (but go read NOTES first, ok? ๐).
the timespec(3) structure is what you use to specify intervals of time with nanosecond precision. tiny tiny time units, we love โจ
the value of the nanoseconds field must be in the range [0, 999999999]. no cap.
compared to sleep(3) and usleep(3), nanosleep() has these advantages, and she ate:
- it provides a higher resolution for specifying the sleep interval ๐
- POSIX.1 explicitly specifies that it does not interact with signals
- it makes the task of resuming a sleep that has been interrupted by a signal handler easier
๐ RETURN VALUE
on successfully sleeping for the requested duration, nanosleep() returns 0. slay. if the call is interrupted by a signal handler or encounters an error, then it returns -1, with errno set to indicate the error ๐ญ
๐จ ERRORS
EFAULTโ problem with copying information from user space.EINTRโ the pause has been interrupted by a signal that was delivered to the thread (seesignal(7)). the remaining sleep time has been written into*remso that the thread can easily callnanosleep()again and continue with the pause.EINVALโ the value in thetv_nsecfield was not in the range[0, 999999999]ortv_secwas negative.
๐ VERSIONS
POSIX.1 specifies that nanosleep() should measure time against the CLOCK_REALTIME clock. however, Linux measures the time using the CLOCK_MONOTONIC clock. and honestly? this probably does not matter, since the POSIX.1 specification for clock_settime(2) says that discontinuous changes in CLOCK_REALTIME should not affect nanosleep(). periodt:
Setting the value of theCLOCK_REALTIMEclock viaclock_settime(2)shall have no effect on threads that are blocked waiting for a relative time service based upon this clock, including thenanosleep()function. Consequently, these time services shall expire when the requested duration elapses, independently of the new or old value of the clock.
๐ STANDARDS
POSIX.1-2024.
๐ง HISTORY
POSIX.1-2001.
in order to support applications requiring much more precise pauses (e.g., in order to control some time-critical hardware), nanosleep() would handle pauses of up to 2 milliseconds by busy waiting with microsecond precision when called from a thread scheduled under a real-time policy like SCHED_FIFO or SCHED_RR. this special extension was removed in Linux 2.5.39, and is thus not available in Linux 2.6.0 and later kernels. rip to a real one ๐
๐ NOTES
if the duration is not an exact multiple of the granularity underlying clock (see time(7)), then the interval will be rounded up to the next multiple. also, after the sleep completes, there may still be a delay before the CPU becomes free to once again execute the calling thread. the CPU is busy, she's got a life ๐ซถ
the fact that nanosleep() sleeps for a relative interval can be problematic if the call is repeatedly restarted after being interrupted by signals, since the time between the interruptions and restarts of the call will lead to drift in the time when the sleep finally completes. this problem can be avoided by using clock_nanosleep(2) with an absolute time value. fr fr, save yourself the drift.
๐ BUGS
if a program that catches signals and usesnanosleep()receives signals at a very high rate, then scheduling delays and rounding errors in the kernel's calculation of the sleep interval and the returned remain value mean that the remain value may steadily increase on successive restarts of thenanosleep()call. ๐ญ
to avoid such problems, use clock_nanosleep(2) with the TIMER_ABSTIME flag to sleep to an absolute deadline. lowkey the move.
in Linux 2.4, if nanosleep() is stopped by a signal (e.g., SIGTSTP), then the call fails with the error EINTR after the thread is resumed by a SIGCONT signal. if the system call is subsequently restarted, then the time that the thread spent in the stopped state is not counted against the sleep interval. this problem is fixed in Linux 2.6.0 and later kernels โ
๐ฅ SEE ALSO
clock_nanosleep(2)restart_syscall(2)sched_setscheduler(2)timer_create(2)sleep(3)timespec(3)usleep(3)time(7)
okay that's the whole lore bestie, go forth and sleep with nanosecond precision ๐ this was Linux man-pages 6.20, dated 2026-02-08 โ catch you on the next page, 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: GPL-2.0-or-later
- Copyright 1996, Markus Kuhn <mkuhn@acm.org>
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms