1 #include <stdlib.h> 2 #include <string.h> 3 #include <unistd.h> 4 #include <errno.h> 5 #include <stdio.h> 6 #include <signal.h> 7 #include <pthread.h> 8 9 static void sig_usr1(int); 10 11 static pthread_t main_thread; 12 13 void * 14 child_main(void *no_args) 15 { 16 // int i; 17 18 // Only do it once, to shorten test --njn 19 // for (i = 0; i < 5; ++i) 20 // { 21 sleep (1); 22 fprintf (stdout, "thread CHILD sending SIGUSR1 to thread MAIN\n"); 23 if (pthread_kill (main_thread, SIGUSR1) != 0) 24 fprintf (stderr, "error doing pthread_kill\n"); 25 // } 26 27 return no_args; 28 } 29 30 int 31 main(void) 32 { 33 struct sigaction sigact; 34 sigset_t newmask, oldmask; 35 pthread_t child; 36 37 memset(&newmask, 0, sizeof newmask); 38 sigemptyset (&newmask); 39 sigaddset (&newmask, SIGUSR1); 40 41 if (pthread_sigmask (SIG_BLOCK, &newmask, &oldmask) != 0) 42 fprintf (stderr, "SIG_BLOCK error"); 43 44 memset (&sigact, 0, sizeof sigact); 45 sigact.sa_handler = sig_usr1; 46 if (sigaction(SIGUSR1, &sigact, NULL) != 0) 47 fprintf (stderr, "signal(SIGINT) error"); 48 49 main_thread = pthread_self (); 50 if (pthread_create (&child, NULL, child_main, NULL) != 0) 51 fprintf (stderr, "error creating thread"); 52 53 pthread_join (child, NULL); 54 55 exit(0); 56 } 57 58 static void 59 sig_usr1 (int signo) 60 { 61 fprintf (stderr, "SHOULD NOT BE HERE (SIGUSR1)!!!!\n"); 62 return; 63 } 64 65 66