Parent process unable to read messages from child process

Discussion in 'Perl Misc' started by Gauri, Feb 28, 2007.

  1. Gauri

    Gauri Guest

    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
     
    Gauri, Feb 28, 2007
    #1
    1. Advertising

  2. Gauri

    -berlin.de Guest

    Gauri <> wrote in comp.lang.perl.misc:
    > 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:


    Unfortunately, it isn't strict-compliant. That makes actual debugging
    harder, so I'll only add a few remarks.

    > 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;
    > }
    > }


    It appears that each child prints only a single line of output. That
    may simplify things.

    Up to here your code looks like you know what you are doing. That
    ends here.

    > # 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){


    Starting an endless loop...

    > foreach my $client ($selectpipe->can_read) {
    > $filedetail[$count] = <$uncompclient>;


    Why aren't you using the file handle that ->can_read returned to you?
    Instead, you're reading from the fixed file handle $uncompclient
    that is left over from the first part. That will only work once.

    In a well-scoped program $uncompclient wouldn't even be in sight.
    All file handles in this part should come from $selectpipe.

    > $count++;
    > }
    > }


    You did nothing to break the endless loop. I find it hard to believe
    that the "process exits", as your comment above states.

    > close $uncompread;
    > close $uncompwrite;
    >
    > foreach(@childs){
    > waitpid($_,0);
    > }
    >
    > I'm using Perl version 5.8.5 on OS-Red Hat Linux.


    If the amount of output of each child process is small, as your code
    seems to indicate, a simple solution (without select) is possible.
    It relies on the assumption that the kids never block on output and
    *is* fragile. For a better solution, fix your original code.

    use Vi::QuickFix;
    use Data::Dumper;

    my $numparts = 5;
    my %pipes;
    for (my $splitnum = 1; $splitnum <= $numparts; $splitnum++) {
    pipe my ( $uncompread, $uncompwrite) or die "Cannot pipe: $!";
    defined( my $pid = fork()) or die "fork: $!";
    if ( $pid ) { # parent
    close $uncompwrite;
    $pipes{ $pid} = $uncompread;
    } else { # child
    close $uncompread;
    sleep rand 5; # random execution time
    print $uncompwrite "result of part $splitnum\n"; # some output
    exit;
    }
    }

    while ( ( my $pid = waitpid( -1, 0)) > 0 ) { # wait for next kid to be done
    my $pipe = $pipes{ $pid}; # find its pipe
    push @detail, scalar <$pipe>; # read output
    }
    chomp @detail; # make output more readable

    print Dumper \ @detail;
    __END__

    Anno
     
    -berlin.de, Feb 28, 2007
    #2
    1. Advertising

  3. Gauri

    Gauri Guest

    Hi,
    Thanks for the quick. I tried to implement the solution you suggested
    exactly as you've posted here, However, the parent process populates
    the @detail array with only one pipe output. i.e. the output of the
    child which exited first. Perhaps there is some delay between the
    time, Child process writes to the pipe and parent reads from the
    pipe.
    Could someone suggest something more?

    -Gauri


    >
    > If the amount of output of each child process is small, as your code
    > seems to indicate, a simple solution (without select) is possible.
    > It relies on the assumption that the kids never block on output and
    > *is* fragile. For a better solution, fix your original code.
    >
    > use Vi::QuickFix;
    > use Data::Dumper;
    >
    > my $numparts = 5;
    > my %pipes;
    > for (my $splitnum = 1; $splitnum <= $numparts; $splitnum++) {
    > pipe my ( $uncompread, $uncompwrite) or die "Cannot pipe: $!";
    > defined( my $pid = fork()) or die "fork: $!";
    > if ( $pid ) { # parent
    > close $uncompwrite;
    > $pipes{ $pid} = $uncompread;
    > } else { # child
    > close $uncompread;
    > sleep rand 5; # random execution time
    > print $uncompwrite "result of part $splitnum\n"; # some output
    > exit;
    > }
    > }
    >
    > while ( ( my $pid = waitpid( -1, 0)) > 0 ) { # wait for next kid to be done
    > my $pipe = $pipes{ $pid}; # find its pipe
    > push @detail, scalar <$pipe>; # read output
    > }
    > chomp @detail; # make output more readable
    >
    > print Dumper \ @detail;
    > __END__
    >
    > Anno- Hide quoted text -
    >
    > - Show quoted text -- Hide quoted text -
    >
    > - Show quoted text -
     
    Gauri, Feb 28, 2007
    #3
  4. Gauri

    Guest

    "Gauri" <> wrote:
    > Hi,
    > Thanks for the quick. I tried to implement the solution you suggested
    > exactly as you've posted here, However, the parent process populates
    > the @detail array with only one pipe output. i.e. the output of the
    > child which exited first. Perhaps there is some delay between the
    > time, Child process writes to the pipe and parent reads from the
    > pipe.


    It worked fine for me (well, once I declared @detail because I had added
    use strict). Post exactly what you ran and what output you got.

    > Could someone suggest something more?


    Please don't top post.

    Xho

    --
    -------------------- http://NewsReader.Com/ --------------------
    Usenet Newsgroup Service $9.95/Month 30GB
     
    , Feb 28, 2007
    #4
  5. Gauri

    -berlin.de Guest

    <> wrote in comp.lang.perl.misc:
    > "Gauri" <> wrote:
    > > Hi,
    > > Thanks for the quick. I tried to implement the solution you suggested
    > > exactly as you've posted here, However, the parent process populates
    > > the @detail array with only one pipe output. i.e. the output of the
    > > child which exited first. Perhaps there is some delay between the
    > > time, Child process writes to the pipe and parent reads from the
    > > pipe.

    >
    > It worked fine for me (well, once I declared @detail because I had added
    > use strict).


    Ah... sorry, that's two copy/paste errors. It should have had
    strictures and warnings (instead of using Vi::QuickFix), and
    @detail was supposed to be declared.

    > Post exactly what you ran and what output you got.
    >
    > > Could someone suggest something more?

    >
    > Please don't top post.


    Hehe :)

    Anno
     
    -berlin.de, Feb 28, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page