1NANOSLEEP(3P) POSIX Programmer's Manual NANOSLEEP(3P)
2
3
4
6 This manual page is part of the POSIX Programmer's Manual. The Linux
7 implementation of this interface may differ (consult the corresponding
8 Linux manual page for details of Linux behavior), or the interface may
9 not be implemented on Linux.
10
12 nanosleep — high resolution sleep
13
15 #include <time.h>
16
17 int nanosleep(const struct timespec *rqtp, struct timespec *rmtp);
18
20 The nanosleep() function shall cause the current thread to be suspended
21 from execution until either the time interval specified by the rqtp
22 argument has elapsed or a signal is delivered to the calling thread,
23 and its action is to invoke a signal-catching function or to terminate
24 the process. The suspension time may be longer than requested because
25 the argument value is rounded up to an integer multiple of the sleep
26 resolution or because of the scheduling of other activity by the sys‐
27 tem. But, except for the case of being interrupted by a signal, the
28 suspension time shall not be less than the time specified by rqtp, as
29 measured by the system clock CLOCK_REALTIME.
30
31 The use of the nanosleep() function has no effect on the action or
32 blockage of any signal.
33
35 If the nanosleep() function returns because the requested time has
36 elapsed, its return value shall be zero.
37
38 If the nanosleep() function returns because it has been interrupted by
39 a signal, it shall return a value of -1 and set errno to indicate the
40 interruption. If the rmtp argument is non-NULL, the timespec structure
41 referenced by it is updated to contain the amount of time remaining in
42 the interval (the requested time minus the time actually slept). The
43 rqtp and rmtp arguments can point to the same object. If the rmtp argu‐
44 ment is NULL, the remaining time is not returned.
45
46 If nanosleep() fails, it shall return a value of -1 and set errno to
47 indicate the error.
48
50 The nanosleep() function shall fail if:
51
52 EINTR The nanosleep() function was interrupted by a signal.
53
54 EINVAL The rqtp argument specified a nanosecond value less than zero or
55 greater than or equal to 1000 million.
56
57 The following sections are informative.
58
60 None.
61
63 None.
64
66 It is common to suspend execution of a thread for an interval in order
67 to poll the status of a non-interrupting function. A large number of
68 actual needs can be met with a simple extension to sleep() that pro‐
69 vides finer resolution.
70
71 In the POSIX.1‐1990 standard and SVR4, it is possible to implement such
72 a routine, but the frequency of wakeup is limited by the resolution of
73 the alarm() and sleep() functions. In 4.3 BSD, it is possible to write
74 such a routine using no static storage and reserving no system facili‐
75 ties. Although it is possible to write a function with similar func‐
76 tionality to sleep() using the remainder of the timer_*() functions,
77 such a function requires the use of signals and the reservation of some
78 signal number. This volume of POSIX.1‐2017 requires that nanosleep() be
79 non-intrusive of the signals function.
80
81 The nanosleep() function shall return a value of 0 on success and -1 on
82 failure or if interrupted. This latter case is different from sleep().
83 This was done because the remaining time is returned via an argument
84 structure pointer, rmtp, instead of as the return value.
85
87 None.
88
90 clock_nanosleep(), sleep()
91
92 The Base Definitions volume of POSIX.1‐2017, <time.h>
93
95 Portions of this text are reprinted and reproduced in electronic form
96 from IEEE Std 1003.1-2017, Standard for Information Technology -- Por‐
97 table Operating System Interface (POSIX), The Open Group Base Specifi‐
98 cations Issue 7, 2018 Edition, Copyright (C) 2018 by the Institute of
99 Electrical and Electronics Engineers, Inc and The Open Group. In the
100 event of any discrepancy between this version and the original IEEE and
101 The Open Group Standard, the original IEEE and The Open Group Standard
102 is the referee document. The original Standard can be obtained online
103 at http://www.opengroup.org/unix/online.html .
104
105 Any typographical or formatting errors that appear in this page are
106 most likely to have been introduced during the conversion of the source
107 files to man page format. To report such errors, see https://www.ker‐
108 nel.org/doc/man-pages/reporting_bugs.html .
109
110
111
112IEEE/The Open Group 2017 NANOSLEEP(3P)