title: Pass File Descriptor - Execve (typecast)
body: <p>I am wondering how I can pass a file descriptor through the <code>execve()</code> command and then access it on the other side. I know that I can use <code>dup2</code> to redirect the file-descriptor but I cannot do that. I am required to actually pass the file descriptor to the child and use it in the child.</p>
  
  <p>What I have done so far:</p>
  
  <p>Parent makes <code>pipe</code> + args like the following:</p>
  
  <pre><code>int pfd[2];
  if(pipe(pfd) == -1)
      exitWithError("PIPE FAILED", 1);
  char *args_1[] = {"reader", argv[1], (char*) pfd, (char *) 0};
  </code></pre>
  
  <p>Then the child calls <code>execve</code> after <code>fork()</code> like the following:</p>
  
  <pre><code>close(pfd[1]);
  execve("./reader", args_1, NULL);
  </code></pre>
  
  <p>Then, in the reader program I try to access pipe descriptor that was passed:</p>
  
  <pre><code>int main(int argc, char* argv[]){
      ...
      write(argv[2][1], buf, read_test);
  </code></pre>
  
  <p><code>argv[2]</code> should be referencing the pipe descriptor, then the <code>argv[1]</code> should go to the write end of the pipe. I am almost positive that I need to do some type-casting differently here, but everything I try doesn't quite work out.</p>
  
  <p><strong>NOTE</strong>: I have a working version of this program using <code>dup2</code> to redirect to <code>stdin</code> and <code>stdout</code> for the children, but I have to actually pass the pipe descriptor to a child as per instructions of the project.</p>
  
  <p>Any help is appreciated.</p>