源地址:http://blog.csdn.net/copal/archive/2010/04/06/5454279.aspx
1. 概念
- 按照 POSIX, 异步 (外部) 信号发送到整个进程.
- 所有线程共享同一个设置, 即通过 sigaction 设置的线程处置方法.
- 每个线程有自己的信号掩码, 线程库根据该掩码决定将信号发送到哪个线程.
- 由于Linux 线程实现上的独特性, 外部信号始终发送到特定的线程.
2. 例子
- #include <pthread.h>
- #include <stdio.h>
- #include <sys/signal.h>
- #define NUMTHREADS 3
- void sighand(int signo);
- void *threadfunc(void *parm)
- {
- pthread_t tid = pthread_self();
- int rc;
- printf("Thread %u entered\n", tid);
- rc = sleep(30); /* 若有信号中断则返回剩余秒数 */
- printf("Thread %u did not get expected results! rc=%d\n", tid, rc);
- return NULL;
- }
- void *threadmasked(void *parm)
- {
- pthread_t tid = pthread_self();
- sigset_t mask;
- int rc;
- printf("Masked thread %lu entered\n", tid);
- sigfillset(&mask); /* 将所有信号加入mask信号集 */
- /* 向当前的信号掩码中添加mask信号集 */
- rc = pthread_sigmask(SIG_BLOCK, &mask, NULL);
- if (rc != 0)
- {
- printf("%d, %s\n", rc, strerror(rc));
- return NULL;
- }
- rc = sleep(15);
- if (rc != 0)
- {
- printf("Masked thread %lu did not get expected results! rc=%d \n", tid, rc);
- return NULL;
- }
- printf("Masked thread %lu completed masked work\n", tid);
- return NULL;
- }
- int main(int argc, char **argv)
- {
- int rc;
- int i;
- struct sigaction actions;
- pthread_t threads[NUMTHREADS];
- pthread_t maskedthreads[NUMTHREADS];
- printf("Enter Testcase - %s\n", argv[0]);
- printf("Set up the alarm handler for the process\n");
- memset(&actions, 0, sizeof(actions));
- sigemptyset(&actions.sa_mask); /* 将参数set信号集初始化并清空 */
- actions.sa_flags = 0;
- actions.sa_handler = sighand;
- /* 设置SIGALRM的处理函数 */
- rc = sigaction(SIGALRM,&actions,NULL);
- printf("Create masked and unmasked threads\n");
- for(i=0; i<NUMTHREADS; ++i)
- {
- rc = pthread_create(&threads[i], NULL, threadfunc, NULL);
- if (rc != 0)
- {
- printf("%d, %s\n", rc, strerror(rc));
- return -1;
- }
- rc = pthread_create(&maskedthreads[i], NULL, threadmasked, NULL);
- if (rc != 0)
- {
- printf("%d, %s\n", rc, strerror(rc));
- return -1;
- }
- }
- sleep(3);
- printf("Send a signal to masked and unmasked threads\n");
- /* 向线程发送SIGALRM信号 */
- for(i=0; i<NUMTHREADS; ++i)
- {
- rc = pthread_kill(threads[i], SIGALRM);
- rc = pthread_kill(maskedthreads[i], SIGALRM);
- }
- printf("Wait for masked and unmasked threads to complete\n");
- for(i=0; i<NUMTHREADS; ++i) {
- rc = pthread_join(threads[i], NULL);
- rc = pthread_join(maskedthreads[i], NULL);
- }
- printf("Main completed\n");
- return 0;
- }
- void sighand(int signo)
- {
- pthread_t tid = pthread_self();
- printf("Thread %lu in signal handler\n", tid);
- return;
- }
3. 打印结果
Enter Testcase - ./test
Set up the alarm handler for the process
Create masked and unmasked threads
Thread 3085065104 entered
Masked thread 3076672400 entered
Thread 3068279696 entered
Masked thread 3059886992 entered
Thread 3051494288 entered
Masked thread 3043101584 entered
Send a signal to masked and unmasked threads
Thread 3085065104 in signal handler
Thread 3085065104 did not get expected results! rc=27
Thread 3068279696 in signal handler
Thread 3068279696 did not get expected results! rc=27
Thread 3051494288 in signal handler
Thread 3051494288 did not get expected results! rc=27
Wait for masked and unmasked threads to complete
Masked thread 3076672400 completed masked work
Masked thread 3059886992 completed masked work
Masked thread 3043101584 completed masked work
Main completed
4. 相关函数
sigaction(查询或设置信号处理方式)
#include<signal.h>
int sigaction(int signum,const struct sigaction *act ,struct sigaction *oldact);
sigaction()会依参数signum指定的信号编号来设置该信号的处理函数。参数signum可以指定SIGKILL和SIGSTOP以外的所有信号。
如参数结构sigaction定义如下
struct sigaction
{
void (*sa_handler) (int);
sigset_t sa_mask;
int sa_flags;
void (*sa_restorer) (void);
}
sa_handler此参数和signal()的参数handler相同,代表新的信号处理函数,其他意义请参考signal()。
sa_mask 用来设置在处理该信号时暂时将sa_mask 指定的信号搁置。
sa_restorer 此参数没有使用。
sa_flags 用来设置信号处理的其他相关操作,下列的数值可用。
#include<signal.h>
int sigfillset(sigset_t * set);
sigfillset()用来将参数set信号集初始化,然后把所有的信号加入到此信号集里。
int sigemptyset(sigset_t *set);
- SIG_BLOCK:向当前的信号掩码中添加new,其中new表示要阻塞的信号组。
- SIG_UNBLOCK:向当前的信号掩码中删除new,其中new表示要取消阻塞的信号组。
- SIG_SETMASK:将当前的信号掩码替换为new,其中new表示新的信号掩码。