Hi. I have been looking through the posts for fork, pipe, dup2, and IPC, but I have not found an answer to my question, so here goes:
I want to set up a pipe between 2 separate programs using fork. I have a parent process (pgm1) that will create the child via execve, and then read the child's response. The child will read an input file and send some calculated results back to the parent through the pipe. Again, they are two separate programs... So I am doing the following, and I would like to know if I can use dup2 to set up stdin and stdout between the processes. If so, can anyone point me at an example that involves 2 programs? I am also wondering, what has to be declared in program 2 to establish a link to the pipeFDs decriptors that are declared in my pgm1?
Thanks a lot!!
Code:
int pipeFDs[2]; /* Pipe fds */
char CHILD_PROC_TO_LOAD_PIPE[] = {"./pgm2.o"}; /* Load process */
char *args[] = {NULL};
FILE *fdout; /* parent's output result file */
...
pid = fork();
switch(pid)
{
case -1:
printf("ERROR");
case 0:
dup2(0,pipeFDs[0]); /* Don't know if this is correct. Want */
dup2(1,pipeFDs[1]); /* to link pipes to pgm2 */
execve(CHILD_PROC_TO_LOAD_PIPE,args,NULL);
default:
processLoadedPipe(pipeFDs,fdout,pid); /* parent */
}