LINUX信号-sigaction更强壮的信号注册函数

测试环境:ubuntu 16.0

编译器:       g++ (Ubuntu 5.4.0-6ubuntu1~16.04.4) 5.4.0 20160609 (查看版本指令:g++ --version)

实验目的: 使用更强壮的linux信号注册函数  int sigaction(int signum, const struct sigaction *act,struct sigaction *oldact);

遇到的问题: 信号一直断不下来,查看代码使用的是while(1),进入了死循环。改用了pause();

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <string.h>
 4 #include <signal.h>
 5 #include <unistd.h>
 6 #include <sys/types.h>
 7 
 8 void SigFunc(int sig_num)
 9 {
10     switch (sig_num)
11     {
12     case SIGINT:
13         printf("signal--------> sigint \n");
14         break;
15     }
16 }
17 
18 int main(int argc, char* argv[])
19 {
20     struct sigaction sigac;
21 
22     sigac.sa_handler = SigFunc;
23     sigemptyset(&sigac.sa_mask);
24     sigac.sa_flags = 0;
25 
26     //注册监听的信号 和 信号发生调用的方法
27     int IsRegister = sigaction(SIGINT, &sigac, NULL);
28     if (-1 == IsRegister)
29     {
30         perror("没注册成功! \n");
31         exit(1);
32     }
33 
34     //让进程阻塞,等待终端信号
35     pause();
36 
37     return 0;
38 }

 

执行结果:

reacher@ubuntu:~/projects/proj$ ./main
^C  signal--------> sigint

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值