| Skip Navigation Links | |
| Exit Print View | |
![]() | man pages section 2: System Calls Oracle Solaris 11 Information Library |
- wait until a signal is posted
#include <signal.h>intsigwait(sigset_t *set);
cc [flag ... ]file ...-D_POSIX_PTHREAD_SEMANTICS [library...]#include <signal.h>intsigwait(const sigset_t *set,int *sig);
Thesigwait() function selects a signal inset that is pending onthe calling thread. If no signal inset is pending,sigwait() blocksuntil a signal inset becomes pending. The selected signal is cleared fromthe set of signals pending on the calling thread and the numberof the signal is returned, or in the standard–conforming version (seestandards(5))placed insig. The selection of a signal inset is independentof the signal mask of the calling thread. This means a threadcan synchronously wait for signals that are being blocked by the signalmask of the calling thread . To ensure that only the caller receivesthe signals defined inset, all threads should have signals insetmasked including the calling thread.
If more than one thread is usingsigwait() to wait for thesame signal, no more than one of these threads returns fromsigwait()with the signal number. If more than a single thread is blockedinsigwait() for a signal when that signal is generated for the process,it is unspecified which of the waiting threads returns fromsigwait(). Ifthe signal is generated for a specific thread, as bypthread_kill(3C), onlythat thread returns.
Should any of the multiple pending signals in the rangeSIGRTMIN toSIGRTMAX be selected, it will be the lowest numbered one. The selectionorder between realtime and non-realtime signals, or between multiple pending non-realtime signals,is unspecified.
Upon successful completion, the default version ofsigwait() returns a signal number;the standard–conforming version returns0 and stores the received signal number atthe location pointed to bysig. Otherwise, the default version returns -1 andsets errno to indicate an error; the standard-conforming version returns an errornumber to indicate the error.
Thesigwait() function will fail if:
Theset argument points to an invalid address.
The wait was interrupted by an unblocked, caught signal.
Theset argument contains an unsupported signal number.
Example 1 Creating a thread to handle receipt of a signal
The following sample C code creates a thread to handle the receiptof a signal. More specifically, it catches the asynchronously generated signal,SIGINT.
/********************************************************************** compile with -D_POSIX_PTHREAD_SEMANTICS switch;* required by sigwait()** sigint thread handles delivery of signal. uses sigwait( ) to wait* for SIGINT signal.*********************************************************************/#include <pthread.h>#include <stdlib.h>#include <stdio.h>#include <string.h>#include <unistd.h>#include <signal.h>#include <synch.h>static void *threadTwo(void *);static void *threadThree(void *);static void *sigint(void *);sigset_t signalSet;void *main(void){ pthread_t t; pthread_t t2; pthread_t t3; sigfillset ( &signalSet ); /* * Block signals in initial thread. New threads will * inherit this signal mask. */ pthread_sigmask ( SIG_BLOCK, &signalSet, NULL ); printf("Creating threads\n"); pthread_create(&t, NULL, sigint, NULL); pthread_create(&t2, NULL, threadTwo, NULL); pthread_create(&t3, NULL, threadThree, NULL); printf("##################\n"); printf("press CTRL-C to deliver SIGINT to sigint thread\n"); printf("##################\n"); pthread_exit((void *)0);}static void *threadTwo(void *arg){ printf("hello world, from threadTwo [tid: %d]\n", pthread_self()); printf("threadTwo [tid: %d] is now complete and exiting\n", pthread_self()); pthread_exit((void *)0);}static void *threadThree(void *arg){ printf("hello world, from threadThree [tid: %d]\n", pthread_self()); printf("threadThree [tid: %d] is now complete and exiting\n", pthread_self()); pthread_exit((void *)0);}void *sigint(void *arg){ int sig; int err; printf("thread sigint [tid: %d] awaiting SIGINT\n", pthread_self()); /* /* use standard-conforming sigwait() -- 2 args: signal set, signum */ err = sigwait ( &signalSet, &sig ); /* test for SIGINT; could catch other signals */ if (err || sig != SIGINT) abort(); printf("\nSIGINT signal %d caught by sigint thread [tid: %d]\n", sig, pthread_self()); pthread_exit((void *)0);}Seeattributes(5) for descriptions of the following attributes:
|
sigaction(2),sigpending(2),sigprocmask(2),sigsuspend(2),pthread_create(3C),pthread_kill(3C),pthread_sigmask(3C),signal.h(3HEAD),attributes(5),standards(5)
Thesigwait() function cannot be used to wait for signals that cannotbe caught (seesigaction(2)). This restriction is silently imposed by the system.
Solaris 2.4 and earlier releases provided asigwait() facility as specified inPOSIX.1c Draft 6. The final POSIX.1c standard changed the interface as describedabove. Support for the Draft 6 interface is provided for compatibility onlyand may not be supported in future releases. New applications and libraries shoulduse the standard–conforming interface.
Copyright © 2011, Oracle and/or its affiliates. All rights reserved.Legal Notices | ![]() ![]() |