1SIGTIMEDWAIT(3P)           POSIX Programmer's Manual          SIGTIMEDWAIT(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       sigtimedwait, sigwaitinfo — wait for queued signals
14

SYNOPSIS

16       #include <signal.h>
17
18       int sigtimedwait(const sigset_t *restrict set,
19           siginfo_t *restrict info,
20           const struct timespec *restrict timeout);
21       int sigwaitinfo(const sigset_t *restrict set,
22           siginfo_t *restrict info);
23

DESCRIPTION

25       The sigtimedwait() function shall be equivalent to sigwaitinfo() except
26       that  if  none  of  the signals specified by set are pending, sigtimed‐
27       wait() shall wait for the  time  interval  specified  in  the  timespec
28       structure  referenced by timeout.  If the timespec structure pointed to
29       by timeout is zero-valued and if none of the signals specified  by  set
30       are  pending,  then  sigtimedwait()  shall  return  immediately with an
31       error. If timeout is the null pointer, the behavior is unspecified.  If
32       the  Monotonic  Clock  option  is  supported, the CLOCK_MONOTONIC clock
33       shall be used to measure the time interval  specified  by  the  timeout
34       argument.
35
36       The  sigwaitinfo()  function  selects  the  pending signal from the set
37       specified by set.  Should any of multiple pending signals in the  range
38       SIGRTMIN  to SIGRTMAX be selected, it shall be the lowest numbered one.
39       The selection order  between  realtime  and  non-realtime  signals,  or
40       between  multiple  pending  non-realtime signals, is unspecified. If no
41       signal in set is pending at the time of the call,  the  calling  thread
42       shall  be  suspended until one or more signals in set become pending or
43       until it is interrupted by an unblocked, caught signal.
44
45       The sigwaitinfo() function shall be equivalent to the  sigwait()  func‐
46       tion  if  the  info argument is NULL. If the info argument is non-NULL,
47       the sigwaitinfo() function shall be  equivalent  to  sigwait(),  except
48       that the selected signal number shall be stored in the si_signo member,
49       and the cause of the signal shall be stored in the si_code  member.  If
50       any value is queued to the selected signal, the first such queued value
51       shall be dequeued and, if the info  argument  is  non-NULL,  the  value
52       shall  be  stored  in the si_value member of info.  The system resource
53       used to queue the signal shall be released and returned to  the  system
54       for  other use. If no value is queued, the content of the si_value mem‐
55       ber is undefined. If no further signals are  queued  for  the  selected
56       signal, the pending indication for that signal shall be reset.
57

RETURN VALUE

59       Upon  successful  completion  (that is, one of the signals specified by
60       set is pending or is generated) sigwaitinfo() and sigtimedwait()  shall
61       return the selected signal number. Otherwise, the function shall return
62       a value of −1 and set errno to indicate the error.
63

ERRORS

65       The sigtimedwait() function shall fail if:
66
67       EAGAIN No signal specified by set was generated  within  the  specified
68              timeout period.
69
70       The sigtimedwait() and sigwaitinfo() functions may fail if:
71
72       EINTR  The  wait  was  interrupted  by  an unblocked, caught signal. It
73              shall be documented in system documentation whether  this  error
74              causes these functions to fail.
75
76       The sigtimedwait() function may also fail if:
77
78       EINVAL The timeout argument specified a tv_nsec value less than zero or
79              greater than or equal to 1000 million.
80
81       An implementation should only check for this  error  if  no  signal  is
82       pending in set and it is necessary to wait.
83
84       The following sections are informative.
85

EXAMPLES

87       None.
88

APPLICATION USAGE

90       The  sigtimedwait()  function  times out and returns an [EAGAIN] error.
91       Application developers should note that this is inconsistent with other
92       functions such as pthread_cond_timedwait() that return [ETIMEDOUT].
93
94       Note that in order to ensure that generated signals are queued and sig‐
95       nal values passed to sigqueue() are available in si_value, applications
96       which  use  sigwaitinfo()  or sigtimedwait() need to set the SA_SIGINFO
97       flag for each signal in the set (see  Section  2.4,  Signal  Concepts).
98       This  means  setting each signal to be handled by a three-argument sig‐
99       nal-catching function, even if the handler will never be called.  It is
100       not  possible  (portably) to set a signal handler to SIG_DFL while set‐
101       ting the SA_SIGINFO flag, because assigning to the sa_handler member of
102       struct  sigaction  instead  of  the sa_sigaction member would result in
103       undefined behavior, and SIG_DFL need not be assignment-compatible  with
104       sa_sigaction.   Even  if  an  assignment  of SIG_DFL to sa_sigaction is
105       accepted by the compiler, the implementation need not treat this  value
106       as  special—it  could just be taken as the address of a signal-catching
107       function.
108

RATIONALE

110       Existing programming practice on realtime systems uses the  ability  to
111       pause  waiting  for a selected set of events and handle the first event
112       that occurs in-line instead of  in  a  signal-handling  function.  This
113       allows applications to be written in an event-directed style similar to
114       a state machine. This style of programming  is  useful  for  largescale
115       transaction  processing  in which the overall throughput of an applica‐
116       tion and the ability to clearly track states are  more  important  than
117       the ability to minimize the response time of individual event handling.
118
119       It  is possible to construct a signal-waiting macro function out of the
120       realtime  signal  function  mechanism  defined  in   this   volume   of
121       POSIX.1‐2008.  However, such a macro has to include the definition of a
122       generalized handler for all signals to be waited on. A significant por‐
123       tion  of  the overhead of handler processing can be avoided if the sig‐
124       nal-waiting  function  is  provided  by  the  kernel.  This  volume  of
125       POSIX.1‐2008  therefore  provides two signal-waiting functions—one that
126       waits indefinitely and one with a timeout—as part of the overall  real‐
127       time signal function specification.
128
129       The specification of a function with a timeout allows an application to
130       be written that can be broken out of a wait after a set period of  time
131       if  no  event  has  occurred.  It was argued that setting a timer event
132       before the wait and recognizing the timer event in the wait would  also
133       implement  the  same  functionality,  but at a lower performance level.
134       Because of the performance degradation associated with  the  user-level
135       specification  of a timer event and the subsequent cancellation of that
136       timer event after the wait completes for a valid event,  and  the  com‐
137       plexity  associated  with handling potential race conditions associated
138       with the user-level method, the separate function has been included.
139
140       Note that the semantics of the sigwaitinfo() function are nearly  iden‐
141       tical  to  that  of  the  sigwait()  function defined by this volume of
142       POSIX.1‐2008. The only difference is  that  sigwaitinfo()  returns  the
143       queued  signal  value  in  the value argument. The return of the queued
144       value is required so that applications can differentiate between multi‐
145       ple events queued to the same signal number.
146
147       The  two distinct functions are being maintained because some implemen‐
148       tations may choose to implement the POSIX Threads  Extension  functions
149       and  not  implement  the  queued signals extensions. Note, though, that
150       sigwaitinfo() does not return the queued value if the value argument is
151       NULL,  so  the POSIX Threads Extension sigwait() function can be imple‐
152       mented as a macro on sigwaitinfo().
153
154       The sigtimedwait() function was separated from the sigwaitinfo()  func‐
155       tion  to  address  concerns  regarding  the  overloading of the timeout
156       pointer to indicate indefinite wait (no timeout), timed wait, and imme‐
157       diate  return,  and concerns regarding consistency with other functions
158       where the conditional and timed waits were separate functions from  the
159       pure  blocking  function. The semantics of sigtimedwait() are specified
160       such that sigwaitinfo() could be implemented as a  macro  with  a  null
161       pointer for timeout.
162
163       The  sigwait  functions  provide a synchronous mechanism for threads to
164       wait for asynchronously-generated signals. One important  question  was
165       how  many  threads that are suspended in a call to a sigwait() function
166       for a signal should return from the call when the signal is sent.  Four
167       choices were considered:
168
169        1. Return  an  error  for multiple simultaneous calls to sigwait func‐
170           tions for the same signal.
171
172        2. One or more threads return.
173
174        3. All waiting threads return.
175
176        4. Exactly one thread returns.
177
178       Prohibiting multiple calls to sigwait() for the same signal was felt to
179       be overly restrictive. The ``one or more'' behavior made implementation
180       of conforming packages easy at the expense  of  forcing  POSIX  threads
181       clients  to protect against multiple simultaneous calls to sigwait() in
182       application code in order to achieve predictable  behavior.  There  was
183       concern  that  the  ``all  waiting  threads''  behavior would result in
184       ``signal broadcast  storms'',  consuming  excessive  CPU  resources  by
185       replicating the signals in the general case. Furthermore, no convincing
186       examples could be presented that delivery to all was either simpler  or
187       more powerful than delivery to one.
188
189       Thus, the consensus was that exactly one thread that was suspended in a
190       call to a sigwait function for a signal should return when that  signal
191       occurs. This is not an onerous restriction as:
192
193        *  A multi-way signal wait can be built from the single-way wait.
194
195        *  Signals  should  only  be  handled  by  application-level  code, as
196           library routines cannot guess what the application wants to do with
197           signals generated for the entire process.
198
199        *  Applications  can  thus arrange for a single thread to wait for any
200           given signal and call any needed routines upon its arrival.
201
202       In an application that is using signals for interprocess communication,
203       signal processing is typically done in one place. Alternatively, if the
204       signal is being caught so that process cleanup can be done, the  signal
205       handler thread can call separate process cleanup routines for each por‐
206       tion of the application. Since the application main line  started  each
207       portion  of  the  application,  it is at the right abstraction level to
208       tell each portion of the application to clean up.
209
210       Certainly, there exist programming styles where it is logical  to  con‐
211       sider  waiting  for  a single signal in multiple threads. A simple sig‐
212       wait_multiple() routine can be constructed to achieve this goal. A pos‐
213       sible  implementation  would  be to have each sigwait_multiple() caller
214       registered as having expressed interest in a set of signals.  The call‐
215       er  then waits on a thread-specific condition variable. A single server
216       thread calls a sigwait() function on the union of all  registered  sig‐
217       nals. When the sigwait() function returns, the appropriate state is set
218       and condition variables are broadcast. New  sigwait_multiple()  callers
219       may  cause  the  pending  sigwait() call to be canceled and reissued in
220       order to update the set of signals being waited for.
221

FUTURE DIRECTIONS

223       None.
224

SEE ALSO

226       Section 2.4, Signal Concepts, Section 2.8.1, Realtime Signals, pause(),
227       pthread_sigmask(), sigaction(), sigpending(), sigsuspend(), sigwait()
228
229       The Base Definitions volume of POSIX.1‐2008, <signal.h>, <time.h>
230
232       Portions  of  this text are reprinted and reproduced in electronic form
233       from IEEE Std 1003.1, 2013 Edition, Standard for Information Technology
234       --  Portable  Operating  System  Interface (POSIX), The Open Group Base
235       Specifications Issue 7, Copyright (C) 2013 by the Institute of Electri‐
236       cal  and  Electronics  Engineers,  Inc  and  The  Open Group.  (This is
237       POSIX.1-2008 with the 2013 Technical Corrigendum  1  applied.)  In  the
238       event of any discrepancy between this version and the original IEEE and
239       The Open Group Standard, the original IEEE and The Open Group  Standard
240       is  the  referee document. The original Standard can be obtained online
241       at http://www.unix.org/online.html .
242
243       Any typographical or formatting errors that appear  in  this  page  are
244       most likely to have been introduced during the conversion of the source
245       files to man page format. To report such errors,  see  https://www.ker
246       nel.org/doc/man-pages/reporting_bugs.html .
247
248
249
250IEEE/The Open Group                  2013                     SIGTIMEDWAIT(3P)
Impressum