//a good site on signal handling //http://www.gnu.org/s/libc/manual/html_node/Signal-Handling.html //written by Dr. Ramsey #include //for signal() and such #include //for printf and the like #include //for exit //the handler for signals void handler(int signum) { //strsignal printf("\n**********************************************\n"); psignal(signum, " (*) Signal: "); //describe the signal with a string printf(" (*) Handling signal %d\n",signum); //some example signals that I am choosing to ignore with this program if(signum == SIGINT || signum == SIGTERM || signum == SIGQUIT || signum == SIGHUP) { printf(" (*) Ignoring Interrupt or Termination Request\n"); return; //this return is preventing me from using the default handler later } else if(signum == SIGSEGV) { //seg fault printf(" (*) segmentation violation! - unrecoverable\n"); } else if(signum == SIGFPE) { //div by 0 or floating point exception printf(" (*) arithmetic error. Divide by 0 or invalid operation - unrecoverable\n"); } //set the signals handler to default signal(signum, SIG_DFL); //raise the exception to use the default handler raise(signum); } int main() { int i = -12; //these are the signals I want to use the handler instead o their default signal(SIGINT, handler); signal(SIGSEGV, handler); signal(SIGFPE, handler); signal(SIGTERM, handler); signal(SIGQUIT, handler); signal(SIGHUP, handler); //this is just a toy loop //divides by 0 if user types 0, it quits the loop if the user types -11 while(i != -11) { printf("enter an integer value: "); fflush(stdout); scanf("%d",&i); printf("you typed in %d and 1/%d is %f or using int. arith. %d\n",i,i,1.0f/i, 1/i); } //cause a sigsegv here - derefencing a NULL pointer char *p = NULL; printf("%c\n",*p); }