[LCP]Fork freezes when forking more then one

Steve Baker ice at mama.indstate.edu
Tue Aug 26 10:48:02 UTC 2003


juman <juman at chello.se> wrote:
> On Mon, Aug 25, 2003 at 06:47:00PM -0500, Steve Baker wrote:
> > > Before reading from child I close as
> > > close(run->pc[OUTPUT]);
> > 
> >   Why do you do that?  It isn't necessary, and if the program is wanting more
> > input for some reason, it will likely die at the point it tries to read more
> > data (broken pipe).
> > 
>
> If I the software will stop when trying to read from the child...? I
> also thought this was strange and that you should be able to read/write
> to the child in sequences...

  I see now, now that I see the awk script.  Of course you want it to start
emitting data, so you close it's stdin.  Gotcha.

> >   I'm curious as to what the test.awk script does, does it possibly need a
> > '\n' after the filename you give it, so perhaps you should be sending:
> > 
> >   write_to_child (b, "testa.mp3\n");
> > 
>
> That is true that the line should end with a \n to be processed by the
> awk script that looks like :
>
> #!/bin/awk -f
> /\.[mM][pP]3/ {print $0}
>
> But changing the software to send \n to the child I still have to close
> the run->pc[OUTPUT] to be able to read anything at all... If I try to
> read without doing that the software hangs...

  This took me a while.  Strace and /proc are your friends in this case.  With
strace I could see that it the children were still stuck reading from stdin.
With /proc I could see that the second child spawned had the descriptors for
the first child spawned, thus holding it's stdin open even after the parent
had closed it.  This immediately explains why it worked when you closed the
second job first, since it would then finally close the stdin on the first
child once the second one had exited.

  I'm not certain how you can go about working around the problem.  You could
just do something like:

  for(i=4;i<255;i++) close(i);

  in child(), after you have closed and duped stdin and stdout, kludgy, but a
simple work-around, otherwise you'd have to keep a list of all open
descriptors on the parent side somewhere that each child can reference for
closing them.

							- Steve



More information about the linuxCprogramming mailing list