一个简单的signal实例(c语言)
1 #include <signal.h> 2 #include <unistd.h> 3 4 5 void sig_hander(int signum) 6 { 7 printf("catch the signal %d\n",signum); 8 return ; 9 } 10 11 12 int main(int argc,char **argv) 13 { 14 pid_t pid; 15 pid = getpid(); 16 printf("pid[%d]\n",getpid()); 17 if(signal(SIGINT,sig_hander)==SIG_ERR) 18 { 19 printf("catch error!\n"); 20 return -1; 21 } 22 while(1); 23 return 0; 24 }
执行:
momo@momo:mydev/cc $ a.out
pid[8675]
^Ccatch the signal 2
^Ccatch the signal 2
^Ccatch the signal 2