在Linux的多線程中使用信號機制,與在進程中使用信號機制有着根本的區別,可以說是完全不同。在進程環境中,對信號的處理是,先註冊信號處理函數,當信號異步發生時,調用處理函數來處理信號。它完全是異步的(我們完全不知到信號會在進程的那個執行點到來!)。然而信號處理函數的實現,有着許多的限制;比如有一些函數不能在信號處理函數中調用;再比如一些函數read、recv等調用時會被異步的信號給中斷(interrupt),因此我們必須對在這些函數在調用時因爲信號而中斷的情況進行處理(判斷函數返回時 enno 是否等於 EINTR)。
- sigwait - wait for a signal
- #include <signal.h>
- int sigwait(const sigset_t *set, int *sig);
Description- The sigwait() function suspends execution of the calling thread until the delivery of one
- of the signals specified in the signal set set. The function accepts the signal (removes
- it from the pending list of signals), and returns the signal number in sig.
- The operation of sigwait() is the same as sigwaitinfo(2), except that:
- * sigwait() only returns the signal number, rather than a siginfo_t structure describing
- the signal.
- * The return values of the two functions are different.
- Return Value
- On success, sigwait() returns 0. On error, it returns a positive error number.
)
- pthread_sigmask - examine and change mask of blocked signals
- #include <signal.h>
- int pthread_sigmask(int how, const sigset_t *set, sigset_t *oldset);
- Compile and link with -pthread.
- DESCRIPTION
- The pthread_sigmask() function is just like sigprocmask(2), with the difference that its use
- in multithreaded programs is explicitly specified by POSIX.1-2001.
- Other differences are noted in this page.
- For a description of the arguments and operation of this function, see sigprocmask(2).
- RETURN VALUE
- On success, pthread_sigmask() returns 0; on error, it returns an error number.
- NOTES
- A new thread inherits a copy of its creator's signal mask.
- (from man sigprocmask: )
- The behavior of the call is dependent on the value of how, as follows.
- SIG_BLOCK
- The set of blocked signals is the union of the current set and the set argument.
- SIG_UNBLOCK
- The signals in set are removed from the current set of blocked signals. It is permissible
- to attempt to unblock a signal which is not blocked.
- SIG_SETMASK
- The set of blocked signals is set to the argument set.
- If oldset is non-NULL, the previous value of the signal mask is stored in oldset.
- If set is NULL, then the signal mask is unchanged (i.e., how is ignored), but the current
- value of the signal mask is nevertheless returned in oldset (if it is not NULL).
- #include <signal.h>
- int pthread_kill(pthread_t thread, int sig);
- Compile and link with -pthread.
- DESCRIPTION
- The pthread_kill() function sends the signal sig to thread, another thread in the same
- process as the caller. The signal is asynchronously directed to thread.
- If sig is 0, then no signal is sent, but error checking is still performed; this can be
- used to check for the existence of a thread ID.
- RETURN VALUE
- On success, pthread_kill() returns 0; on error, it returns an error number, and no signal
- is sent.
- ERRORS
- ESRCH No thread with the ID thread could be found.
- EINVAL An invalid signal was specified.
- #include <pthread.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <signal.h>
- #include <errno.h>
- /* Simple error handling functions */
- #define handle_error_en(en, msg) \
- do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0)
- static void *
- sig_thread(void *arg)
- {
- sigset_t *set = (sigset_t *) arg;
- int s, sig;
- for (;;) {
- s = sigwait(set, &sig);
- if (s != 0)
- handle_error_en(s, "sigwait");
- printf("Signal handling thread got signal %d\n", sig);
- }
- }
- int
- main(int argc, char *argv[])
- {
- pthread_t thread;
- sigset_t set;
- int s;
- /*
- Block SIGINT; other threads created by main() will inherit
- a copy of the signal mask.
- */
- sigemptyset(&set);
- sigaddset(&set, SIGQUIT);
- sigaddset(&set, SIGUSR1);
- s = pthread_sigmask(SIG_BLOCK, &set, NULL);
- if (s != 0)
- handle_error_en(s, "pthread_sigmask");
- s = pthread_create(&thread, NULL, &sig_thread, (void *) &set);
- if (s != 0)
- handle_error_en(s, "pthread_create");
- /*
- Main thread carries on to create other threads and/or do
- other work
- */
- pause(); /* Dummy pause so we can test program */
- return 0;
- }
- digdeep@ubuntu:~/pthread/learnthread$ gcc -Wall -pthread -o
pthread_sigmask pthread_sigmask.c
- digdeep@ubuntu:~/pthread/learnthread$ ./pthread_sigmask &
- [1] 4170
- digdeep@ubuntu:~/pthread/learnthread$ kill -QUIT %1
- digdeep@ubuntu:~/pthread/learnthread$ Signal handling thread got signal 3
- digdeep@ubuntu:~/pthread/learnthread$ kill -USR1 %1
- digdeep@ubuntu:~/pthread/learnthread$ Signal handling thread got signal 10
- digdeep@ubuntu:~/pthread/learnthread$ kill -TERM %1
- digdeep@ubuntu:~/pthread/learnthread$
- [1]+ Terminated ./pthread_sigmask
- digdeep@ubuntu:~/pthread/learnthread$