sigwait() — Wait for an asynchronous signal
Standards
Standards / Extensions | C or C++ | Dependencies |
---|---|---|
POSIX.1 |
both |
Format
#define _OPEN_THREADS
#include <signal.h>
int sigwait(sigset_t *set);
SUSV3:
#define _POSIX_C_SOURCE 200112L
#include <signal.h>
int sigwait(const sigset_t *__restrict__ set, int *__restrict__ sig);
General description
Causes a thread to wait for an asynchronous signal by choosing a pending signal from set, automatically clearing it from the system's set of pending signals, and returning that signal number in the return code.
If no signal in set is pending at the time of the call, the thread is suspended until one or more of the signals in set become pending. The signals defined by set may be unblocked during the call to this routine, and will be blocked when the thread returns from the call unless some other thread is currently waiting for one of those signals.
If more than one thread is using this routine to wait for the same signal, only one of these threads will return from this routine with the signal number.
Special behavior for SUSV3: The sigwait() function selects a pending signal from set, atomically clear it from the system's set of pending signals, and return that signal number in the location referenced by sig.
Argument | Description |
---|---|
sig | location reference where the signal number is stored |
Usage notes
The use of the SIGTHSTOP and SIGTHCONT signal is not supported with this function.
Returned value
If successful, sigwait() returns the signal number.
- Error Code
- Description
- EINVAL
- The set argument contains an invalid or unsupported signal number.
Special behavior for SUSV3: Upon successful completion, sigwait() stores the signal number of the received signal at the location referenced by sig and return zero. Otherwise, an error number is returned to indicate the error.
Example
/* CELEBS26 */
#define _OPEN_THREADS
#include <stdio.h>
#include <errno.h>
#include <signal.h>
#include <pthread.h>
#include <unistd.h>
void *threadfunc(void *parm)
{
int threadnum;
int *tnum;
sigset_t set;
tnum = parm;
threadnum = *tnum;
printf("Thread %d executing\n", threadnum);
sigemptyset(&set);
if(sigaddset(&set, SIGUSR1) == -1) {
perror("Sigaddset error");
pthread_exit((void *)1);
}
if(sigwait(&set) != SIGUSR1) {
perror("Sigwait error");
pthread_exit((void *)2);
}
pthread_exit((void *)0);
}
main() {
int status;
int threadparm = 1;
pthread_t threadid;
int thread_stat;
status = pthread_create( &threadid, NULL,
threadfunc,
(void *)&threadparm);
if ( status < 0) {
perror("pthread_create failed");
exit(1);
}
sleep(5);
status = pthread_kill( threadid, SIGUSR1);
if ( status < 0)
perror("pthread_kill failed");
status = pthread_join( threadid, (void *)&thread_stat);
if ( status < 0)
perror("pthread_join failed");
exit(0);
}
CELEBP73
/* CELEBS73
This example demonstrates the use of the sigwait() function.
The program will wait until a SIGINT signal is received from the
command line.
Expected output:
SIGINT was received
*/
#define _POSIX_C_SOURCE 200112L
#include <signal.h>
#include <stdio.h>
#include <errno.h>
void main() {
sigset_t set;
int sig;
int *sigptr = &sig;
int ret_val;
sigemptyset(&set);
sigaddset(&set, SIGINT);
sigprocmask( SIG_BLOCK, &set, NULL );
printf("Waiting for a SIGINT signal\n");
ret_val = sigwait(&set,sigptr);
if(ret_val == -1)
perror("sigwait failed\n");
else {
if(*sigptr == 2)
printf("SIGINT was received\n");
else
printf("sigwait returned with sig: %d\n", *sigptr);
}
}
Related information
- signal.h
- bsd_signal() — BSD version of signal()
- pause() — Suspend a process pending a signal
- sigaction() — Examine or change a signal action
- sigignore() — Set disposition to ignore a signal
- sigpause() — Unblock a signal and wait for a signal
- sigpending() — Examine pending signals
- sigprocmask() — Examine or change a thread
- sigset() — Change a signal action or a thread
- sigsuspend() — Change mask and suspend the thread