Files
oldlinux-files/Ref-docs/POSIX/susv3/functions/sleep.html
2024-02-19 00:21:47 -05:00

188 lines
9.8 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>sleep</title>
</head>
<body bgcolor="white">
<script type="text/javascript" language="JavaScript" src="../jscript/codes.js">
</script>
<basefont size="3"> <a name="sleep"></a> <a name="tag_03_705"></a><!-- sleep -->
<!--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_705_01"></a>NAME</h4>
<blockquote>sleep - suspend execution for an interval of time</blockquote>
<h4><a name="tag_03_705_02"></a>SYNOPSIS</h4>
<blockquote class="synopsis">
<p><code><tt>#include &lt;<a href="../basedefs/unistd.h.html">unistd.h</a>&gt;<br>
<br>
unsigned sleep(unsigned</tt> <i>seconds</i><tt>);<br>
</tt></code></p>
</blockquote>
<h4><a name="tag_03_705_03"></a>DESCRIPTION</h4>
<blockquote>
<p>The <i>sleep</i>() function shall cause the calling thread to be suspended from execution until either the number of realtime
seconds specified by the argument <i>seconds</i> has elapsed or a signal is delivered to the calling thread and its action is to
invoke a signal-catching function or to terminate the process. The suspension time may be longer than requested due to the
scheduling of other activity by the system.</p>
<p>If a SIGALRM signal is generated for the calling process during execution of <i>sleep</i>() and if the SIGALRM signal is being
ignored or blocked from delivery, it is unspecified whether <i>sleep</i>() returns when the SIGALRM signal is scheduled. If the
signal is being blocked, it is also unspecified whether it remains pending after <i>sleep</i>() returns or it is discarded.</p>
<p>If a SIGALRM signal is generated for the calling process during execution of <i>sleep</i>(), except as a result of a prior call
to <a href="../functions/alarm.html"><i>alarm</i>()</a>, and if the SIGALRM signal is not being ignored or blocked from delivery,
it is unspecified whether that signal has any effect other than causing <i>sleep</i>() to return.</p>
<p>If a signal-catching function interrupts <i>sleep</i>() and examines or changes either the time a SIGALRM is scheduled to be
generated, the action associated with the SIGALRM signal, or whether the SIGALRM signal is blocked from delivery, the results are
unspecified.</p>
<p>If a signal-catching function interrupts <i>sleep</i>() and calls <a href="../functions/siglongjmp.html"><i>siglongjmp</i>()</a>
or <a href="../functions/longjmp.html"><i>longjmp</i>()</a> to restore an environment saved prior to the <i>sleep</i>() call, the
action associated with the SIGALRM signal and the time at which a SIGALRM signal is scheduled to be generated are unspecified. It
is also unspecified whether the SIGALRM signal is blocked, unless the process' signal mask is restored as part of the
environment.</p>
<p><sup>[<a href="javascript:open_code('XSI')">XSI</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]" border="0">
Interactions between <i>sleep</i>() and any of <a href="../functions/setitimer.html"><i>setitimer</i>()</a>, <a href=
"../functions/ualarm.html"><i>ualarm</i>()</a>, or <a href="../functions/usleep.html"><i>usleep</i>()</a> are unspecified. <img
src="../images/opt-end.gif" alt="[Option End]" border="0"></p>
</blockquote>
<h4><a name="tag_03_705_04"></a>RETURN VALUE</h4>
<blockquote>
<p>If <i>sleep</i>() returns because the requested time has elapsed, the value returned shall be 0. If <i>sleep</i>() returns due
to delivery of a signal, the return value shall be the &quot;unslept&quot; amount (the requested time minus the time actually slept) in
seconds.</p>
</blockquote>
<h4><a name="tag_03_705_05"></a>ERRORS</h4>
<blockquote>
<p>No errors are defined.</p>
</blockquote>
<hr>
<div class="box"><em>The following sections are informative.</em></div>
<h4><a name="tag_03_705_06"></a>EXAMPLES</h4>
<blockquote>
<p>None.</p>
</blockquote>
<h4><a name="tag_03_705_07"></a>APPLICATION USAGE</h4>
<blockquote>
<p>None.</p>
</blockquote>
<h4><a name="tag_03_705_08"></a>RATIONALE</h4>
<blockquote>
<p>There are two general approaches to the implementation of the <i>sleep</i>() function. One is to use the <a href=
"../functions/alarm.html"><i>alarm</i>()</a> function to schedule a SIGALRM signal and then suspend the process waiting for that
signal. The other is to implement an independent facility. This volume of IEEE&nbsp;Std&nbsp;1003.1-2001 permits either
approach.</p>
<p>In order to comply with the requirement that no primitive shall change a process attribute unless explicitly described by this
volume of IEEE&nbsp;Std&nbsp;1003.1-2001, an implementation using SIGALRM must carefully take into account any SIGALRM signal
scheduled by previous <a href="../functions/alarm.html"><i>alarm</i>()</a> calls, the action previously established for SIGALRM,
and whether SIGALRM was blocked. If a SIGALRM has been scheduled before the <i>sleep</i>() would ordinarily complete, the
<i>sleep</i>() must be shortened to that time and a SIGALRM generated (possibly simulated by direct invocation of the
signal-catching function) before <i>sleep</i>() returns. If a SIGALRM has been scheduled after the <i>sleep</i>() would ordinarily
complete, it must be rescheduled for the same time before <i>sleep</i>() returns. The action and blocking for SIGALRM must be saved
and restored.</p>
<p>Historical implementations often implement the SIGALRM-based version using <a href="../functions/alarm.html"><i>alarm</i>()</a>
and <a href="../functions/pause.html"><i>pause</i>()</a>. One such implementation is prone to infinite hangups, as described in <a
href="pause.html"><i>pause</i>()</a> . Another such implementation uses the C-language <a href=
"../functions/setjmp.html"><i>setjmp</i>()</a> and <a href="../functions/longjmp.html"><i>longjmp</i>()</a> functions to avoid that
window. That implementation introduces a different problem: when the SIGALRM signal interrupts a signal-catching function installed
by the user to catch a different signal, the <a href="../functions/longjmp.html"><i>longjmp</i>()</a> aborts that signal-catching
function. An implementation based on <a href="../functions/sigprocmask.html"><i>sigprocmask</i>()</a>, <a href=
"../functions/alarm.html"><i>alarm</i>()</a>, and <a href="../functions/sigsuspend.html"><i>sigsuspend</i>()</a> can avoid these
problems.</p>
<p>Despite all reasonable care, there are several very subtle, but detectable and unavoidable, differences between the two types of
implementations. These are the cases mentioned in this volume of IEEE&nbsp;Std&nbsp;1003.1-2001 where some other activity relating
to SIGALRM takes place, and the results are stated to be unspecified. All of these cases are sufficiently unusual as not to be of
concern to most applications.</p>
<p>See also the discussion of the term <i>realtime</i> in <a href="alarm.html"><i>alarm</i>()</a> .</p>
<p>Since <i>sleep</i>() can be implemented using <a href="../functions/alarm.html"><i>alarm</i>()</a>, the discussion about alarms
occurring early under <a href="../functions/alarm.html"><i>alarm</i>()</a> applies to <i>sleep</i>() as well.</p>
<p>Application writers should note that the type of the argument <i>seconds</i> and the return value of <i>sleep</i>() is
<b>unsigned</b>. That means that a Strictly Conforming POSIX System Interfaces Application cannot pass a value greater than the
minimum guaranteed value for {UINT_MAX}, which the ISO&nbsp;C standard sets as 65535, and any application passing a larger value is
restricting its portability. A different type was considered, but historical implementations, including those with a 16-bit
<b>int</b> type, consistently use either <b>unsigned</b> or <b>int</b>.</p>
<p>Scheduling delays may cause the process to return from the <i>sleep</i>() function significantly after the requested time. In
such cases, the return value should be set to zero, since the formula (requested time minus the time actually spent) yields a
negative number and <i>sleep</i>() returns an <b>unsigned</b>.</p>
</blockquote>
<h4><a name="tag_03_705_09"></a>FUTURE DIRECTIONS</h4>
<blockquote>
<p>None.</p>
</blockquote>
<h4><a name="tag_03_705_10"></a>SEE ALSO</h4>
<blockquote>
<p><a href="alarm.html"><i>alarm</i>()</a> , <a href="getitimer.html"><i>getitimer</i>()</a> , <a href=
"nanosleep.html"><i>nanosleep</i>()</a> , <a href="pause.html"><i>pause</i>()</a> , <a href="sigaction.html"><i>sigaction</i>()</a>
, <a href="sigsetjmp.html"><i>sigsetjmp</i>()</a> , <a href="ualarm.html"><i>ualarm</i>()</a> , <a href=
"usleep.html"><i>usleep</i>()</a> , the Base Definitions volume of IEEE&nbsp;Std&nbsp;1003.1-2001, <a href=
"../basedefs/unistd.h.html"><i>&lt;unistd.h&gt;</i></a></p>
</blockquote>
<h4><a name="tag_03_705_11"></a>CHANGE HISTORY</h4>
<blockquote>
<p>First released in Issue 1. Derived from Issue 1 of the SVID.</p>
</blockquote>
<h4><a name="tag_03_705_12"></a>Issue 5</h4>
<blockquote>
<p>The DESCRIPTION is updated for alignment with the POSIX Threads Extension.</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>