sigaction-sigqueue


#include<stdio.h>
#include<stdlib.h>
#include<signal.h>
#include <unistd.h>
/*********************************************************************

        PCB:    block----------pending
                0               1
                1               0
                sigprocmask     sigpending

        if signal is blocked(sigprocmask),the bit is 1,we can read the bit to use sigpending

*********************************************************************/
#if 0
struct sigaction {
        void     (*sa_handler)(int);
        void     (*sa_sigaction)(int, siginfo_t *, void *);
        sigset_t   sa_mask;
        int        sa_flags;
        void     (*sa_restorer)(void);
};
#endif
#if 0
union sigval {
               int   sival_int;
               void *sival_ptr;
           };
#endif
void handler(int num,siginfo_t *s_t,void *p)
{
        int receive_data = 0;

        if(num == SIGINT)
        {
                receive_data = s_t->si_int;
                printf("receive sig num is %d,data is %d \n",num,receive_data);
        }else if(num == SIGRTMIN){

                receive_data = s_t->si_int;
                printf("receive sig num is %d,data is %d\n",num,receive_data);

        }else if(num == SIGUSR1)
        {

                sigset_t set;//for set the bit of block
                sigemptyset(&set);
                sigaddset(&set,SIGINT);
                sigaddset(&set,SIGRTMIN);
                sigprocmask(SIG_UNBLOCK,&set,NULL);
                printf("unblock of sigint and signalrtmin......\n");
        }
}
int main()
{
        sigset_t set;//for set the bit of block
        struct sigaction my_set;
        my_set.sa_sigaction = handler;
        my_set.sa_flags=SA_SIGINFO;
        sigaction(SIGINT,&my_set ,NULL);
        sigaction(SIGRTMIN,&my_set,NULL);
        sigaction(SIGUSR1,&my_set,NULL);
        sigemptyset(&set);
        sigaddset(&set,SIGINT);
        sigaddset(&set,SIGRTMIN);
        sigprocmask(SIG_BLOCK,&set,NULL);

        pid_t pid = fork();
        if(pid == 0)
        {
                int i = 0;
                union sigval test;
                test.sival_int = 201;
                for(i = 0; i <3 ;i++)
                {
                        printf("send sigint....\n");
                        sigqueue(getppid(),SIGINT,test);
                        test.sival_int++;
                }
                test.sival_int = 300;
                for(i = 0; i <3 ;i++)
                {
                        printf("send sigrtmin.....\n");
                        sigqueue(getppid(),SIGRTMIN,test);
                        test.sival_int++;
                }
                kill(getppid(),SIGUSR1);
        }else if(pid > 0){
                while(1)
                {
                        sleep(1);
                }
        }
}
send sigint....
send sigint....
send sigint....
send sigrtmin.....
send sigrtmin.....
send sigrtmin.....
receive sig num is 34,data is 300
receive sig num is 34,data is 301
receive sig num is 34,data is 302
receive sig num is 2,data is 201 
unblock of sigint and signalrtmin......

as:
1.可靠信号与不可靠信号同时发了3次,可靠信号全部抵达,不可靠信号抵达1次。
2.sigprocmask阻塞后,解除阻塞信号仍然会抵达。
3.sigqueue和sigaction可以传送数据。
4.block为1,pending就是0,表示未达状态。当block为unblock后,sending为0,信号可以抵达。
5.不解之处:SIGINT已经设置为unblock,但是ctrl+c,仍然无法终止进程。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值