title: How can I make 'fork()'ed children share stdin?
body: <p>I have this small program:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;sys/wait.h&gt;
  
  int main() {
      int orig = 1;
      for (int i = 0; (i != 3) &amp;&amp; orig; ++i) {
          orig = orig &amp;&amp; fork();
      }
      if (orig) {
          for (int i = 0; i != 3; ++i) {
              wait(NULL);
          }
      } else {
          int num;
          scanf("%d", &amp;num);
          printf("%d\n", num*num);
      }
  }
  </code></pre>
  
  <p>Which is  supposed to simply square three numbers that it reads from <code>stdin</code>, but it does not work as expected. Specifically, it looks like one of the children "hogs" all of the <code>cat</code>ted input, since the program does roughly this:</p>
  
  <pre><code>2
  2
  2
  </code></pre>
  
  <p>to</p>
  
  <pre><code>4
  0
  0
  </code></pre>
  
  <p>I think I need to use <code>dup</code> to fix this, but there is almost nothing on it in our course materials, and all I find on the web is way too complicated for me to understand. How exactly can I make all the processes share <code>stdin</code>?</p>