title: How to fork multiple processes from a same parent？
body: <p>I am trying to create multiple processes from a same parent, but it always ended up with more processes than expected. I couldn't figure out how to do it and need some help here.</p>
  
  <p>I found a piece of code online and tried it,</p>
  
  <pre><code>int main ()
  {
      pid_t pid=0;
      int i=0;
      for (i=0; i&lt;3; i++)
      {
          pid=fork();
          switch(pid)
          {
              case 0:
              {
                  cout&lt;&lt;"\nI am a child and my pid is:"&lt;&lt;getpid();
                  cout&lt;&lt;endl;
                  exit(0);
                  break;
              }
              default:
              {
                  cout&lt;&lt;"\nI am a parent and my pid is: "&lt;&lt;getpid();
                  cout&lt;&lt;"\nMy child pid is: "&lt;&lt;pid;
                  cout&lt;&lt;endl;
                  wait(NULL);
                  break;
              }
          }
      }
    return 0;
   }
  </code></pre>
  
  <p>This code does work and creates 3 children from same parent. However, it seems like that's because after each child process was created, it was terminated immediately. So it won't fork more grandchild process in the next round of for loop. But I need to keep these child processes running for sometime and they need to communicate with the parents. </p>