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.
here is the parent process -
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);
}
in the program 'proc2' I am trying to access the writerfd in the following way-
write(writerfd, msg, msg_len);
but instead, I get a compilation time error - "error: ‘writerfd’ undeclared (first use in this function);"
why is that? I read here on stack overflow that "Open file descriptors are preserved across a call to exec." link. should't I be able to reach writerfd if that is so?
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..)?
Thanks!