1nanosleep(2)                  System Calls Manual                 nanosleep(2)
2
3
4

NAME

6       nanosleep - high-resolution sleep
7

LIBRARY

9       Standard C library (libc, -lc)
10

SYNOPSIS

12       #include <time.h>
13
14       int nanosleep(const struct timespec *req,
15                     struct timespec *_Nullable rem);
16
17   Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
18
19       nanosleep():
20           _POSIX_C_SOURCE >= 199309L
21

DESCRIPTION

23       nanosleep()  suspends  the execution of the calling thread until either
24       at least the time specified in *req has elapsed, or the delivery  of  a
25       signal  that triggers the invocation of a handler in the calling thread
26       or that terminates the process.
27
28       If the call is interrupted by a signal handler, nanosleep() returns -1,
29       sets  errno  to EINTR, and writes the remaining time into the structure
30       pointed to by rem unless rem is NULL.  The value of *rem  can  then  be
31       used  to  call  nanosleep() again and complete the specified pause (but
32       see NOTES).
33
34       The timespec(3) structure is used to specify  intervals  of  time  with
35       nanosecond precision.
36
37       The value of the nanoseconds field must be in the range [0, 999999999].
38
39       Compared  to  sleep(3) and usleep(3), nanosleep() has the following ad‐
40       vantages: it provides a higher resolution for specifying the sleep  in‐
41       terval;  POSIX.1  explicitly  specifies  that it does not interact with
42       signals; and it makes the task of resuming a sleep that has been inter‐
43       rupted by a signal handler easier.
44

RETURN VALUE

46       On  successfully  sleeping  for the requested interval, nanosleep() re‐
47       turns 0.  If the call is interrupted by a signal handler or  encounters
48       an error, then it returns -1, with errno set to indicate the error.
49

ERRORS

51       EFAULT Problem with copying information from user space.
52
53       EINTR  The pause has been interrupted by a signal that was delivered to
54              the thread (see signal(7)).  The remaining sleep time  has  been
55              written into *rem so that the thread can easily call nanosleep()
56              again and continue with the pause.
57
58       EINVAL The value in  the  tv_nsec  field  was  not  in  the  range  [0,
59              999999999] or tv_sec was negative.
60

VERSIONS

62       POSIX.1  specifies  that  nanosleep()  should  measure time against the
63       CLOCK_REALTIME clock.  However,  Linux  measures  the  time  using  the
64       CLOCK_MONOTONIC  clock.   This  probably  does  not  matter,  since the
65       POSIX.1 specification  for  clock_settime(2)  says  that  discontinuous
66       changes in CLOCK_REALTIME should not affect nanosleep():
67
68              Setting  the  value  of  the CLOCK_REALTIME clock via clock_set‐
69              time(2) shall have no effect on threads that are blocked waiting
70              for a relative time service based upon this clock, including the
71              nanosleep() function; ...   Consequently,  these  time  services
72              shall expire when the requested relative interval elapses, inde‐
73              pendently of the new or old value of the clock.
74

STANDARDS

76       POSIX.1-2008.
77

HISTORY

79       POSIX.1-2001.
80
81       In order to support applications requiring  much  more  precise  pauses
82       (e.g.,  in  order  to control some time-critical hardware), nanosleep()
83       would handle pauses of up to 2 milliseconds by busy  waiting  with  mi‐
84       crosecond  precision  when called from a thread scheduled under a real-
85       time policy like SCHED_FIFO or SCHED_RR.  This  special  extension  was
86       removed  in  Linux 2.5.39, and is thus not available in Linux 2.6.0 and
87       later kernels.
88

NOTES

90       If the interval specified in req is not an exact multiple of the granu‐
91       larity  underlying  clock  (see  time(7)),  then  the  interval will be
92       rounded up to the next multiple.  Furthermore,  after  the  sleep  com‐
93       pletes,  there may still be a delay before the CPU becomes free to once
94       again execute the calling thread.
95
96       The fact that nanosleep() sleeps for a relative interval can  be  prob‐
97       lematic  if the call is repeatedly restarted after being interrupted by
98       signals, since the time between the interruptions and restarts  of  the
99       call  will  lead to drift in the time when the sleep finally completes.
100       This problem can be avoided by using clock_nanosleep(2) with  an  abso‐
101       lute time value.
102

BUGS

104       If a program that catches signals and uses nanosleep() receives signals
105       at a very high rate, then scheduling delays and rounding errors in  the
106       kernel's  calculation  of  the  sleep  interval and the returned remain
107       value mean that the remain value may steadily  increase  on  successive
108       restarts  of  the  nanosleep()  call.   To  avoid  such  problems,  use
109       clock_nanosleep(2) with the TIMER_ABSTIME flag to sleep to an  absolute
110       deadline.
111
112       In  Linux  2.4,  if nanosleep() is stopped by a signal (e.g., SIGTSTP),
113       then the call fails with the error EINTR after the thread is resumed by
114       a  SIGCONT  signal.  If the system call is subsequently restarted, then
115       the time that the thread spent in the  stopped  state  is  not  counted
116       against  the  sleep interval.  This problem is fixed in Linux 2.6.0 and
117       later kernels.
118

SEE ALSO

120       clock_nanosleep(2),     restart_syscall(2),      sched_setscheduler(2),
121       timer_create(2), sleep(3), timespec(3), usleep(3), time(7)
122
123
124
125Linux man-pages 6.05              2023-03-30                      nanosleep(2)
Impressum