man-pages/man2/clock_nanosleep.2.html
2021-03-31 01:06:50 +01:00

394 lines
8.7 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML><HEAD><TITLE>Man page of CLOCK_NANOSLEEP</TITLE>
</HEAD><BODY>
<H1>CLOCK_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">&nbsp;</A>
<H2>NAME</H2>
clock_nanosleep - high-resolution sleep with specifiable clock
<A NAME="lbAC">&nbsp;</A>
<H2>SYNOPSIS</H2>
<B>#include &lt;<A HREF="file:///usr/include/time.h">time.h</A>&gt;</B>
<PRE>
<B>int clock_nanosleep(clockid_t </B><I>clock_id</I><B>, int </B><I>flags</I><B>,</B>
<B> const struct timespec *</B><I>request</I><B>,</B>
<B> struct timespec *</B><I>remain</I><B>);</B>
</PRE>
<P>
Link with <I>-lrt</I> (only for glibc versions before 2.17).
<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>clock_nanosleep</B>():
<DL COMPACT><DT id="1"><DD>
_POSIX_C_SOURCE&nbsp;&gt;=&nbsp;200112L
</DL>
<A NAME="lbAD">&nbsp;</A>
<H2>DESCRIPTION</H2>
Like
<B><A HREF="/cgi-bin/man/man2html?2+nanosleep">nanosleep</A></B>(2),
<B>clock_nanosleep</B>()
allows the calling thread to sleep for an interval specified
with nanosecond precision.
It differs in allowing the caller to select the clock against
which the sleep interval is to be measured,
and in allowing the sleep interval to be specified as
either an absolute or a relative value.
<P>
The time values passed to and returned by this call are specified using
<I>timespec</I>
structures, defined as follows:
<P>
struct timespec {
<BR>&nbsp;&nbsp;&nbsp;&nbsp;time_t&nbsp;tv_sec;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;seconds&nbsp;*/
<BR>&nbsp;&nbsp;&nbsp;&nbsp;long&nbsp;&nbsp;&nbsp;tv_nsec;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;nanoseconds&nbsp;[0&nbsp;..&nbsp;999999999]&nbsp;*/
};
<P>
The
<I>clock_id</I>
argument specifies the clock against which the sleep interval
is to be measured.
This argument can have one of the following values:
<DL COMPACT>
<DT id="2"><B>CLOCK_REALTIME</B>
<DD>
A settable system-wide real-time clock.
<DT id="3"><B>CLOCK_MONOTONIC</B>
<DD>
A nonsettable, monotonically increasing clock that measures time
since some unspecified point in the past that does not change after
system startup.
<DT id="4"><B>CLOCK_PROCESS_CPUTIME_ID</B>
<DD>
A settable per-process clock that measures CPU time consumed
by all threads in the process.
</DL>
<P>
See
<B><A HREF="/cgi-bin/man/man2html?2+clock_getres">clock_getres</A></B>(2)
for further details on these clocks.
In addition, the CPU clock IDs returned by
<B><A HREF="/cgi-bin/man/man2html?3+clock_getcpuclockid">clock_getcpuclockid</A></B>(3)
and
<B><A HREF="/cgi-bin/man/man2html?3+pthread_getcpuclockid">pthread_getcpuclockid</A></B>(3)
can also be passed in
<I>clock_id</I>.
<P>
If
<I>flags</I>
is 0, then the value specified in
<I>request</I>
is interpreted as an interval relative to the current
value of the clock specified by
<I>clock_id</I>.
<P>
If
<I>flags</I>
is
<B>TIMER_ABSTIME</B>,
then
<I>request</I>
is interpreted as an absolute time as measured by the clock,
<I>clock_id</I>.
If
<I>request</I>
is less than or equal to the current value of the clock,
then
<B>clock_nanosleep</B>()
returns immediately without suspending the calling thread.
<P>
<B>clock_nanosleep</B>()
suspends the execution of the calling thread
until either at least the time specified by
<I>request</I>
has elapsed,
or a signal is delivered that causes a signal handler to be called or
that terminates the process.
<P>
If the call is interrupted by a signal handler,
<B>clock_nanosleep</B>()
fails with the error
<B>EINTR</B>.
In addition, if
<I>remain</I>
is not NULL, and
<I>flags</I>
was not
<B>TIMER_ABSTIME</B>,
it returns the remaining unslept time in
<I>remain</I>.
This value can then be used to call
<B>clock_nanosleep</B>()
again and complete a (relative) sleep.
<A NAME="lbAE">&nbsp;</A>
<H2>RETURN VALUE</H2>
On successfully sleeping for the requested interval,
<B>clock_nanosleep</B>()
returns 0.
If the call is interrupted by a signal handler or encounters an error,
then it returns one of the positive error number listed in ERRORS.
<A NAME="lbAF">&nbsp;</A>
<H2>ERRORS</H2>
<DL COMPACT>
<DT id="5"><B>EFAULT</B>
<DD>
<I>request</I>
or
<I>remain</I>
specified an invalid address.
<DT id="6"><B>EINTR</B>
<DD>
The sleep was interrupted by a signal handler; see
<B><A HREF="/cgi-bin/man/man2html?7+signal">signal</A></B>(7).
<DT id="7"><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.
<DT id="8"><B>EINVAL</B>
<DD>
<I>clock_id</I>
was invalid.
(<B>CLOCK_THREAD_CPUTIME_ID</B>
is not a permitted value for
<I>clock_id</I>.)
</DL>
<A NAME="lbAG">&nbsp;</A>
<H2>VERSIONS</H2>
The
<B>clock_nanosleep</B>()
system call first appeared in Linux 2.6.
Support is available in glibc since version 2.1.
<A NAME="lbAH">&nbsp;</A>
<H2>CONFORMING TO</H2>
POSIX.1-2001, POSIX.1-2008.
<A NAME="lbAI">&nbsp;</A>
<H2>NOTES</H2>
If the interval specified in
<I>request</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>
Using an absolute timer is useful for preventing
timer drift problems of the type described in
<B><A HREF="/cgi-bin/man/man2html?2+nanosleep">nanosleep</A></B>(2).
(Such problems are exacerbated in programs that try to restart
a relative sleep that is repeatedly interrupted by signals.)
To perform a relative sleep that avoids these problems, call
<B><A HREF="/cgi-bin/man/man2html?2+clock_gettime">clock_gettime</A></B>(2)
for the desired clock,
add the desired interval to the returned time value,
and then call
<B>clock_nanosleep</B>()
with the
<B>TIMER_ABSTIME</B>
flag.
<P>
<B>clock_nanosleep</B>()
is never restarted after being interrupted by a signal handler,
regardless of the use of the
<B><A HREF="/cgi-bin/man/man2html?2+sigaction">sigaction</A></B>(2)
<B>SA_RESTART</B>
flag.
<P>
The
<I>remain</I>
argument is unused, and unnecessary, when
<I>flags</I>
is
<B>TIMER_ABSTIME</B>.
(An absolute sleep can be restarted using the same
<I>request</I>
argument.)
<P>
POSIX.1 specifies that
<B>clock_nanosleep</B>()
has no effect on signals dispositions or the signal mask.
<P>
POSIX.1 specifies that after changing 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),
the new clock value shall be used to determine the time
at which a thread blocked on an absolute
<B>clock_nanosleep</B>()
will wake up;
if the new clock value falls past the end of the sleep interval, then the
<B>clock_nanosleep</B>()
call will return immediately.
<P>
POSIX.1 specifies that
changing 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 a thread that is blocked on a relative
<B>clock_nanosleep</B>().
<A NAME="lbAJ">&nbsp;</A>
<H2>SEE ALSO</H2>
<B><A HREF="/cgi-bin/man/man2html?2+clock_getres">clock_getres</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?2+nanosleep">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+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="lbAK">&nbsp;</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">&nbsp;</A><H2>Index</H2>
<DL>
<DT id="9"><A HREF="#lbAB">NAME</A><DD>
<DT id="10"><A HREF="#lbAC">SYNOPSIS</A><DD>
<DT id="11"><A HREF="#lbAD">DESCRIPTION</A><DD>
<DT id="12"><A HREF="#lbAE">RETURN VALUE</A><DD>
<DT id="13"><A HREF="#lbAF">ERRORS</A><DD>
<DT id="14"><A HREF="#lbAG">VERSIONS</A><DD>
<DT id="15"><A HREF="#lbAH">CONFORMING TO</A><DD>
<DT id="16"><A HREF="#lbAI">NOTES</A><DD>
<DT id="17"><A HREF="#lbAJ">SEE ALSO</A><DD>
<DT id="18"><A HREF="#lbAK">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:32 GMT, March 31, 2021
</BODY>
</HTML>