将signal封装成sigaction;自定义信号

信号的“未决”是一种状态,指的是从信号的产生到信号被处理前的这一段时间;
信号的“阻塞”是一个开关动作,指的是阻止信号被处理,但不是阻止信 号产生

int signal1 (int signo,void (*func)(int))

{

struct sigaction act,oact;

act.sa_handler = func;

sigemptyset(&act.sa_mask);

act.sa_flags=0;

return sigaction(signo,&act,&oact);

}

#include <stdio.h>

#include <stdlib.h>

#include <unistd.h>

#include <signal.h>

 

int status = 0;

void catch_signal (int sign)

{

    switch(sign)

    {

        case SIGINT:

        printf("SIG");

        exit(0);

        case SIGTERM:

        printf("KILLED");

        exit(0);

        case SIGUSR1://用户信号:SIGUSR1和SIGUSR2

        status = 1;

       

    }

}

 

int main (int arg,char *args[])

{

    signal(SIGINT,catch_signal);

    signal(SIGKILL,catch_signal);

     signal(SIGTERM,catch_signal);

     signal(SIGUSR1,catch_signal);//注册函数

    int i=0;

    while(1)

    {

        if(status == 1)//通过消息来唤醒

         printf("hello %d\n",i++);

        sleep(1);

       

    }

    return 0;

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值