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


【原创】《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 thread
this is no set mask 3067616064 thread
this is parent ,send SIGUSR1,SIGUSR2 to thread 3076008768
this is parent ,send SIGUSR1,SIGUSR2  to thread 3067616064

in signal ,the sig=12	,the thread id=3076008768
this is set mask 3076008768 thread

in signal ,the sig=12	,the thread id=3067616064

in signal ,the sig=10	,the thread id=3067616064
this is no set mask 3067616064 thread
已杀死

原文链接:

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值