347 lines
8.0 KiB
HTML
347 lines
8.0 KiB
HTML
|
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
|
<HTML><HEAD><TITLE>Man page of NANOSLEEP</TITLE>
|
|
</HEAD><BODY>
|
|
<H1>NANOSLEEP</H1>
|
|
Section: Linux Programmer's Manual (2)<BR>Updated: 2017-09-15<BR><A HREF="#index">Index</A>
|
|
<A HREF="/cgi-bin/man/man2html">Return to Main Contents</A><HR>
|
|
|
|
<A NAME="lbAB"> </A>
|
|
<H2>NAME</H2>
|
|
|
|
nanosleep - high-resolution sleep
|
|
<A NAME="lbAC"> </A>
|
|
<H2>SYNOPSIS</H2>
|
|
|
|
<B>#include <<A HREF="file:///usr/include/time.h">time.h</A>></B>
|
|
|
|
<P>
|
|
|
|
<B>int nanosleep(const struct timespec *</B><I>req</I><B>, struct timespec *</B><I>rem</I><B>);</B>
|
|
|
|
<P>
|
|
|
|
|
|
Feature Test Macro Requirements for glibc (see
|
|
<B><A HREF="/cgi-bin/man/man2html?7+feature_test_macros">feature_test_macros</A></B>(7)):
|
|
|
|
|
|
<P>
|
|
|
|
<B>nanosleep</B>():
|
|
|
|
_POSIX_C_SOURCE >= 199309L
|
|
<A NAME="lbAD"> </A>
|
|
<H2>DESCRIPTION</H2>
|
|
|
|
<B>nanosleep</B>()
|
|
|
|
suspends the execution of the calling thread
|
|
until either at least the time specified in
|
|
<I>*req</I>
|
|
|
|
has elapsed, or the delivery of a signal
|
|
that triggers the invocation of a handler in the calling thread or
|
|
that terminates the process.
|
|
<P>
|
|
|
|
If the call is interrupted by a signal handler,
|
|
<B>nanosleep</B>()
|
|
|
|
returns -1, sets
|
|
<I>errno</I>
|
|
|
|
to
|
|
<B>EINTR</B>,
|
|
|
|
and writes the remaining time into the structure pointed to by
|
|
<I>rem</I>
|
|
|
|
unless
|
|
<I>rem</I>
|
|
|
|
is NULL.
|
|
The value of
|
|
<I>*rem</I>
|
|
|
|
can then be used to call
|
|
<B>nanosleep</B>()
|
|
|
|
again and complete the specified pause (but see NOTES).
|
|
<P>
|
|
|
|
The structure
|
|
<I>timespec</I>
|
|
|
|
is used to specify intervals of time with nanosecond precision.
|
|
It is defined as follows:
|
|
<P>
|
|
|
|
|
|
|
|
struct timespec {
|
|
<BR> time_t tv_sec; /* seconds */
|
|
<BR> long tv_nsec; /* nanoseconds */
|
|
};
|
|
|
|
|
|
<P>
|
|
|
|
The value of the nanoseconds field must be in the range 0 to 999999999.
|
|
<P>
|
|
|
|
Compared to
|
|
<B><A HREF="/cgi-bin/man/man2html?3+sleep">sleep</A></B>(3)
|
|
|
|
and
|
|
<B><A HREF="/cgi-bin/man/man2html?3+usleep">usleep</A></B>(3),
|
|
|
|
<B>nanosleep</B>()
|
|
|
|
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.
|
|
<A NAME="lbAE"> </A>
|
|
<H2>RETURN VALUE</H2>
|
|
|
|
On successfully sleeping for the requested interval,
|
|
<B>nanosleep</B>()
|
|
|
|
returns 0.
|
|
If the call is interrupted by a signal handler or encounters an error,
|
|
then it returns -1, with
|
|
<I>errno</I>
|
|
|
|
set to indicate the error.
|
|
<A NAME="lbAF"> </A>
|
|
<H2>ERRORS</H2>
|
|
|
|
<DL COMPACT>
|
|
<DT id="1"><B>EFAULT</B>
|
|
|
|
<DD>
|
|
Problem with copying information from user space.
|
|
<DT id="2"><B>EINTR</B>
|
|
|
|
<DD>
|
|
The pause has been interrupted by a signal that was
|
|
delivered to the thread (see
|
|
<B><A HREF="/cgi-bin/man/man2html?7+signal">signal</A></B>(7)).
|
|
|
|
The remaining sleep time has been written
|
|
into
|
|
<I>*rem</I>
|
|
|
|
so that the thread can easily call
|
|
<B>nanosleep</B>()
|
|
|
|
again and continue with the pause.
|
|
<DT id="3"><B>EINVAL</B>
|
|
|
|
<DD>
|
|
The value in the
|
|
<I>tv_nsec</I>
|
|
|
|
field was not in the range 0 to 999999999 or
|
|
<I>tv_sec</I>
|
|
|
|
was negative.
|
|
</DL>
|
|
<A NAME="lbAG"> </A>
|
|
<H2>CONFORMING TO</H2>
|
|
|
|
POSIX.1-2001, POSIX.1-2008.
|
|
<A NAME="lbAH"> </A>
|
|
<H2>NOTES</H2>
|
|
|
|
If the interval specified in
|
|
<I>req</I>
|
|
|
|
is not an exact multiple of the granularity underlying clock (see
|
|
<B><A HREF="/cgi-bin/man/man2html?7+time">time</A></B>(7)),
|
|
|
|
then the interval will be rounded up to the next multiple.
|
|
Furthermore, after the sleep completes, there may still be a delay before
|
|
the CPU becomes free to once again execute the calling thread.
|
|
<P>
|
|
|
|
The fact that
|
|
<B>nanosleep</B>()
|
|
|
|
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
|
|
<B><A HREF="/cgi-bin/man/man2html?2+clock_nanosleep">clock_nanosleep</A></B>(2)
|
|
|
|
with an absolute time value.
|
|
<P>
|
|
|
|
POSIX.1 specifies that
|
|
<B>nanosleep</B>()
|
|
|
|
should measure time against the
|
|
<B>CLOCK_REALTIME</B>
|
|
|
|
clock.
|
|
However, Linux measures the time using the
|
|
<B>CLOCK_MONOTONIC</B>
|
|
|
|
clock.
|
|
|
|
|
|
|
|
This probably does not matter, since the POSIX.1 specification for
|
|
<B><A HREF="/cgi-bin/man/man2html?2+clock_settime">clock_settime</A></B>(2)
|
|
|
|
says that discontinuous changes in
|
|
<B>CLOCK_REALTIME</B>
|
|
|
|
should not affect
|
|
<B>nanosleep</B>():
|
|
|
|
<DL COMPACT><DT id="4"><DD>
|
|
<P>
|
|
|
|
Setting the value of the
|
|
<B>CLOCK_REALTIME</B>
|
|
|
|
clock via
|
|
<B><A HREF="/cgi-bin/man/man2html?2+clock_settime">clock_settime</A></B>(2)
|
|
|
|
shall
|
|
have no effect on threads that are blocked waiting for a relative time
|
|
service based upon this clock, including the
|
|
<B>nanosleep</B>()
|
|
|
|
function; ...
|
|
Consequently, these time services shall expire when the requested relative
|
|
interval elapses, independently of the new or old value of the clock.
|
|
</DL>
|
|
|
|
<A NAME="lbAI"> </A>
|
|
<H3>Old behavior</H3>
|
|
|
|
In order to support applications requiring much more precise pauses
|
|
(e.g., in order to control some time-critical hardware),
|
|
<B>nanosleep</B>()
|
|
|
|
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
|
|
<B>SCHED_FIFO</B>
|
|
|
|
or
|
|
<B>SCHED_RR</B>.
|
|
|
|
This special extension was removed in kernel 2.5.39,
|
|
and is thus not available in Linux 2.6.0 and later kernels.
|
|
<A NAME="lbAJ"> </A>
|
|
<H2>BUGS</H2>
|
|
|
|
If a program that catches signals and uses
|
|
<B>nanosleep</B>()
|
|
|
|
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
|
|
<I>remain</I>
|
|
|
|
value mean that the
|
|
<I>remain</I>
|
|
|
|
value may steadily
|
|
<I>increase</I>
|
|
|
|
on successive restarts of the
|
|
<B>nanosleep</B>()
|
|
|
|
call.
|
|
To avoid such problems, use
|
|
<B><A HREF="/cgi-bin/man/man2html?2+clock_nanosleep">clock_nanosleep</A></B>(2)
|
|
|
|
with the
|
|
<B>TIMER_ABSTIME</B>
|
|
|
|
flag to sleep to an absolute deadline.
|
|
<P>
|
|
|
|
In Linux 2.4, if
|
|
<B>nanosleep</B>()
|
|
|
|
is stopped by a signal (e.g.,
|
|
<B>SIGTSTP</B>),
|
|
|
|
then the call fails with the error
|
|
<B>EINTR</B>
|
|
|
|
after the thread is resumed by a
|
|
<B>SIGCONT</B>
|
|
|
|
signal.
|
|
If the system call is subsequently restarted,
|
|
then the time that the thread spent in the stopped state is
|
|
<I>not</I>
|
|
|
|
counted against the sleep interval.
|
|
This problem is fixed in Linux 2.6.0 and later kernels.
|
|
<A NAME="lbAK"> </A>
|
|
<H2>SEE ALSO</H2>
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?2+clock_nanosleep">clock_nanosleep</A></B>(2),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?2+restart_syscall">restart_syscall</A></B>(2),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?2+sched_setscheduler">sched_setscheduler</A></B>(2),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?2+timer_create">timer_create</A></B>(2),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?3+sleep">sleep</A></B>(3),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?3+usleep">usleep</A></B>(3),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?7+time">time</A></B>(7)
|
|
|
|
<A NAME="lbAL"> </A>
|
|
<H2>COLOPHON</H2>
|
|
|
|
This page is part of release 5.05 of the Linux
|
|
<I>man-pages</I>
|
|
|
|
project.
|
|
A description of the project,
|
|
information about reporting bugs,
|
|
and the latest version of this page,
|
|
can be found at
|
|
<A HREF="https://www.kernel.org/doc/man-pages/.">https://www.kernel.org/doc/man-pages/.</A>
|
|
<P>
|
|
|
|
<HR>
|
|
<A NAME="index"> </A><H2>Index</H2>
|
|
<DL>
|
|
<DT id="5"><A HREF="#lbAB">NAME</A><DD>
|
|
<DT id="6"><A HREF="#lbAC">SYNOPSIS</A><DD>
|
|
<DT id="7"><A HREF="#lbAD">DESCRIPTION</A><DD>
|
|
<DT id="8"><A HREF="#lbAE">RETURN VALUE</A><DD>
|
|
<DT id="9"><A HREF="#lbAF">ERRORS</A><DD>
|
|
<DT id="10"><A HREF="#lbAG">CONFORMING TO</A><DD>
|
|
<DT id="11"><A HREF="#lbAH">NOTES</A><DD>
|
|
<DL>
|
|
<DT id="12"><A HREF="#lbAI">Old behavior</A><DD>
|
|
</DL>
|
|
<DT id="13"><A HREF="#lbAJ">BUGS</A><DD>
|
|
<DT id="14"><A HREF="#lbAK">SEE ALSO</A><DD>
|
|
<DT id="15"><A HREF="#lbAL">COLOPHON</A><DD>
|
|
</DL>
|
|
<HR>
|
|
This document was created by
|
|
<A HREF="/cgi-bin/man/man2html">man2html</A>,
|
|
using the manual pages.<BR>
|
|
Time: 00:05:33 GMT, March 31, 2021
|
|
</BODY>
|
|
</HTML>
|