previous | start | next

Signal Example 2

The same example, but the calls to signal are replaced by calls to this wrapper function Signal:

This wrapper function calls the sigaction function and can guarantee

    1   
    2   typedef void (*sighandler_t)(int);
    3   
    4   /*
    5    * Signal - wrapper for the sigaction function
    6    */
    7   sighandler_t Signal(int signum, sighandler_t handler)
    8   {
    9       struct sigaction action, old_action;
   10   
   11       action.sa_handler = handler;
   12       sigemptyset(&action.sa_mask); /* block sigs of type being handled
   13       */
   14       action.sa_flags = SA_RESTART; /* restart syscalls if possible */
   15   
   16       if (sigaction(signum, &action, &old_action) < 0) {
   17           printf("Signal error\n");
   18           exit(0);
   19       }
   20       return (old_action.sa_handler);
   21   }


previous | start | next