#include <signal.h> #include <stdio.h> #include <time.h> #include <unistd.h> void handler(int signum) { char result[100]; time_t now; struct tm time1; now = time(NULL); localtime_r(&now, &time1); strftime(result, 100, "%T", &time1); printf("At %s, user pressed Ctrl-C\n", result); } int main (void) { time_t now; struct tm ltime; int i; if (signal(SIGHUP, handler) == SIG_IGN){ return; } now = time(NULL); for(i=0 ; ; i++){ localtime_r(&now, <ime); if(i%1000 == 0){ printf("%d\n", i); } } return 0; }
编译,测试: cc test.c ./a.out & sleep 1 pkill -HUP -f 'a.out' ps aux | grep a.out gdb -p `pidof a.out`