1SIGTIMEDWAIT(3P) POSIX Programmer's Manual SIGTIMEDWAIT(3P)
2
3
4
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
12 sigtimedwait, sigwaitinfo - wait for queued signals (REALTIME)
13
15 #include <signal.h>
16
17 int sigtimedwait(const sigset_t *restrict set,
18 siginfo_t *restrict info,
19 const struct timespec *restrict timeout);
20 int sigwaitinfo(const sigset_t *restrict set,
21 siginfo_t *restrict info);
22
23
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
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
62 return a value of -1 and set errno to indicate the error.
63
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
71 The sigtimedwait() and sigwaitinfo() functions may fail if:
72
73 EINTR The wait was interrupted by an unblocked, caught signal. It
74 shall be documented in system documentation whether this error
75 causes these functions to fail.
76
77
78 The sigtimedwait() function may also fail if:
79
80 EINVAL The timeout argument specified a tv_nsec value less than zero or
81 greater than or equal to 1000 million.
82
83
84 An implementation only checks for this error if no signal is pending in
85 set and it is necessary to wait.
86
87 The following sections are informative.
88
90 None.
91
93 The sigtimedwait() function times out and returns an [EAGAIN] error.
94 Application writers should note that this is inconsistent with other
95 functions such as pthread_cond_timedwait() that return [ETIMEDOUT].
96
98 Existing programming practice on realtime systems uses the ability to
99 pause waiting for a selected set of events and handle the first event
100 that occurs in-line instead of in a signal-handling function. This
101 allows applications to be written in an event-directed style similar to
102 a state machine. This style of programming is useful for largescale
103 transaction processing in which the overall throughput of an applica‐
104 tion and the ability to clearly track states are more important than
105 the ability to minimize the response time of individual event handling.
106
107 It is possible to construct a signal-waiting macro function out of the
108 realtime signal function mechanism defined in this volume of
109 IEEE Std 1003.1-2001. However, such a macro has to include the defini‐
110 tion of a generalized handler for all signals to be waited on. A sig‐
111 nificant portion of the overhead of handler processing can be avoided
112 if the signal-waiting function is provided by the kernel. This volume
113 of IEEE Std 1003.1-2001 therefore provides two signal-waiting func‐
114 tions-one that waits indefinitely and one with a timeout-as part of the
115 overall realtime signal function specification.
116
117 The specification of a function with a timeout allows an application to
118 be written that can be broken out of a wait after a set period of time
119 if no event has occurred. It was argued that setting a timer event
120 before the wait and recognizing the timer event in the wait would also
121 implement the same functionality, but at a lower performance level.
122 Because of the performance degradation associated with the user-level
123 specification of a timer event and the subsequent cancellation of that
124 timer event after the wait completes for a valid event, and the com‐
125 plexity associated with handling potential race conditions associated
126 with the user-level method, the separate function has been included.
127
128 Note that the semantics of the sigwaitinfo() function are nearly iden‐
129 tical to that of the sigwait() function defined by this volume of
130 IEEE Std 1003.1-2001. The only difference is that sigwaitinfo() returns
131 the queued signal value in the value argument. The return of the queued
132 value is required so that applications can differentiate between multi‐
133 ple events queued to the same signal number.
134
135 The two distinct functions are being maintained because some implemen‐
136 tations may choose to implement the POSIX Threads Extension functions
137 and not implement the queued signals extensions. Note, though, that
138 sigwaitinfo() does not return the queued value if the value argument is
139 NULL, so the POSIX Threads Extension sigwait() function can be imple‐
140 mented as a macro on sigwaitinfo().
141
142 The sigtimedwait() function was separated from the sigwaitinfo() func‐
143 tion to address concerns regarding the overloading of the timeout
144 pointer to indicate indefinite wait (no timeout), timed wait, and imme‐
145 diate return, and concerns regarding consistency with other functions
146 where the conditional and timed waits were separate functions from the
147 pure blocking function. The semantics of sigtimedwait() are specified
148 such that sigwaitinfo() could be implemented as a macro with a NULL
149 pointer for timeout.
150
151 The sigwait functions provide a synchronous mechanism for threads to
152 wait for asynchronously-generated signals. One important question was
153 how many threads that are suspended in a call to a sigwait() function
154 for a signal should return from the call when the signal is sent. Four
155 choices were considered:
156
157 1. Return an error for multiple simultaneous calls to sigwait func‐
158 tions for the same signal.
159
160 2. One or more threads return.
161
162 3. All waiting threads return.
163
164 4. Exactly one thread returns.
165
166 Prohibiting multiple calls to sigwait() for the same signal was felt to
167 be overly restrictive. The "one or more" behavior made implementation
168 of conforming packages easy at the expense of forcing POSIX threads
169 clients to protect against multiple simultaneous calls to sigwait() in
170 application code in order to achieve predictable behavior. There was
171 concern that the "all waiting threads" behavior would result in "signal
172 broadcast storms", consuming excessive CPU resources by replicating the
173 signals in the general case. Furthermore, no convincing examples could
174 be presented that delivery to all was either simpler or more powerful
175 than delivery to one.
176
177 Thus, the consensus was that exactly one thread that was suspended in a
178 call to a sigwait function for a signal should return when that signal
179 occurs. This is not an onerous restriction as:
180
181 * A multi-way signal wait can be built from the single-way wait.
182
183 * Signals should only be handled by application-level code, as library
184 routines cannot guess what the application wants to do with signals
185 generated for the entire process.
186
187 * Applications can thus arrange for a single thread to wait for any
188 given signal and call any needed routines upon its arrival.
189
190 In an application that is using signals for interprocess communication,
191 signal processing is typically done in one place. Alternatively, if
192 the signal is being caught so that process cleanup can be done, the
193 signal handler thread can call separate process cleanup routines for
194 each portion of the application. Since the application main line
195 started each portion of the application, it is at the right abstraction
196 level to tell each portion of the application to clean up.
197
198 Certainly, there exist programming styles where it is logical to con‐
199 sider waiting for a single signal in multiple threads. A simple sig‐
200 wait_multiple() routine can be constructed to achieve this goal. A pos‐
201 sible implementation would be to have each sigwait_multiple() caller
202 registered as having expressed interest in a set of signals. The caller
203 then waits on a thread-specific condition variable. A single server
204 thread calls a sigwait() function on the union of all registered sig‐
205 nals. When the sigwait() function returns, the appropriate state is set
206 and condition variables are broadcast. New sigwait_multiple() callers
207 may cause the pending sigwait() call to be canceled and reissued in
208 order to update the set of signals being waited for.
209
211 None.
212
214 Realtime Signals, pause(), pthread_sigmask(), sigaction(), sigpend‐
215 ing(), sigsuspend(), sigwait(), the Base Definitions volume of
216 IEEE Std 1003.1-2001, <signal.h>, <time.h>
217
219 Portions of this text are reprinted and reproduced in electronic form
220 from IEEE Std 1003.1, 2003 Edition, Standard for Information Technology
221 -- Portable Operating System Interface (POSIX), The Open Group Base
222 Specifications Issue 6, Copyright (C) 2001-2003 by the Institute of
223 Electrical and Electronics Engineers, Inc and The Open Group. In the
224 event of any discrepancy between this version and the original IEEE and
225 The Open Group Standard, the original IEEE and The Open Group Standard
226 is the referee document. The original Standard can be obtained online
227 at http://www.opengroup.org/unix/online.html .
228
229
230
231IEEE/The Open Group 2003 SIGTIMEDWAIT(3P)