title: Signal handling in C with process and 2 threads doesn't work
body: <p>I am working with the following example (which is based on the example in the manpage for pthread_sigmask in linux):</p>
  
  <pre><code>#include &lt;pthread.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;signal.h&gt;
  #include &lt;errno.h&gt;
  #include &lt;string.h&gt;
  
  /* Simple error handling functions */
  
  #define handle_error_en(en, msg) \
          do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0)
  
  static void * silly_worker(void *arg)
  {
    for(int index=0,max=5; index&lt;max; ++index) {
      printf("waiting %d of %d\n",index,max);
      sleep(1);
    }
    puts("Finished waiting.  Here comes the SIGSEGV");
    strcpy(NULL,"this will crash");
  }
  
  static void *
  sig_thread(void *arg)
  {
      sigset_t *set = (sigset_t *) arg;
      int s, sig;
  
      for (;;) {
          s = sigwait(set, &amp;sig);
          if (s != 0)
              handle_error_en(s, "sigwait");
          printf("Signal handling thread got signal %d\n", sig);
      }
  }
  
  int
  main(int argc, char *argv[])
  {
      pthread_t thread;
      pthread_t thread2;
      sigset_t set;
      int s;
  
      /* Block SIGINT; other threads created by main() will inherit
         a copy of the signal mask. */
  
      sigemptyset(&amp;set);
      sigaddset(&amp;set, SIGQUIT);
      sigaddset(&amp;set, SIGUSR1);
      sigaddset(&amp;set, SIGSEGV);
      s = pthread_sigmask(SIG_BLOCK, &amp;set, NULL);
      if (s != 0)
          handle_error_en(s, "pthread_sigmask");
  
      s = pthread_create(&amp;thread, NULL, &amp;sig_thread, (void *) &amp;set);
      if (s != 0)
          handle_error_en(s, "pthread_create");
  
      /* Main thread carries on to create other threads and/or do
         other work */
  
      s = pthread_create(&amp;thread2, NULL, &amp;silly_worker, (void *) &amp;set);
      if (s != 0)
          handle_error_en(s, "pthread_create");
  
      pause();            /* Dummy pause so we can test program */
  }
  </code></pre>
  
  <p>According to the man page, this should catch the SIGSEGV generated by the silly_worker thread.  But it doesn't.  In fact I'm not sure what agency is getting the signal at all.  When the program runs, I get the following output:</p>
  
  <pre><code>waiting 0 of 5
  waiting 1 of 5
  waiting 2 of 5
  waiting 3 of 5
  waiting 4 of 5
  Finished waiting.  Here comes the SIGSEGV
  Segmentation fault
  </code></pre>
  
  <p>You can see that the signal handler doesn't output a "Segmentation fault" string, so it must be coming from the default handler.  If default, then it kind of spoils the purpose of the  example - to set a signal handler and capture signals and do something with them.</p>
  
  <p>I can find a lot of examples of handlers, but none of them work for this situation:  none of them demonstrate a thread which causes a really obvious SIGSEGV, and catches and reports the error in its custom handler.</p>
  
  <p>The question remains:  how does one get a custom signal handler to get the signal from this SIGSEGV'ing thread?</p>