#include #include #include #include #include #include #include void *catch_usr1(void *); void *catch_usr1(void *p) { int signo = SIGUSR1; int caught; sigset_t sigs_to_catch; sigset_t s; /* Unblock SIGINT */ sigemptyset(&sigs_to_catch); sigaddset(&sigs_to_catch, SIGINT); kth_sigmask(SIG_UNBLOCK, &sigs_to_catch, &s); /* Identify our thread */ printf("catch_usr1: signal %d processing running as thread %lu \n", signo, kth_self()); printf("catch_usr1: Someone please send pid %d a SIGUSR1\n", getpid()); /* * We inherited a thread sigmask with all the signals * blocked. So, we can wait on whatever signals we're * interested in and (as long as no other thread waits * for them) we'll be sure return from sigwait() to * handle it. */ /* set this thread's signal mask to block out all other signals */ sigaddset(&sigs_to_catch, signo); sigwait(&sigs_to_catch, &caught); printf("catch_usr1: signal %d processing thread caught signal %d\n", signo, caught); return (p); } extern int main(void) { int i; kth_t threads[1]; int num_threads = 0; sigset_t sigs_to_block; /* Identify our thread */ printf("main: running in thread 0x%x\n", (int)kth_self()); /* * Set this thread's signal mask to block out all other signals * Other thread's will inherit the mask */ /* BLOCK ALL SIGNALS */ sigfillset(&sigs_to_block); /* sigdelset(&sigs_to_block,SIGINT); */ kth_sigmask(SIG_BLOCK, &sigs_to_block, NULL); /* Set signal handler for catching SIGSEGV and SIGBUS */ /* Rather than install the action/handler for the process, we create a thread to wait for the signal */ kth_create(&threads[num_threads++], NULL, catch_usr1, NULL); printf("main: %d threads created\n", num_threads); /* wait until all threads have finished */ for (i = 0; i < num_threads; i++) { kth_join(threads[i], NULL); printf("main: joined to thread %d \n", i); } printf("main: all %d threads have finished. \n", num_threads); return 0; }