一个简单小例子说明signal的基本用法。
一个简单小例子说明signal的基本用法。捕捉Ctrl+C( INT)
程序代码:
#include <stdio.h> #include <unistd.h> #include <signal.h> /* first, here is the signal handler */ void catch_int(int sig_num) { /* re-set the signal handler again to * catch_int, for next time */ signal(SIGINT, catch_int); printf("Dont't do that\n"); fflush(stdout); } int main(int argc, char *argv[]) { /* set the INT (Ctrl-C) signal handler to * 'catch_int */ signal(SIGINT, catch_int); /* now, lets get into an infinite loop of doing * nothing */ for( ; ; ) pause(); }