title: C close STDOUT running forever
body: <p>I am writing some C code that involves the use of pipes.  To make a child process use my pipe instead of STDOUT for output, I used the following lines:</p>
  
  <pre><code>    close(STDOUT);
      dup2(leftup[1], STDOUT);
  </code></pre>
  
  <p>However, it seems to go into some sort of infinite loop or hang on those lines.  When I get rid of <code>close</code>, it hangs on <code>dup2</code>.  </p>
  
  <p>Curiously, the same idea works in the immediately preceding line for STDIN:</p>
  
  <pre><code>close(STDIN);
  dup2(leftdown[0], STDIN);
  </code></pre>
  
  <p>What could be causing this behavior?</p>
  
  <p>Edit:  Just to be clear...</p>
  
  <pre><code>#define STDIN   0
  #define STDOUT  1
  </code></pre>
  
  <p>Edit 2:  Here is a stripped-down example:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  #include &lt;sys/wait.h&gt;
  
  #define STDIN   0
  #define STDOUT  1
  
  main(){
      pid_t child1 = 0;
      int leftdown[2];
      if (pipe(leftdown) != 0)
          printf("ERROR");
      int leftup[2];
      if (pipe(leftup) != 0)
          printf("ERROR");
  
      printf("MADE PIPES");
  
      child1 = fork();
      if (child1 == 0){
          close(STDOUT);
          printf("TEST 1");
          dup2(leftup[1], STDOUT);
          printf("TEST 2");
          exit(0);
      }
      return(0);  
  }
  </code></pre>
  
  <p>The <code>"TEST 1"</code> line is never reached.  The only output is <code>"MADE PIPES"</code>.</p>