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

NAME

6       sleep - suspend execution for an interval of time
7

SYNOPSIS

9       #include <unistd.h>
10
11       unsigned sleep(unsigned seconds);
12
13

DESCRIPTION

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

RETURN VALUE

51       If sleep() returns because the requested time has  elapsed,  the  value
52       returned  shall  be  0. If sleep() returns due to delivery of a signal,
53       the return value shall be the  "unslept"  amount  (the  requested  time
54       minus the time actually slept) in seconds.
55

ERRORS

57       No errors are defined.
58
59       The following sections are informative.
60

EXAMPLES

62       None.
63

APPLICATION USAGE

65       None.
66

RATIONALE

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

FUTURE DIRECTIONS

126       None.
127

SEE ALSO

129       alarm()  ,  getitimer()  ,  nanosleep()  ,  pause()  ,  sigaction()   ,
130       sigsetjmp()  ,  ualarm()  ,  usleep()  , the Base Definitions volume of
131       IEEE Std 1003.1-2001, <unistd.h>
132
134       Portions of this text are reprinted and reproduced in  electronic  form
135       from IEEE Std 1003.1, 2003 Edition, Standard for Information Technology
136       -- Portable Operating System Interface (POSIX),  The  Open  Group  Base
137       Specifications  Issue  6,  Copyright  (C) 2001-2003 by the Institute of
138       Electrical and Electronics Engineers, Inc and The Open  Group.  In  the
139       event of any discrepancy between this version and the original IEEE and
140       The Open Group Standard, the original IEEE and The Open Group  Standard
141       is  the  referee document. The original Standard can be obtained online
142       at http://www.opengroup.org/unix/online.html .
143
144
145
146IEEE/The Open Group                  2003                             SLEEP(P)
Impressum