void handler(int signum) { printf("get sigum=%d",signum);//打印输出信号,返回的符号值 printf("never quit\n");//打印输出 } int main() { signal(SIGINT,handler);//调用信号函数,捕捉ctrl+c信号,就调用handler函数 while(1);//进入死循环 return 0; } ~
执行运行显示:
CLC@Embed_Learn:~/liuzhihao$ vi xinhao.c
CLC@Embed_Learn:~/liuzhihao$ gcc xinhao.c
CLC@Embed_Learn:~/liuzhihao$ ./a.out
^Cget sigum=2never quit(按Ctrl+C)
^Cget sigum=2never quit(按Ctrl+C)
^Cget sigum=2never quit(按Ctrl+C)
Killed
杀死进程:
CLC@Embed_Learn:~$ ps -aux | grep a.out(查看进程)
Warning: bad ps syntax, perhaps a bogus ‘-’? See http://procps.sf.net/faq.html
CLC 3777 103 0.0 4160 352 pts/0 R+ 13:51 0:10 ./a.out
CLC 3844 0.0 0.0 13588 980 pts/3 R+ 13:51 0:00 grep --color=auto a.out
CLC@Embed_Learn:~$ kill -9 3777(杀死PID为3777的进程)