#include #include #include #include #include void sigcatcher(int sig) { fprintf(stderr, "The sigcatcher caught signal %d\n", sig); pthread_exit(0); } void *sig_thread(void *p) { sigset_t sigs_to_catch; /* Unblock SIGINT */ sigemptyset(&sigs_to_catch); sigaddset(&sigs_to_catch, SIGINT); pthread_sigmask(SIG_UNBLOCK, &sigs_to_catch, NULL); fprintf(stderr, "sent a SIGINT (ctrl-C) to this process(%d)\n", getpid()); sleep(60); return (NULL); } extern int main(void) { pthread_t thread; sigset_t sigs_to_block; struct sigaction action; /* Block all signals */ sigfillset(&sigs_to_block); pthread_sigmask(SIG_BLOCK, &sigs_to_block, NULL); /* Set a signal handler */ action.sa_handler = sigcatcher; action.sa_flags = 0; sigaction(SIGINT, &action, NULL); pthread_create(&thread, NULL, sig_thread, NULL); /* wait until thread is finished */ pthread_join(thread, NULL); return 0; }