title: About sigprocmask(), SIG_BLOCK and SIG_SETMASK
body: <p>In the chapter 10 Signals of the APUE.</p>
  
  <p>I have some questions about <code>sigprocmask()</code>, <code>SIG_BLOCK</code> and <code>SIG_SETMASK</code>.</p>
  
  <ol>
  <li>The books says "we saved the old mask when we blocked the signal". My understanding is that: </li>
  </ol>
  
  <p>We declare a <code>newmask</code> of type <code>sigset_t</code>, then <code>sigempty(&amp;newmask)</code> means to initalize the newmask so that all signals pointed by <code>newmask</code> are excluded, finally add <code>SIGQUIT</code> to the <code>newmask</code> set.</p>
  
  <p>But I'm not sure what the following means. Is it to <code>SIG_BLOCK</code> the the signals pointed by <code>newmask</code> and <code>sigprocmask()</code> them to the <code>oldmask</code>?</p>
  
  <p><code>sigprocmask(SIG_BLOCK, &amp;newmask, &amp;oldmask)</code></p>
  
  <ol start="2">
  <li>"To unblock the signal, we did a <code>SIG_SETMASK</code> of the old mask.</li>
  </ol>
  
  <p>The <code>oldmask</code> is a <code>sigset_t</code> containing <code>SIGQUIT</code>, how can this unblock the signal(<code>SIGQUIT</code>)?</p>
  
  <p><code>sigprocmask(SIG_SETMASK, &amp;oldmask, NULL)</code></p>
  
  <p>Here is the whole code:</p>
  
  <pre><code>#include &lt;signal.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;errno.h&gt;
  #include &lt;unistd.h&gt;
  
  static void sig_quit(int signo) {
      printf("caught SIGQUIT\n");
      if (signal(SIGQUIT, SIG_DFL) == SIG_ERR)
          printf("can't reset SIGQUIT\n");
  }
  
  int main() {
      sigset_t newmask, oldmask, pendmask;
  
      if (signal(SIGQUIT, sig_quit) == SIG_ERR)
          printf("can't catch SIGQUIT\n");
  
      /* Block SIGQUIT and save current signal mask. */
      sigemptyset(&amp;newmask);
      sigaddset(&amp;newmask, SIGQUIT);
  
      if (sigprocmask(SIG_BLOCK, &amp;newmask, &amp;oldmask) &lt; 0)
         printf("SIG_BLOCK error");
      sleep(5);   /* SIGQUIT here will remain pending */
      if (sigpending(&amp;pendmask) &lt; 0)
         printf("sigpending error");
      if (sigismember(&amp;pendmask, SIGQUIT))
         printf("\nSIGQUIT pending\n");
  
      /* Restore signal mask which unblocks SIGQUIT. */
      if (sigprocmask(SIG_SETMASK, &amp;oldmask, NULL) &lt; 0)
         printf("SIG_SETMASK error");
      printf("SIGQUIT unblocked\n");
      sleep(5);   /* SIGQUIT here will terminate with core file */
  
      return 0;
  }
  </code></pre>
  
  <p>Thanks a lot!</p>