title: Input redirection and pipes
body: <p>I understand piping where you want to run a command like ls -l | wc -l:</p>
  
  <pre><code>int pipes[2];
  pipe(pipes); 
  
  if (fork() == 0){ //first fork
    dup2(pipes[1],1); 
    close(pipes[0]);
    close(pipes[1]);
  
    execvp(arr1[0], arr1); //arr1[0] = "ls" and  arr1[1] = "-l" and arr1[2] = 0
    perror("Ex failed");
    exit(1);
  }
  
  if (fork() == 0){ //2nd fork
    close(pipes[1]);
    dup2(pipes[0],0); 
    close(pipes[0]);
  
    execvp(arr2[0], arr2); //arr2[0] = "wc" and  arr2[1] = "-l" and arr2[2] = 0
    perror("Ex failed");
    exit(1);
  }
  </code></pre>
  
  <p>But, how would you include input and output redirection? lets say I want to cat &lt; foo.txt | wc -l</p>
  
  <p>I understand the first fork needs to be modified, but I don't understand what is needed (another dup2()?). I would greatly appreciate some help.</p>
  
  <p>Thanks. </p>