00001 /* 00002 Signal-handler example 00003 AUP2, Sec. 9.01.7 00004 00005 Copyright 2003 by Marc J. Rochkind. All rights reserved. 00006 May be copied only for purposes and under conditions described 00007 on the Web page www.basepath.com/aup/copyright.htm. 00008 00009 The Example Files are provided "as is," without any warranty; 00010 without even the implied warranty of merchantability or fitness 00011 for a particular purpose. The author and his publisher are not 00012 responsible for any damages, direct or incidental, resulting 00013 from the use or non-use of these Example Files. 00014 00015 The Example Files may contain defects, and some contain deliberate 00016 coding mistakes that were included for educational reasons. 00017 You are responsible for determining if and how the Example Files 00018 are to be used. 00019 00020 */ 00021 #include "defs.h" 00022 /*[pgm]*/ 00023 00024 static volatile sig_atomic_t gotsig = -1; 00025 00026 static void handler(int signum) 00027 { 00028 gotsig = signum; 00029 } 00030 00031 int main(void) 00032 { 00033 struct sigaction act; 00034 time_t start, stop; 00035 00036 memset(&act, 0, sizeof(act)); 00037 act.sa_handler = handler; 00038 act.sa_flags = SA_RESTART; 00039 ec_neg1( sigaction(SIGINT, &act, NULL) ) 00040 printf("Type Ctrl-c in the next 10 secs.\n"); 00041 ec_neg1( start = time(NULL) ) 00042 sleep(20); 00043 ec_neg1( stop = time(NULL) ) 00044 printf("Slept for %ld secs\n", (long)(stop - start)); 00045 if (gotsig > 0) 00046 printf("Got signal number %ld\n", (long)gotsig); 00047 else 00048 printf("Did not get signal\n"); 00049 exit(EXIT_SUCCESS); 00050 00051 EC_CLEANUP_BGN 00052 exit(EXIT_FAILURE); 00053 EC_CLEANUP_END 00054 } 00055 /*[]*/