title: C synchronize processes using signal
body: <p>Okay so I am trying to teach myself on how to do signalling, and I came across a hiccup and I can't figure out what I'm doing wrong. What is going on right now is: it is executing the parent then goes to child and then back to parent.. It's not doing what I want it to do which is execute the parent (which the user defines the amount of time it runs) then kills it then go to child and run itself at the same amount of time.</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;signal.h&gt;
  #include &lt;sys/types.h&gt; // for wait
  #include &lt;sys/wait.h&gt;  // for wait
  
  void action(int);
  void action(int dummy){
      sleep(1);
      printf("Switching\n");
  }
  
  int main(int argc, char *argv[]){
      pid_t pid;
      int m = atoi(argv[1]), i = 0, x = 0;
      if((pid=fork())&gt;0){//parent
          sleep(1);
          while(i &lt; m){
              printf("hello %d\n", x);
              x++;
              kill(pid, SIGUSR1);
              signal(SIGUSR1, action);
              pause();
              i++;
          }
      }
  
      else
          while(i &lt; m){//child
              //waitpid(getppid(), &amp;status, 0); // wait for parent
              signal(SIGUSR1, action);
              pause();
              printf("hi%d\n", x);
              x++;
              kill(getppid(), SIGUSR1);
              i++;
          }
  }
  </code></pre>
  
  <p>What I want it to do is:</p>
  
  <pre><code>hello 0
  hello 1
  hello 2
  hello 3
  hello 4
  Switching
  hi 0
  hi 1
  hi 2
  hi 3
  hi 4
  </code></pre>
  
  <p>Any help is much appreciated!</p>