[LCP]Fork freezes when forking more then one

juman juman at chello.se
Tue Aug 26 07:07:01 UTC 2003


I tested to rewrite the software as :

Directly after forking in the parent I close as
close (run->cp[OUTPUT]);
close (run->pc[INPUT]);

Before reading from child I close as
close(run->pc[OUTPUT]);

And now in the main I can actually start 2 processes as
int main(void)
{
    prog *a, *b;

    a = runthis ("/root/mp3/musidab/src/test.awk");
    b = runthis ("/root/mp3/musidab/src/test.awk");
    write_to_child (b, "testa.mp3");
    write_to_child (a, "testb.mp3");
    stoprunning (b);
    stoprunning (a);

    return 0;
}

But now it only works if I close the processes in the order b and then
a. If I turn them around the software hangs again... (I have also fixed
the error I had when using malloc) But I guess there is still something
wrong or must there be any specific close order?

Cheers,

juman

On Mon, Aug 25, 2003 at 12:52:31PM -0500, Steve Baker wrote:
> juman <juman at chello.se> wrote:
> > The sleep is only so I can check which processes is running.
> > But if I run 2 processes as can be seen in the real program the software
> > freezes when trying to read the output of the first process. Can somebody help
> > me with what I do wrong when trying to exit the forked process and read their
> > output?
> 
>   From the look of it, you're failing to close the client side pipe
> descriptors on the parent side after you've forked and exec'ed your new
> program you wish to read from.  When the child exits, the close on the pipe
> will not be registered since the parent also has an open file handle, thus any
> further reads on the pipe will block forever.
> 
> >From your program:
> 
> ...
> > void child (prog *run, char *runthis)
> > {
> >     close(STDOUT);
> >     dup( run->cp[OUTPUT]);
> >     close(STDIN);
> >     dup(run->pc[INPUT]);
> 
> You should close and dup for stderr here as well, since any errors will go to
> the tty, which could raise a SIGTTOU signal if the child process has been
> disassociated from the tty.
> 
> >     close(run->pc[OUTPUT]);
> >     close(run->cp[INPUT]);
> 
>   You're closing the parents descriptors here, like you should, but you do not
> do the reverse of this in the parent.  Make the default action for the fork
> close the run->cp[OUTPUT] and run-pc[INPUT] before you return from the
> runthis() function.
> 
> >     execlp (runthis, NULL);
> >     printf ("ERROR!\n");
> >     exit(1);
> > }
> ...
> 
>   You may also wish to look at socketpair(), which creates a bi-directional
> socket/pipe which is less work to setup than multiple pipe()s.
> 
> 								- Steve
> _______________________________________________
> This is the Linux C Programming List
> :  http://lists.linux.org.au/listinfo/linuxcprogramming List



More information about the linuxCprogramming mailing list