Files
oldlinux-files/study/Ref-docs/POSIX/susv3/functions/clock_nanosleep.html
2024-02-19 00:25:23 -05:00

198 lines
11 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta name="generator" content="HTML Tidy, see www.w3.org">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link type="text/css" rel="stylesheet" href="style.css"><!-- Generated by The Open Group's rhtm tool v1.2.1 -->
<!-- Copyright (c) 2001 The Open Group, All Rights Reserved -->
<title>clock_nanosleep</title>
</head>
<body bgcolor="white">
<script type="text/javascript" language="JavaScript" src="../jscript/codes.js">
</script>
<basefont size="3"> <a name="clock_nanosleep"></a> <a name="tag_03_77"></a><!-- clock_nanosleep -->
<!--header start-->
<center><font size="2">The Open Group Base Specifications Issue 6<br>
IEEE Std 1003.1-2001<br>
Copyright &copy; 2001 The IEEE and The Open Group, All Rights reserved.</font></center>
<!--header end-->
<hr size="2" noshade>
<h4><a name="tag_03_77_01"></a>NAME</h4>
<blockquote>clock_nanosleep - high resolution sleep with specifiable clock (<b>ADVANCED REALTIME</b>)</blockquote>
<h4><a name="tag_03_77_02"></a>SYNOPSIS</h4>
<blockquote class="synopsis">
<div class="box"><code><tt><sup>[<a href="javascript:open_code('CS')">CS</a>]</sup> <img src="../images/opt-start.gif" alt=
"[Option Start]" border="0"> #include &lt;<a href="../basedefs/time.h.html">time.h</a>&gt;<br>
<br>
int clock_nanosleep(clockid_t</tt> <i>clock_id</i><tt>, int</tt> <i>flags</i><tt>,<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; const struct timespec *</tt><i>rqtp</i><tt>, struct timespec *</tt><i>rmtp</i><tt>); <img
src="../images/opt-end.gif" alt="[Option End]" border="0"></tt></code></div>
<tt><br>
</tt></blockquote>
<h4><a name="tag_03_77_03"></a>DESCRIPTION</h4>
<blockquote>
<p>If the flag TIMER_ABSTIME is not set in the <i>flags</i> argument, the <i>clock_nanosleep</i>() function shall cause the current
thread to be suspended from execution until either the time interval specified by the <i>rqtp</i> argument has elapsed, or a signal
is delivered to the calling thread and its action is to invoke a signal-catching function, or the process is terminated. The clock
used to measure the time shall be the clock specified by <i>clock_id</i>.</p>
<p>If the flag TIMER_ABSTIME is set in the <i>flags</i> argument, the <i>clock_nanosleep</i>() function shall cause the current
thread to be suspended from execution until either the time value of the clock specified by <i>clock_id</i> reaches the absolute
time specified by the <i>rqtp</i> argument, or a signal is delivered to the calling thread and its action is to invoke a
signal-catching function, or the process is terminated. If, at the time of the call, the time value specified by <i>rqtp</i> is
less than or equal to the time value of the specified clock, then <i>clock_nanosleep</i>() shall return immediately and the calling
process shall not be suspended.</p>
<p>The suspension time caused by this function may be longer than requested because the argument value is rounded up to an integer
multiple of the sleep resolution, or because of the scheduling of other activity by the system. But, except for the case of being
interrupted by a signal, the suspension time for the relative <i>clock_nanosleep</i>() function (that is, with the TIMER_ABSTIME
flag not set) shall not be less than the time interval specified by <i>rqtp</i>, as measured by the corresponding clock. The
suspension for the absolute <i>clock_nanosleep</i>() function (that is, with the TIMER_ABSTIME flag set) shall be in effect at
least until the value of the corresponding clock reaches the absolute time specified by <i>rqtp</i>, except for the case of being
interrupted by a signal.</p>
<p>The use of the <i>clock_nanosleep</i>() function shall have no effect on the action or blockage of any signal.</p>
<p>The <i>clock_nanosleep</i>() function shall fail if the <i>clock_id</i> argument refers to the CPU-time clock of the calling
thread. It is unspecified whether <i>clock_id</i> values of other CPU-time clocks are allowed.</p>
</blockquote>
<h4><a name="tag_03_77_04"></a>RETURN VALUE</h4>
<blockquote>
<p>If the <i>clock_nanosleep</i>() function returns because the requested time has elapsed, its return value shall be zero.</p>
<p>If the <i>clock_nanosleep</i>() function returns because it has been interrupted by a signal, it shall return the corresponding
error value. For the relative <i>clock_nanosleep</i>() function, if the <i>rmtp</i> argument is non-NULL, the <b>timespec</b>
structure referenced by it shall be updated to contain the amount of time remaining in the interval (the requested time minus the
time actually slept). If the <i>rmtp</i> argument is NULL, the remaining time is not returned. The absolute
<i>clock_nanosleep</i>() function has no effect on the structure referenced by <i>rmtp</i>.</p>
<p>If <i>clock_nanosleep</i>() fails, it shall return the corresponding error value.</p>
</blockquote>
<h4><a name="tag_03_77_05"></a>ERRORS</h4>
<blockquote>
<p>The <i>clock_nanosleep</i>() function shall fail if:</p>
<dl compact>
<dt>[EINTR]</dt>
<dd>The <i>clock_nanosleep</i>() function was interrupted by a signal.</dd>
<dt>[EINVAL]</dt>
<dd>The <i>rqtp</i> argument specified a nanosecond value less than zero or greater than or equal to 1000 million; or the
TIMER_ABSTIME flag was specified in flags and the <i>rqtp</i> argument is outside the range for the clock specified by
<i>clock_id</i>; or the <i>clock_id</i> argument does not specify a known clock, or specifies the CPU-time clock of the calling
thread.</dd>
<dt>[ENOTSUP]</dt>
<dd>The <i>clock_id</i> argument specifies a clock for which <i>clock_nanosleep</i>() is not supported, such as a CPU-time
clock.</dd>
</dl>
</blockquote>
<hr>
<div class="box"><em>The following sections are informative.</em></div>
<h4><a name="tag_03_77_06"></a>EXAMPLES</h4>
<blockquote>
<p>None.</p>
</blockquote>
<h4><a name="tag_03_77_07"></a>APPLICATION USAGE</h4>
<blockquote>
<p>Calling <i>clock_nanosleep</i>() with the value TIMER_ABSTIME not set in the <i>flags</i> argument and with a <i>clock_id</i> of
CLOCK_REALTIME is equivalent to calling <a href="../functions/nanosleep.html"><i>nanosleep</i>()</a> with the same <i>rqtp</i> and
<i>rmtp</i> arguments.</p>
</blockquote>
<h4><a name="tag_03_77_08"></a>RATIONALE</h4>
<blockquote>
<p>The <a href="../functions/nanosleep.html"><i>nanosleep</i>()</a> function specifies that the system-wide clock CLOCK_REALTIME is
used to measure the elapsed time for this time service. However, with the introduction of the monotonic clock CLOCK_MONOTONIC a new
relative sleep function is needed to allow an application to take advantage of the special characteristics of this clock.</p>
<p>There are many applications in which a process needs to be suspended and then activated multiple times in a periodic way; for
example, to poll the status of a non-interrupting device or to refresh a display device. For these cases, it is known that precise
periodic activation cannot be achieved with a relative <a href="../functions/sleep.html"><i>sleep</i>()</a> or <a href=
"../functions/nanosleep.html"><i>nanosleep</i>()</a> function call. Suppose, for example, a periodic process that is activated at
time <i>T</i>0, executes for a while, and then wants to suspend itself until time <i>T</i>0+ <i>T</i>, the period being <i>T</i>.
If this process wants to use the <a href="../functions/nanosleep.html"><i>nanosleep</i>()</a> function, it must first call <a href=
"../functions/clock_gettime.html"><i>clock_gettime</i>()</a> to get the current time, then calculate the difference between the
current time and <i>T</i>0+ <i>T</i> and, finally, call <a href="../functions/nanosleep.html"><i>nanosleep</i>()</a> using the
computed interval. However, the process could be preempted by a different process between the two function calls, and in this case
the interval computed would be wrong; the process would wake up later than desired. This problem would not occur with the absolute
<i>clock_nanosleep</i>() function, since only one function call would be necessary to suspend the process until the desired time.
In other cases, however, a relative sleep is needed, and that is why both functionalities are required.</p>
<p>Although it is possible to implement periodic processes using the timers interface, this implementation would require the use of
signals, and the reservation of some signal numbers. In this regard, the reasons for including an absolute version of the
<i>clock_nanosleep</i>() function in IEEE&nbsp;Std&nbsp;1003.1-2001 are the same as for the inclusion of the relative <a href=
"../functions/nanosleep.html"><i>nanosleep</i>()</a>.</p>
<p>It is also possible to implement precise periodic processes using <a href=
"../functions/pthread_cond_timedwait.html"><i>pthread_cond_timedwait</i>()</a>, in which an absolute timeout is specified that
takes effect if the condition variable involved is never signaled. However, the use of this interface is unnatural, and involves
performing other operations on mutexes and condition variables that imply an unnecessary overhead. Furthermore, <a href=
"../functions/pthread_cond_timedwait.html"><i>pthread_cond_timedwait</i>()</a> is not available in implementations that do not
support threads.</p>
<p>Although the interface of the relative and absolute versions of the new high resolution sleep service is the same
<i>clock_nanosleep</i>() function, the <i>rmtp</i> argument is only used in the relative sleep. This argument is needed in the
relative <i>clock_nanosleep</i>() function to reissue the function call if it is interrupted by a signal, but it is not needed in
the absolute <i>clock_nanosleep</i>() function call; if the call is interrupted by a signal, the absolute <i>clock_nanosleep</i>()
function can be invoked again with the same <i>rqtp</i> argument used in the interrupted call.</p>
</blockquote>
<h4><a name="tag_03_77_09"></a>FUTURE DIRECTIONS</h4>
<blockquote>
<p>None.</p>
</blockquote>
<h4><a name="tag_03_77_10"></a>SEE ALSO</h4>
<blockquote>
<p><a href="clock_getres.html"><i>clock_getres</i>()</a> , <a href="nanosleep.html"><i>nanosleep</i>()</a> , <a href=
"pthread_cond_timedwait.html"><i>pthread_cond_timedwait</i>()</a> , <a href="sleep.html"><i>sleep</i>()</a> , the Base Definitions
volume of IEEE&nbsp;Std&nbsp;1003.1-2001, <a href="../basedefs/time.h.html"><i>&lt;time.h&gt;</i></a></p>
</blockquote>
<h4><a name="tag_03_77_11"></a>CHANGE HISTORY</h4>
<blockquote>
<p>First released in Issue 6. Derived from IEEE&nbsp;Std&nbsp;1003.1j-2000.</p>
</blockquote>
<div class="box"><em>End of informative text.</em></div>
<hr>
<hr size="2" noshade>
<center><font size="2"><!--footer start-->
UNIX &reg; is a registered Trademark of The Open Group.<br>
POSIX &reg; is a registered Trademark of The IEEE.<br>
[ <a href="../mindex.html">Main Index</a> | <a href="../basedefs/contents.html">XBD</a> | <a href=
"../utilities/contents.html">XCU</a> | <a href="../functions/contents.html">XSH</a> | <a href="../xrat/contents.html">XRAT</a>
]</font></center>
<!--footer end-->
<hr size="2" noshade>
</body>
</html>