title: How to create unix process binary trees?
body: <p>Can some one help me out here, not necessarily complete my homework.</p>
  
  <p>I need to create a process tree using fork(); in Unix/C so far the best I can get to to the 4th level here is my code:</p>
  
  <pre><code>/* Includes */
  #include &lt;unistd.h&gt; /* Symbolic Constants */
  #include &lt;stdio.h&gt; /* Input/Output */
  #include &lt;stdlib.h&gt; /* General Utilities */
  
  int main()
  {
      pid_t childpid;
      pid_t pid;
      int i;
      childpid=fork();
      waitpid();
      i=0;
      for (; i&lt;1; i++) {
          int b=0;
          for (; b&lt;1;b++) {
              childpid=fork();
              fprintf(stderr,"the value of i is %d, i am %ld , my parent is %ld\n",
                              i, (long)getpid(), (long)getppid());
  
              if (childpid==0) {
                  break;
              }
  
          }
          if (childpid==0) {
              break;
          }
      }
  
      sleep(1);
  }
  </code></pre>
  
  <p>I am very new to C programming, well programming in general, so I wanted to ask the general public what steps I am missing. The tree depth needs to be user specified so I figured I would use n a for loop but that only multiplies across a level rather than creating new levels.</p>
  
  <p>Can someone help me out and point me in the right direction :)
  I need it to fork like this</p>
  
  <pre><code>           i
            / \
           /   \
          /     \
         /       \
        /         \
       i           i
      / \         / \
     /   \       /   \
    i     i     i     i
   / \   / \   / \   / \
  i   i i   i i   i i   i
  </code></pre>