title: how to use a file descriptor in a child process after execvp?
body: <p>I am trying to open a child process using fork(), and then execvp()-ing into another program.
  I also want the parent process and the child process to communicate with each other using a pipe.</p>
  
  <p>here is the parent process - </p>
  
  <pre><code>int pipefds[2];
  pipe(pipefds); // In original code I check for errors...
  int readerfd = pipefds[0];
  int writerfd = pipefds[1];
  
  if(pid == 0){
      // Child
      close(readerfd);
          execvp("./proc2",NULL);
  }
  
  
  </code></pre>
  
  <p>in the program 'proc2' I am trying to access the writerfd in the following way-</p>
  
  <pre><code>write(writerfd, msg, msg_len);
  
  </code></pre>
  
  <p>but instead, I get a compilation time error -
  "error: ‘writerfd’ undeclared (first use in this function);"</p>
  
  <p>why is that? I read here on stack overflow that "Open file descriptors are preserved across a call to exec." <a href="https://stackoverflow.com/questions/22241000/does-exec-preserve-file-descriptors">link</a>. should't I be able to reach writerfd if that is so?</p>
  
  <p>how can I write to that file descriptor on the child process after using execvp? what is the correct way to do this and where can I read about the answer (I looked but I didn't find..)?</p>
  
  <p>Thanks!</p>