nanosleep - high-resolution sleep
#include <time.h>
int nanosleep(const struct timespec *duration,
struct timespec *_Nullable rem);
Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
nanosleep() suspends the execution of the calling
thread until either at least the time specified in *duration
has elapsed, or the delivery of a signal that triggers the invocation of
a handler in the calling thread or that terminates the process.
If the call is 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 see NOTES).
The timespec(3) structure is used to specify intervals of time with nanosecond precision.
The value of the nanoseconds field must be in the range [0, 999999999].
Compared to sleep(3) and usleep(3), nanosleep() has the following advantages: it provides a higher resolution for specifying the sleep interval; POSIX.1 explicitly specifies that it does not interact with signals; and it makes the task of resuming a sleep that has been interrupted by a signal handler easier.
On successfully sleeping for the requested duration,
nanosleep() returns 0. If the call is interrupted by a
signal handler or encounters an error, then it returns -1, with
errno
set to indicate the error.
clock_nanosleep(2), restart_syscall(2), sched_setscheduler(2), timer_create(2), sleep(3), timespec(3), usleep(3), time(7)