linux 信号的阻塞,Linux 信号的阻塞和未决

// 断言

#include // 结束状态处理

void handle_exit(int wstatus){

if(WIFEXITED(wstatus)){

// 正常结束,结束状态

printf("normal: %d\n",WEXITSTATUS(wstatus));

} else {

if(WIFSIGNALED(wstatus)){

// 信号状态

printf("abnormal: signal %d\n",WTERMSIG(wstatus));

}

}

}

// alarm 处理

void alarm_handle(int num){

printf("alarm_handle: %d\n",num);

alarm(1);

}

void int_handle(int num){

printf("int_handle: %d\n",num);

}

int main ()

{

//测试 1 wait 的使用

/*

int wstatus;

pid_t pid;

pid = fork();

assert(pid!=-1);

if(pid==0){

// son

exit(7);

} else {

wait(&wstatus);

handle_exit(wstatus);

}

pid = fork();

assert(pid!=-1);

if(pid==0){

// son

abort();

} else {

waitpid(pid, &wstatus, 0);

handle_exit(wstatus);

}

*/

//测试 2 信号的注册 发送信号

/*

int wstatus;

int pid = fork();

assert(pid!=-1);

if(pid==0){

// son

execv("./hello.out",NULL);

} else {

// father

sleep(1); // 睡眠一下,等待子进程完成信号的注册

kill(pid, SIGINT); //发送信号

waitpid(pid, &wstatus, 0);

handle_exit(wstatus);

}

*/

//测试 3 alarm循环

/*

signal(SIGALRM,alarm_handle);

alarm(1);

while(1)

pause();

*/

// 测试4 信号状态

/*

sigset_t old;

sigset_t new;

sigset_t pending;

// 注册 SIGINT 的处理函数

signal(SIGINT,int_handle);

// 读取并打印现在的状态字

sigprocmask(0,NULL,&old);

sigpending(&pending);

printf("读取并打印现在的状态字\n");

printf("block: %lu\n", sigismember(&old,SIGINT) );

printf("pendi: %lu\n", sigismember(&pending,SIGINT) );

// 阻塞 SIGINT

printf("\n阻塞 SIGINT\n");

sigemptyset(&new);

sigaddset(&new, SIGINT);

sigprocmask(SIG_BLOCK,&new,&old); //阻塞

sigprocmask(0,NULL,&old); //读取现在的状态

sigpending(&pending);

printf("block: %d\n", sigismember(&old,SIGINT) );

printf("pendi: %d\n", sigismember(&pending,SIGINT) );

// 给自己发送信号, 由于已经阻塞,不能处理

printf("\n给自己发送信号, 由于已经阻塞,不能处理\n");

raise(SIGINT);

sigprocmask(0,NULL,&old);

sigpending(&pending);

printf("block: %d\n", sigismember(&old,SIGINT) );

printf("pendi: %d\n", sigismember(&pending,SIGINT) );

sleep(1); //等待信号处理

// 解除阻塞

printf("\n解除阻塞\n");

sigprocmask(SIG_UNBLOCK,&new,&old);

sigpending(&pending);

printf("block: %d\n", sigismember(&old,SIGINT));

printf("pendi: %d\n", sigismember(&pending,SIGINT) );

sleep(1); //等待信号处理

*/

return 0;

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值