[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [LCP]Fork freezes when forking more then one



juman <juman@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