/* * signal-test.c */ #include #include #include #include #include #include #include void handler(int signum); int main(int argc, char *argv[]) { char buf[10]; struct sigaction sa; /* set up the struct that defines what to do on receipt of a signal */ memset(&sa, '\0', sizeof(sa)); sa.sa_handler = handler; sigemptyset(&sa.sa_mask); sa.sa_flags = 0; /* associate that action with a particular signal */ if(sigaction(SIGUSR1, &sa, NULL) < 0) { perror("sigaction"); exit(1); } /* be helpful */ printf("My pid is %d\n", getpid()); write(1, "waiting for a signal\n", 21); read(0, buf, 10); write(1, "and we're back\n", 15); } void handler(int signum) { char time_string[100]; time_t t; struct tm *tmp; t = time(NULL); tmp = localtime(&t); strftime(time_string, 100, "%H:%M:%S on %d %b %Y", tmp); printf("I received signal %d\n", signum); printf("The time is %s.\n", time_string); }