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
- Only signals of the same type as signum will be blocked while in the signal handler.
- If the signal signum is received while the process is executing in a slow system calls such as a read, the system call will be restarted if possible when the handler returns.
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 }