fork and pipe problem

Martijn van Oosterhout kleptog at svana.org
Mon Feb 24 14:03:20 EST 2003


On Mon, Feb 24, 2003 at 12:29:37PM +1100, Duncan Roe wrote:
> On Sat, Feb 22, 2003 at 06:22:44PM +1100, Alex Pollard wrote:
> *** So *close* STDOUT_FILENO before the dup2()
> >     if (dup2(childToParentPipe[1],STDOUT_FILENO)==-1) {
> >       fprintf(stderr, "Failed to dup2 in proc = %d\n",(int)getpid());
> >     }
> >     //fcntl(STDOUT_FILENO, F_SETFD,FD_CLOEXEC);
> *** You *never* want to to that. It will close the pipe, if the dup2 call worked

Actually, dup2 is defined so that it'll close the STDOUT_FILENO if it's still
open. So you don't need to close it first. However, it's only takes a copy
so the original filehandle (childToParentPipe[1]) is still open. You'll want
to close that.

So you should end up with at least two close()s in the child and at least
one in the parent. A FD_CLOEXEC means the handle will get closed on exec().
Is that what you want?

> > Also, is it OK for multiple child processes to use the same pipe to send data
> >  to their common parent? Or should there be a new pipe for each child? I'm 
> > ignoring the problem of zombie children for the moment.

Sure, a pipe can have as many readers and writers as you want. Though it's
your problem to make sure that you can seperate the outputs...
-- 
Martijn van Oosterhout   <kleptog at svana.org>   http://svana.org/kleptog/
> Support bacteria! They're the only culture some people have.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 232 bytes
Desc: not available
Url : http://lists.samba.org/archive/linux/attachments/20030224/83ea8009/attachment.bin


More information about the linux mailing list