#include <stdio.h> #include <stdlib.h> #include <errno.h> #include <time.h> #include <signal.h> void handler(int sig) { alarm(1); return; } int main(void) { int i; time_t now; struct tm *pnow; struct sigaction act = { .sa_handler = handler, .sa_flags = SA_RESTART, }; sigemptyset(&act.sa_mask); if (sigaction(SIGALRM, &act, NULL) < 0) return 1; alarm(1); for(i=0;i<60;i++) { sleep(10); now = time(NULL); pnow = localtime(&now); printf("%d:%d:%d\n",pnow->tm_hour,pnow->tm_min,pnow->tm_sec); } exit(0); } |
$ ./Sample0 10:15:52 10:15:53 10:15:54 10:15:56 10:15:57 10:15:58 10:15:59 10:16:0 10:16:1 10:16:2 10:16:3 10:16:4 10:16:5 10:16:6 |