Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Parent process unable to read messages from child process

Reply
Thread Tools

Parent process unable to read messages from child process

 
 
Gauri
Guest
Posts: n/a
 
      02-28-2007
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.

I want the parent to be able to read the values of all the children
immediately after they exit.
Can some one help me fix the problem?

Here is my script:

use IO::Select;
use IO::File;

my $selectpipe = new IO::Select;

for ($splitnum = 1; $splitnum <= $numparts; $splitnum++) { #$numparts
changes each time.
pipe($uncompread, $uncompwrite) or die "Cannot pipe: $!";
my $pid = fork();
die "fork: $!" unless defined $pid;
if ($pid){#parent
push (@childs, $pid);
close $uncompwrite;
$selectpipe->add($uncompread);
} else { #child
close $uncompread;
$uncompwrite->autoflush(1);

# compute the following arrays:
# $uncompfile[$splitnum],
# $uncompsize[$splitnum],
# $uncompcksum[$splitnum]
# and store the above values in the array: $uncomp[$splitnum
- 1] as follows:

$uncomp[$splitnum - 1] = "$uncompfile[$splitnum]
$uncompsize[$splitnum] $uncompcksum[$splitnum]";
print $uncompwrite $uncomp[$splitnum - 1];

exit;
}
}

# In the following part I'm trying to store the @uncomp() array above
in the @filedetail() array.
# The number of elements in @filedetail() array should be equal to
$numparts above.
# This is the part which causes error. I'm unable to read all elements
in the @uncomp() array
# above. After the first element in the @uncomp() array is read, the
process exits. Can
# somebody please help?

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

foreach(@childs){
waitpid($_,0);
}

I'm using Perl version 5.8.5 on OS-Red Hat Linux.

-Thanks,
Regards,
Gauri

 
Reply With Quote
 
 
 
 
xhoster@gmail.com
Guest
Posts: n/a
 
      02-28-2007
"Gauri" <> 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
 
Reply With Quote
 
 
 
 
Gauri
Guest
Posts: n/a
 
      02-28-2007
Hi,
I appreciate the quick response.

Apologies for the typo--
The statement in the main code is:
foreach $uncompclient($selectpipe->can_read){


my @filedetail = map {<$_>} @list_of_child_handles;
Could you please explain series of blocking read in details with
respect to my code?

-Thanks a lot.

-Regards,
Gauri



On Feb 28, 2:09 pm, xhos...@gmail.com wrote:
> "Gauri" <himaga...@gmail.com> 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- Hide quoted text -
>
> - Show quoted text -



 
Reply With Quote
 
xhoster@gmail.com
Guest
Posts: n/a
 
      03-01-2007
"Gauri" <> wrote:
> Hi,
> I appreciate the quick response.
>
> Apologies for the typo--
> The statement in the main code is:
> foreach $uncompclient($selectpipe->can_read){
>
> my @filedetail = map {<$_>} @list_of_child_handles;
> Could you please explain series of blocking read in details with
> respect to my code?


The reason for using a select is so that you can do something useful
while waiting for data to become ready for reading. But you don't
have anything useful to do. So don't use select. In your spawning code,
instead of adding the handle to the IO::Select object just push it onto
@list_of_child_handles.

Xho

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

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
pexpect on windows - child process of another child process - quickquestion Z W Python 0 03-09-2013 10:00 PM
If a class Child inherits from Parent, how to implementChild.some_method if Parent.some_method() returns Parent instance ? metal Python 8 10-30-2009 10:31 AM
a simple program to illustrate that child process does not shareresources with parent process sree.harsha.sn@gmail.com C Programming 4 11-09-2008 03:59 AM
Parent process unable to read messages from child process Gauri Perl Misc 4 02-28-2007 11:18 PM
Pass from parent to child, then update parent with child value... Noel Dolan Javascript 0 07-18-2004 05:52 PM



Advertisments