
-----------------------------------
jamonathin
Thu Mar 05, 2009 9:03 pm

Children, piping and execlp
-----------------------------------
Hey all!

So, I have to fork off 3 children and have them execute in order using pipes.  No problem.  The children are supposed to send a string into the pipe just before they exit().  
The second child has to exec the command ls -lR, so I use:

execlp ("ls", "ls", "-lR", NULL);


The problem is: ls -lR is too slow!

I can't put my pipe after the execlp because the child has already exited.  If I put it before, the messaged is passed to child3 who then executes quicker and gets all of his output in before child2.

I can't create an additional child process to take care of the ls -lR and have child2 wait on that to finish.  Also, I think if I were to waitpid() on child2 to exit it would defeat the purpose of piping. 

Does anyone have any ideas? Thanks!

- Jamonathin

-----------------------------------
btiffin
Fri Mar 06, 2009 2:43 pm

RE:Children, piping and execlp
-----------------------------------
Old guy recollections ... so they may be confused.

wait won't defeat the purpose of the pipeline if you need to "execute in order".

Cheers

-----------------------------------
md
Fri Mar 06, 2009 3:15 pm

RE:Children, piping and execlp
-----------------------------------
I'm not quite clear on what your trying to accomplish with the order either; but as btiffin says wait() is probably what your looking for. wait(pid_t) will wait for the specified process to exit.

-----------------------------------
jamonathin
Sat Mar 07, 2009 2:35 pm

Re: Children, piping and execlp
-----------------------------------
What I mean with the order is, all 3 children are going to output something and it has to be displayed:




The thing is, all 3 children also have to be running at the same time.  Before Child 2 exits, it sends a token through the pipe  to the parent who is waiting for that token.  Once the parent receives that token, it sends a token through another pipe to child 3 whom then starts its execution.

So, the pipe is supposed to act as the wait().  If I have the parent use wait() and pipe on child 2, then the pipe will always be hit first and have no point to it since the parent is wait()ing.

My piping code for the parent is as follows: 
Note: Each childX is already connected to their fdX pipe read side 

printf ("I am the father of the following: %d, %d and %d.\n", child1, child2, child3);
                close (fdP

Hope this clears up my issue
