1SLEEP(3P)                  POSIX Programmer's Manual                 SLEEP(3P)
2
3
4

PROLOG

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
11

NAME

13       sleep — suspend execution for an interval of time
14

SYNOPSIS

16       #include <unistd.h>
17
18       unsigned sleep(unsigned seconds);
19

DESCRIPTION

21       The sleep() function shall cause the calling  thread  to  be  suspended
22       from execution until either the number of realtime seconds specified by
23       the argument seconds has elapsed or a signal is delivered to the  call‐
24       ing thread and its action is to invoke a signal-catching function or to
25       terminate the process. The suspension time may be longer than requested
26       due to the scheduling of other activity by the system.
27
28       If  a SIGALRM signal is generated for the calling process during execu‐
29       tion of sleep() and if the SIGALRM signal is being ignored  or  blocked
30       from  delivery,  it  is  unspecified  whether  sleep() returns when the
31       SIGALRM signal is scheduled. If the signal is being blocked, it is also
32       unspecified  whether  it remains pending after sleep() returns or it is
33       discarded.
34
35       If a SIGALRM signal is generated for the calling process during  execu‐
36       tion  of sleep(), except as a result of a prior call to alarm(), and if
37       the SIGALRM signal is not being ignored or blocked from delivery, it is
38       unspecified  whether  that  signal  has  any  effect other than causing
39       sleep() to return.
40
41       If a  signal-catching  function  interrupts  sleep()  and  examines  or
42       changes  either  the  time  a SIGALRM is scheduled to be generated, the
43       action associated with the SIGALRM signal, or whether the SIGALRM  sig‐
44       nal is blocked from delivery, the results are unspecified.
45
46       If a signal-catching function interrupts sleep() and calls siglongjmp()
47       or longjmp() to restore an environment saved prior to the sleep() call,
48       the  action  associated with the SIGALRM signal and the time at which a
49       SIGALRM signal is scheduled to be generated  are  unspecified.   It  is
50       also unspecified whether the SIGALRM signal is blocked, unless the sig‐
51       nal mask of the process is restored as part of the environment.
52
53       Interactions between sleep() and setitimer() are unspecified.
54

RETURN VALUE

56       If sleep() returns because the requested time has  elapsed,  the  value
57       returned  shall  be  0. If sleep() returns due to delivery of a signal,
58       the return value shall be the ``unslept'' amount  (the  requested  time
59       minus the time actually slept) in seconds.
60

ERRORS

62       No errors are defined.
63
64       The following sections are informative.
65

EXAMPLES

67       None.
68

APPLICATION USAGE

70       None.
71

RATIONALE

73       There  are  two general approaches to the implementation of the sleep()
74       function. One is to use the alarm() function to schedule a SIGALRM sig‐
75       nal  and  then  suspend the calling thread waiting for that signal. The
76       other  is  to  implement  an  independent  facility.  This  volume   of
77       POSIX.1‐2008 permits either approach.
78
79       In  order to comply with the requirement that no primitive shall change
80       a process attribute unless  explicitly  described  by  this  volume  of
81       POSIX.1‐2008,  an implementation using SIGALRM must carefully take into
82       account any SIGALRM signal scheduled by  previous  alarm()  calls,  the
83       action  previously  established  for  SIGALRM,  and whether SIGALRM was
84       blocked. If a SIGALRM has been scheduled before the sleep() would ordi‐
85       narily  complete,  the  sleep()  must  be  shortened to that time and a
86       SIGALRM generated (possibly simulated by direct invocation of the  sig‐
87       nal-catching  function)  before  sleep() returns. If a SIGALRM has been
88       scheduled after the sleep()  would  ordinarily  complete,  it  must  be
89       rescheduled  for  the  same time before sleep() returns. The action and
90       blocking for SIGALRM must be saved and restored.
91
92       Historical implementations often implement  the  SIGALRM-based  version
93       using  alarm()  and pause().  One such implementation is prone to infi‐
94       nite hangups, as described in  pause().   Another  such  implementation
95       uses the C-language setjmp() and longjmp() functions to avoid that win‐
96       dow. That implementation  introduces  a  different  problem:  when  the
97       SIGALRM  signal  interrupts a signal-catching function installed by the
98       user to catch a different signal, the  longjmp()  aborts  that  signal-
99       catching  function.  An implementation based on sigprocmask(), alarm(),
100       and sigsuspend() can avoid these problems.
101
102       Despite all  reasonable  care,  there  are  several  very  subtle,  but
103       detectable and unavoidable, differences between the two types of imple‐
104       mentations.  These  are  the  cases  mentioned  in   this   volume   of
105       POSIX.1‐2008 where some other activity relating to SIGALRM takes place,
106       and the results are stated to be unspecified. All of  these  cases  are
107       sufficiently unusual as not to be of concern to most applications.
108
109       See also the discussion of the term realtime in alarm().
110
111       Since  sleep()  can  be implemented using alarm(), the discussion about
112       alarms occurring early under alarm() applies to sleep() as well.
113
114       Application developers should note that the type of the  argument  sec‐
115       onds  and  the  return value of sleep() is unsigned.  That means that a
116       Strictly Conforming POSIX System Interfaces Application cannot  pass  a
117       value  greater  than the minimum guaranteed value for {UINT_MAX}, which
118       the ISO C standard sets as 65535, and any application passing a  larger
119       value  is restricting its portability. A different type was considered,
120       but historical implementations, including those with a 16-bit int type,
121       consistently use either unsigned or int.
122
123       Scheduling  delays  may  cause  the  process to return from the sleep()
124       function significantly after the requested time.  In  such  cases,  the
125       return  value  should be set to zero, since the formula (requested time
126       minus the time actually spent) yields a  negative  number  and  sleep()
127       returns an unsigned.
128

FUTURE DIRECTIONS

130       None.
131

SEE ALSO

133       alarm(), getitimer(), nanosleep(), pause(), sigaction(), sigsetjmp()
134
135       The Base Definitions volume of POSIX.1‐2008, <unistd.h>
136
138       Portions  of  this text are reprinted and reproduced in electronic form
139       from IEEE Std 1003.1, 2013 Edition, Standard for Information Technology
140       --  Portable  Operating  System  Interface (POSIX), The Open Group Base
141       Specifications Issue 7, Copyright (C) 2013 by the Institute of Electri‐
142       cal  and  Electronics  Engineers,  Inc  and  The  Open Group.  (This is
143       POSIX.1-2008 with the 2013 Technical Corrigendum  1  applied.)  In  the
144       event of any discrepancy between this version and the original IEEE and
145       The Open Group Standard, the original IEEE and The Open Group  Standard
146       is  the  referee document. The original Standard can be obtained online
147       at http://www.unix.org/online.html .
148
149       Any typographical or formatting errors that appear  in  this  page  are
150       most likely to have been introduced during the conversion of the source
151       files to man page format. To report such errors,  see  https://www.ker
152       nel.org/doc/man-pages/reporting_bugs.html .
153
154
155
156IEEE/The Open Group                  2013                            SLEEP(3P)
Impressum