Re: Parent process unable to read messages from child process



"Gauri" <himagauri@xxxxxxxxx> wrote:
Hello,

I've a process which is supposed to do the following:
1.A Parent process has to fork to create fixed number of child
processes. (This fixed number changes each time my code runs.)
2. Each child computes certain values and writes these values on to a
pipe.
3. The parent should read each of these values from pipe output of all
children and store them in a global variable to be used later in the
process.

Fork work correct, Child processes compute the correct value, however
Parent is unable to read output of all children. For e.g. when 3
children are created, one child completes execution first, and parent
is able to read it's value. The other two processes complete 20
seconds later and exit immediately one after the other. The parent is
unable to read the values of the 2nd and 3rd child.

First of all, you should be using strict and warnings. If you did
so, it probably would have helped you identify the problem.

$count = 0;
while(1){
foreach my $client ($selectpipe->can_read) {
$filedetail[$count] = <$uncompclient>;
$count++;
}
}

Since $client is what can_read returned, you should be reading from
$client, not $uncompclient, which seems to be used no where else and
so probably undefined. You also need to remove the handle from $selectpipe
once it reaches eof. And you probably shouldn't mix <> and read on the
same handles you are using Select on.

And since your code doesn't do anything meaningful until all the children
are done anyway, there is really no reason to use IO::Select at all. Just
keep a list of the file handles and do a series of blocking reads on them:

my @filedetail = map {<$_>} @list_of_child_handles;

Xho

--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB
.



Relevant Pages

  • Re: timing a fork
    ... if ($kid) { ... What it does is say that the parent isn't interested in ... Once a parent process has forked it can carry on on its own while the child ... In fact what a call to 'system' does is to fork a child ...
    (perl.beginners)
  • Unix Programming FAQ (v1.37)
    ... Why use _exit rather than exit in the child branch of a fork? ... Why doesn't my process get SIGHUP when its parent dies? ... How do I create a named pipe? ... How do I compare strings using regular expressions? ...
    (comp.unix.programmer)
  • Unix Programming FAQ (v1.37)
    ... Why use _exit rather than exit in the child branch of a fork? ... Why doesn't my process get SIGHUP when its parent dies? ... How do I create a named pipe? ... How do I compare strings using regular expressions? ...
    (comp.unix.programmer)
  • Unix Programming FAQ (v1.37)
    ... Why use _exit rather than exit in the child branch of a fork? ... Why doesn't my process get SIGHUP when its parent dies? ... How do I create a named pipe? ... How do I compare strings using regular expressions? ...
    (comp.unix.programmer)
  • Unix Programming FAQ (v1.37)
    ... Why use _exit rather than exit in the child branch of a fork? ... Why doesn't my process get SIGHUP when its parent dies? ... How do I create a named pipe? ... How do I compare strings using regular expressions? ...
    (comp.unix.programmer)