title: C fork in a while loop [continuously check for something and fork a child and kill the other one]
body: <p>say my code is something like this (C, Linux):</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;string.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;errno.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;sys/stat.h&gt;
  #include &lt;signal.h&gt;
  #include &lt;unistd.h&gt;
  
  void zombie1(void)
  {
      while(1)
      {
          // do_some_stuff;
      }
  }
  
  void zombie2(void)
  {
      while(1)
      {
          // do_some_stuff;
      }
  }
  
  int which_zombie(void)
  {
      check_for_this_or_that();
  
      if(this_happened)
      {
          return 1;
      }
      else if(that_happened)
      {
          return 2;
      }
  }
  
  int main(void){
  
      // fork()
      // here i want to call which_zombie() every 10 seconds and run one of the zombies
      // and kill the other one...    
  }
  </code></pre>
  
  <p>how to do that while main process remains intact ? inside the main function, i'm calling a fork() and in the child process i'm calling which_zombie and if one of them already exist kill it and running the other one. i have tried this but no success! :</p>
  
  <pre><code>int main(void)
  {
      int pid = fork();
  
      if(pid == 0)
      {
          int type;
          int zombie1_pid = 0;
          int zombie2_pid = 0;
          while(1)
          {
              type = which_zombie();
              if(type == 1)
              {
                  if(zombie2_pid != 0)
                  {
                      kill(zombie2_pid, SIGTERM);
                      zombie2_pid = 0;
                  }
  
                  zombie1_pid = fork();
                  if(zombie1_pid == 0)
                  {
                      zombie1();
                  }
  
              }
              else if(type == 2)
              {
                  if(zombie1_pid != 0)
                  {
                      kill(zombie1_pid, SIGTERM);
                      zombie1_pid = 0;
                  }
  
                  zombie2_pid = fork();
                  if(zombie2_pid == 0)
                  {
                      zombie2();
                  }
  
              }
  
  
              sleep(10);
          }
      }
      else
      {
          // parent ...
      }
  }
  </code></pre>
  
  <p>from man pages(waitpid) i found that should add a call to waitpid somewhere [childs remain in zombie state]. so what's wrong in this code ?</p>
  
  <hr>
  
  <p><strong>edit1:</strong>
  just to be clear, the program should check the "type" every 10 seconds in the while loop and fork func1 or func2 based on the type and every time if the type changed it kills the child process and "fork"s the other one. of course when we closed the parent all child processes should exit too...
  thanks in adv.</p>
  
  <hr>
  
  <p><strong>edit2:</strong>
  Chris' answer solved the problem but to kill all children and keep the parent alive i found that i should add a signal handler like so:</p>
  
  <pre><code>pid_t parent_pid;
  
  void sigquit_handler(int sig)
  {
      assert(sig == SIGQUIT);
      pid_t self_pid = getpid();
      if(parent_pid != self_pid)
      {
          _exit(0);
      }
  }
  </code></pre>
  
  <p><strong>main:</strong></p>
  
  <pre><code>    parent_pid = getpid();
      signal(SIGQUIT, sigquit_handler);
      kill(-parent_pid, SIGQUIT);
      int status;
      int i;
  
  
          for(;;)
          {
              pid_t child = wait(&amp;status);
              if(child &gt; 0 &amp;&amp; WIFEXITED(status) &amp;&amp; WEXITSTATUS(status) == 0)
              {
                  printf("child %d exited successfully.\n", (int)child);
              }
              else if(child &lt; 0 &amp;&amp; errno == EINTR)
              {
                  continue;
              }
              else{
                  perror("wait");
                  abort();
              }
              break;
      }
  </code></pre>