title: C: dup2, pipe and fork not working as expected
body: <p>I'm trying to do a simple fork -> execute another program -> say "hello" to that child process -> read back something -> print what received.</p>
  
  <p>The program used as child just waits for any line of input and prints something to the stdout like "hello there!"</p>
  
  <p>This is my "host" program (that is not working):</p>
  
  <pre><code>#include &lt;sys/types.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;stdio.h&gt;
  
  #define IN 0
  #define OUT 1
  #define CHILD 0
  
  main ()
  {
    pid_t pid;
    int pipefd[2];
    FILE* output;
    char buf[256];
  
    pipe(pipefd);
  
    pid = fork();    
    if (pid == CHILD)
    {
      printf("child\n");
      dup2(pipefd[IN], IN);
      dup2(pipefd[OUT], OUT);
      execl("./test", "test", (char*) NULL);
    }
    else
    {
      sleep(1);
      printf("parent\n");
      write(pipefd[IN], "hello!", 10); // write message to the process    
      read(pipefd[OUT], buf, sizeof(buf));
      printf("received: %s\n", buf);
    }
  }
  </code></pre>
  
  <p>I get this:</p>
  
  <pre><code>child
  [.. waits 1 second ..]
  parent
  received: 
  </code></pre>
  
  <p>What am I missing? Thanks!</p>
  
  <p><strong>EDIT (test.c):</strong></p>
  
  <p>By request, this is the child program:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;string.h&gt;
  #include &lt;stdlib.h&gt;
  
  int getln(char line[])
  {
    int nch = 0;
    int c;
  
    while((c = getchar()) != EOF)
      {
        if(c == '\n') break;
      line[nch] = c;
      nch++;
      }
  
   if(c == EOF &amp;&amp; nch == 0) return EOF;
   return nch;
  }
  
  main()
  {
    char line[20];
  
    getln(line);
    printf("hello there!", line);
    fflush(stdout);  
    return 0;
  }
  </code></pre>