title: Signals Behavior in C/C++ - Linux
body: <p>Completely new to processes and signals and I was messing around with the following code I wrote :</p>
  <pre><code>void childSignalHandler(int signalCode) {
  
      if (signalCode == SIGUSR1)
          cout &lt;&lt; &quot;Child here : Received ping, responding&quot; &lt;&lt; endl;
  }
  
  void parentSignalHandler(int signalCode) {
  
      if (signalCode == SIGINT)
          cout &lt;&lt; &quot;Parent here : Pinging Child&quot; &lt;&lt; endl;
  
      else if (signalCode == SIGUSR1)
          cout &lt;&lt; &quot;Parent here : Received response&quot; &lt;&lt; endl;
  
  }
  
  
  int main() {
  
      int pid = fork();
  
      /* Child process*/
      if (pid == 0) {
  
          if (signal(SIGUSR1, childSignalHandler) == SIG_ERR) {
              perror(&quot;Child SIGUSR1&quot;);
              exit(1);
          }
          // This did the trick!
          if (signal(SIGINT,SIG_IGN) == SIG_ERR){
              perror(&quot;Child SIGINT&quot;);
              exit(1);
          }
  
          pause();
          kill(getppid(), SIGUSR1);
      } else {
  
          if (signal(SIGINT, parentSignalHandler) == SIG_ERR) {
              perror(&quot;Parent SIGINT&quot;);
              exit(1);
          }
          if (signal(SIGUSR1, parentSignalHandler) == SIG_ERR) {
              perror(&quot;Parent SIGUSR1&quot;);
              exit(1);
          }
          raise(SIGINT); // Maybe change it to pause(); to use keyboard instead ?
          kill(pid, SIGUSR1);
          pause();
      }
  
  
      return 0;
  } 
  </code></pre>
  <p>The code above works perfectly fine and the expected behavior is :</p>
  <pre><code>Parent here : Pinging Child
  Child here : Received ping, responding
  Parent here : Received response
  </code></pre>
  <p>However when I tried the same thing using the keyboard <code>Ctrl + C</code> signal and changed the <code>raise(SIGINT);</code> to <code>pause();</code> then the output was :</p>
  <pre><code>^CParent here : Pinging Child
  ^CParent here : Pinging Child
  </code></pre>
  <p>The way I thought this would work is :</p>
  <ul>
  <li>Both processes will be paused</li>
  <li><code>CTRL + C</code> aka <code>SIGINT</code> will be sent to the parent process which will be <em>unpaused</em></li>
  <li><code>parentSignalHandler()</code> will handle <code>SIGINT</code> and the appropriate message will be printed</li>
  <li>Parent process will send a <code>SIGUSR1</code> to the child process which will be unpaused and the parent process itself will be paused again</li>
  <li><code>childSignalHandler()</code> will handle <code>SIGUSR1</code></li>
  <li>Finally the child process will send a <code>SIGUSR1</code> to the parent process,which once again will be handled by <code>parentSignalHandler()</code></li>
  </ul>
  <p>As it seems it does not work the way I imagined and any insight would be really helpful.</p>
  <p><strong>Edit -- Updated Code</strong></p>