Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Children, piping and execlp
Index -> Programming, C -> C Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
jamonathin




PostPosted: Thu Mar 05, 2009 9:03 pm   Post subject: 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:
c:

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
Sponsor
Sponsor
Sponsor
sponsor
btiffin




PostPosted: Fri Mar 06, 2009 2:43 pm   Post subject: 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




PostPosted: Fri Mar 06, 2009 3:15 pm   Post subject: 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.
C:
wait(pid_t)
will wait for the specified process to exit.
jamonathin




PostPosted: Sat Mar 07, 2009 2:35 pm   Post subject: 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:
<Child 1 output>
<Child 2 output>
<Child 3 output>


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
c:

printf ("I am the father of the following: %d, %d and %d.\n", child1, child2, child3);
                close (fdP[1]); /* Close parent pipe writing, read only for Parent
                                each child will send to parent via fdP pipe */

 
                close(fd1[0]); /* Close Child1 read side */
                write (fd1[1], message, strlen(message)); /* Starting child 1 execution */
                read (fdP[0], readbuffer, sizeof(readbuffer)); /* Wait child 1 pipe exit */

                close(fd2[0]); /* Close Child2 read side */
                write (fd2[1], message, strlen(message)); /* Starting child 2 execution */
                read (fdP[0], readbuffer, sizeof(readbuffer)); /* Wait child 2 pipe exit */

                /* right here is where child2is executing: ls -lR
                but child 3 will start and finish before child2 finishes */


                close(fd3[0]); /* Close Child3 read side */
                write (fd3[1], message, strlen(message)); /* Starting child 3 execution */
                read (fdP[0], readbuffer, sizeof(readbuffer)); /* Wait child 3 pipe exit */


Hope this clears up my issue
Display posts from previous:   
   Index -> Programming, C -> C Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 4 Posts ]
Jump to:   


Style:  
Search: