title: Child process cannot get the input from stdin written by parent process
body: <p>In child process, I use execl() to execute a program "test1" with the input from stdin. However, the input from stdin cannot be passed to "test1", but it is fine for "sort" command.</p>
  
  <p><strong>example.c</strong></p>
  
  <pre class="lang-c prettyprint-override"><code>#include &lt;unistd.h&gt;
  #include &lt;sys/wait.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  int main(int argc, char *argv[]) {
    int fds[2];                      // an array that will hold two file descriptors
    pipe(fds);                       // populates fds with two file descriptors
    pid_t pid = fork();              // create child process that is a clone of the parent
  
    if (pid == 0) {                  // if pid == 0, then this is the child process
      dup2(fds[0], STDIN_FILENO);    // fds[0] (the read end of pipe) donates its data to file descriptor 0
      close(fds[0]);                 // file descriptor no longer needed in child since stdin is a copy
      close(fds[1]);                 // file descriptor unused in child
      //if (execl("/usr/bin/sort", "sort", (char *)0) &lt; 0) exit(0);//working
      if (execl("./test1", "test1", (char *)0) &lt; 0) exit(0);//not working
    } 
  
    // if we reach here, we are in parent process
    close(fds[0]);                 // file descriptor unused in parent
    const char *words[] = {"pear", "peach", "apple"};
    // write input to the writable file descriptor so it can be read in from child:
    size_t numwords = sizeof(words)/sizeof(words[0]);
    for (size_t i = 0; i &lt; numwords; i++) {
      dprintf(fds[1], "%s\n", words[i]); 
    }
  
    // send EOF so child can continue (child blocks until all input has been processed):
    close(fds[1]); 
  
    int status;
    pid_t wpid = waitpid(pid, &amp;status, 0); // wait for child to finish before exiting
    return wpid == pid &amp;&amp; WIFEXITED(status) ? WEXITSTATUS(status) : -1;
  }
  </code></pre>
  
  <p>For "sort" command, the output is:</p>
  
  <pre class="lang-sh prettyprint-override"><code>$ ./example
  apple
  peach
  pear
  </code></pre>
  
  <p>For "test1" , the output is:</p>
  
  <pre class="lang-sh prettyprint-override"><code>$ ./example
  hello!
  test1
  </code></pre>
  
  <p>If it is working for "test1", the output should be:</p>
  
  <pre class="lang-sh prettyprint-override"><code>$ ./test1 pear peach apple
  hello!
  ./test1
  pear
  peach
  apple
  </code></pre>
  
  <p><strong>test1.c</strong></p>
  
  <pre class="lang-c prettyprint-override"><code>#include&lt;stdio.h&gt; 
  #include&lt;stdlib.h&gt; 
  
  int main(int argc, char** argv){
    printf("hello!\n");
    for(int i = 0; i &lt; argc; i++){
      printf("%s\n", argv[i]);
    }
    return 0;
  }
  </code></pre>
  
  <p>What am I doing wrong?</p>