Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
 
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
Parent process unable to read messages from child

Posted by gauri (gauri), 28 February 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




Posted by admin (Graham Ellis), 1 March 2007
Isn't your $uncompread getting reset each time around?



This page is a thread posted to the opentalk forum at www.opentalk.org.uk and archived here for reference. To jump to the archive index please follow this link.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2024: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho