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