title: signal handling in linux for c
body: <p>I am trying to understand how signals work in Linux from the sample program that I found online, but it has some parts which I don't really understand.</p>
  
  <p>This is my sample program:</p>
  
  <pre><code>#include &lt;signal.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;sys/types.h&gt;
  
  void catcher(int sig) {
      printf("catcher() has gained control\n");
  }
  
  int main(int argc, char *argv[]) {
      struct sigaction sigact;
      sigset_t sigset;
  
      sigemptyset(&amp;sigact.sa_mask);
      sigact.sa_flags = 0;
      sigact.sa_handler = catcher;
      sigaction(SIGUSR1, &amp;sigact, NULL);
  
      printf("before first kill()\n");
      kill(getpid(), SIGUSR1);
  
      sigemptyset(&amp;sigset);
      sigaddset(&amp;sigset, SIGUSR1);
      sigprocmask(SIG_SETMASK, &amp;sigset, NULL);
  
      printf("before second kill()\n");
      kill(getpid(), SIGUSR1);
      printf("after second kill()\n");
  
      return 0;
  }
  </code></pre>
  
  <p>Here is the sample output from my program:</p>
  
  <blockquote>
    <p>before first kill()<br>
    catcher() has gained control<br>
    before second kill()<br>
    after second kill()  </p>
  </blockquote>
  
  <p>Can I know why the first line in the output is <code>before first kill()</code>? Why doesn't <code>catcher() has gained control</code> appear first?</p>
  
  <p>From what I know, <code>sa_handler</code> consists of two types of signal, signal default and signal ignore.</p>
  
  <p>How do we know which signal it will generate? Why would it trigger the function to print the catcher() has gained control if the signal ignore being generate?</p>
  
  <p>Besides, what is the <code>sa_mask</code> function in this program? In my understanding, <code>sa_mask</code> will block the specified signal.</p>