【原创】《Linux高级程序设计》杨宗德著 - Linux多线程编程 - 多线程异步管理 - 信号

来源:互联网 发布:java拖拽文件上传实例 编辑:程序博客网 时间:2024/05/18 08:25


【原创】《Linux高级程序设计》杨宗德著 - Linux多线程编程 - 多线程异步管理 - 信号


线程在信号操作时的特性 

(1)每一个线程可以向别的线程发送信号。pthread_kill()函数用来完成这一操作。
(2)每一个线程可以设置自己的信号阻塞集合。pthread_sigmask()函数用来完成这一操作,其类似于进程的sigprocmask()函数。
(3)每个线可以设置针对某信号处理的方式,但同一进程中对某信号的处理方式只能有一个有效,即最后一次设置的处理方式。
(4)如果别的进程向当前进程中发送一个信号,由哪个线程处理是未知的。

线程信号管理

1. pthread_kill发送信号


2. pthread_sigmask调用线程的信号掩码


要注意的是,要阻塞SIGKILL和SIGSTOP是不可能的。另外,如果由于某种原因pthread_sigmask()失败,线程的信号掩码将不会变化。

应用实例

#include<stdio.h>#include<pthread.h>#include<stdlib.h>#include<unistd.h>#include<signal.h>void *sigone_program(void *arg);void *sigtwo_program(void *arg);void report(int);pthread_t thread_one,thread_two;int main(int argc,char *argv[]){        int i;        void *status;if(pthread_create(&thread_one,NULL,sigone_program,NULL)!=0)        {                fprintf(stderr,"pthread_create failure\n");                exit(EXIT_FAILURE);        }        if(pthread_create(&thread_two,NULL,sigtwo_program,NULL)!=0)        {                fprintf(stderr,"pthread_create failure\n");                exit(EXIT_FAILURE);        }        sleep(1);        printf("this is parent ,send SIGUSR1,SIGUSR2 to thread %u\n",thread_one);        if(pthread_kill(thread_one,SIGUSR1)!=0)        {                perror("pthread_kill");                exit(EXIT_FAILURE);        }        if(pthread_kill(thread_one,SIGUSR2)!=0)        {                perror("pthread_kill");                exit(EXIT_FAILURE);        }        printf("this is parent ,send SIGUSR1,SIGUSR2  to thread %u\n",thread_two);        if(pthread_kill(thread_two,SIGUSR1)!=0)        {                perror("pthread_kill");                exit(EXIT_FAILURE);        }        if(pthread_kill(thread_two,SIGUSR2)!=0)        {                perror("pthread_kill");                exit(EXIT_FAILURE);        }sleep(1);if(pthread_kill(thread_one,SIGKILL)!=0)        {                perror("pthread_kill");                exit(EXIT_FAILURE);        }        printf("the end\n");        pthread_join(thread_two,NULL);        pthread_join(thread_one,NULL);        return 0;}void *sigone_program(void *arg){        int i;        __sigset_t set;signal(SIGUSR1,report);        sigfillset(&set);        sigdelset(&set,SIGUSR2);pthread_sigmask(SIG_SETMASK,&set,NULL);        for(i=0;i<5;i++)        {                printf("this is set mask %u thread\n",pthread_self());        pause();}}void report(int sig){        printf("\nin signal ,the sig=%d\t,the thread id=%u\n",sig,pthread_self());}void *sigtwo_program(void *arg){        int i;        signal(SIGUSR2,report);        for(i=0;i<5;i++)        {                printf("this is no set mask %u thread\n",pthread_self());        pause();}}
运行结果:

$ ./pthread_signal this is set mask 3076008768 threadthis is no set mask 3067616064 threadthis is parent ,send SIGUSR1,SIGUSR2 to thread 3076008768this is parent ,send SIGUSR1,SIGUSR2  to thread 3067616064in signal ,the sig=12,the thread id=3076008768this is set mask 3076008768 threadin signal ,the sig=12,the thread id=3067616064in signal ,the sig=10,the thread id=3067616064this is no set mask 3067616064 thread已杀死

原文链接:

http://blog.csdn.net/geng823/article/details/41626107

0 0