title: Signal handling in C
body: <p>I am figuring how signals work in C.
  Here is one of the examples taken from old exams:</p>
  
  <pre><code>#include&lt;signal.h&gt;
  #include&lt;unistd.h&gt;
  #include&lt;stdio.h&gt;
  #include&lt;errno.h&gt;
  
  //#include &lt;sys/types.h&gt;
  
  void handler1(int sig) {
   printf("Phantom");
   exit(0);
  }
  int main()
  {
     pid_t pid1;
     signal(SIGUSR1, handler1); //installing signal handler
     if((pid1 = fork()) == 0) { //forking
         printf("Ghost");
         exit(0);
     }
     kill(pid1, SIGUSR1);
     printf("Ninja");
     return 0;
  }
  </code></pre>
  
  <p>So far, GCC gives me two answers <code>Ghost Ninja</code> &amp; <code>Ninja Phantom</code>. Could it produce <code>Ghost Phantom Ninja</code> or any other combination made of 3 names ?</p>
  
  <p>One way I see it could possibly produce 3 names is: 
  Fork, run in Child, print <code>Ghost</code>, exit(0) => in Parent, accept/process signal and from signal Handler print <code>Phantom</code>, kill child, print <code>Ninja</code>. But I am not sure if my "theory" holds the ground.</p>
  
  <p>Also, would the <code>kill(pid1, SIGUSR1)</code> invoke <code>handler()</code> ?</p>
  
  <p>Thanks !</p>