title: I'm trying to make a diagram of fork() process calls in C
body: <p>Here's where I am now</p>
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;unistd.h&gt;
  
  main(){   
      int n;
      int i;
      int x = 1;
  
      printf(&quot;\nenter number of forks:\n&quot;);
      scanf (&quot;%d&quot;,&amp;n);
      printf(&quot;\nnow forking %d times....\n\n&quot;, n);
  
      for (i = 1;i &lt;= n; i++){
          int pid = fork();
          if (pid &lt; 0){ 
              return -1;
          }
  
          if (pid != 0){
              printf(&quot;\nI am the parent (
                  ppid = %d pid = %d)\n&quot;,getppid(),getpid()
              );          
              printf(&quot; x = %d\n&quot;,x);
  
              x++;
              wait();
          }else{
              printf(&quot;\nI am the child (
                  ppid = %d, pid = %d)\n x = 
                  %d\n-------------------------------\n&quot;,
                  getppid(),getpid(),x
              );
          }
      }
  }
  </code></pre>
  <p>and here is my output when I pass in 4:</p>
  <pre><code>enter number of forks:
  4
  
  now forking 4 times....
  
  
  I am the parent (ppid = 26178 pid = 39785)
   x = 1
  
  I am the child (ppid = 39785, pid = 39786)
   x = 1
  -------------------------------
  
  I am the parent (ppid = 39785 pid = 39786)
   x = 1
  
  I am the child (ppid = 39786, pid = 39787)
   x = 1
  -------------------------------
  
  I am the parent (ppid = 39786 pid = 39787)
   x = 1
  
  I am the child (ppid = 39787, pid = 39788)
   x = 1
  -------------------------------
  
  I am the parent (ppid = 39787 pid = 39788)
   x = 1
  
  I am the child (ppid = 39788, pid = 39789)
   x = 1
  -------------------------------
  
  I am the parent (ppid = 39786 pid = 39787)
   x = 2
  
  I am the child (ppid = 39787, pid = 39790)
   x = 2
  -------------------------------
  
  I am the parent (ppid = 39785 pid = 39786)
   x = 2
  
  I am the child (ppid = 39786, pid = 39791)
   x = 2
  -------------------------------
  
  I am the parent (ppid = 39786 pid = 39791)
   x = 2
  
  I am the child (ppid = 39791, pid = 39792)
   x = 2
  -------------------------------
  
  I am the parent (ppid = 39785 pid = 39786)
   x = 3
  
  I am the child (ppid = 39786, pid = 39793)
   x = 3
  -------------------------------
  
  I am the parent (ppid = 26178 pid = 39785)
   x = 2
  
  I am the child (ppid = 39785, pid = 39794)
   x = 2
  -------------------------------
  
  I am the parent (ppid = 39785 pid = 39794)
   x = 2
  
  I am the child (ppid = 39794, pid = 39795)
   x = 2
  -------------------------------
  
  I am the parent (ppid = 39794 pid = 39795)
   x = 2
  
  I am the child (ppid = 39795, pid = 39796)
   x = 2
  -------------------------------
  
  I am the parent (ppid = 39785 pid = 39794)
   x = 3
  
  I am the child (ppid = 39794, pid = 39797)
   x = 3
  -------------------------------
  
  I am the parent (ppid = 26178 pid = 39785)
   x = 3
  
  I am the child (ppid = 39785, pid = 39798)
   x = 3
  -------------------------------
  
  I am the parent (ppid = 39785 pid = 39798)
   x = 3
  
  I am the child (ppid = 39798, pid = 39799)
   x = 3
  -------------------------------
  
  I am the parent (ppid = 26178 pid = 39785)
   x = 4
  
  I am the child (ppid = 39785, pid = 39800)
   x = 4
  -------------------------------
  </code></pre>
  <p>The first thing I notice is that for each instance of the code run, the PPID of the &quot;child&quot; is the PID of the &quot;parent&quot;, so that's good.</p>
  <p>But when I draw a diagram by hand:
  <a href="https://i.stack.imgur.com/FtTzQ.png" rel="nofollow noreferrer">diagram of my output (I'm new so I can't post photos)</a></p>
  <p>For the sake of the assignment, it should be a balanced tree so that the idea of a level is significant. I'm expected to print each process as a node in a diagram such as I have drawn using graphviz, and each node should include its PID and its Level.</p>
  <p>Here is an example from Geeks for Geeks showing what I mean by level:</p>
  <pre><code>
         L1       // There will be 1 child process 
      /     \     // created by line 1.
    L2      L2    // There will be 2 child processes
   /  \    /  \   //  created by line 2
  L3  L3  L3  L3  // There will be 4 child processes 
                  // created by line 3
  
  </code></pre>
  <p>I think I over coded this. How should I alter my code to fork this in a loop and gain a tree-like structure? I am trying to use the variable x to denote the level, but I am not sure if it is working or not, since the structure of the output is quite unexpected to me.</p>