Raymond Wan wrote:

Hi Richard,

I'm no Perl expert, but it sounds like you are right.


Richard wrote:
still confused by after running this program.

1)when I do  #print while <READER>; program only outputs

  --> truly done


Let's comment out "fibonacci", first. Then, my understanding of your posted code is that the child process calculates the factorial and sends it to the parent process. So, if you comment out "print while <READER>", then the parent process doesn't read what it received, and just prints the single line.



2)here is my attempt for understanding of this program.

"The parent process keeps one filehandle and closes the other, while the child does the opposite. The parent and child process can now communicate acorss the pipe as they work in parallel"

because above statement from the book network programming with perl... my thought is a)child choose to write to filehandle WRITER and have all the output from factorial and fibonacci go there b)parent choose to close WRITER and read from READER because that's what it will be able to read since READER will have
     all the output from child's filehandle WRITER

am I interpreting this right way? or close?



Yes, that sounds right. Again, ignoring "fibonacci", the child does the calculations and sends what it has to the parent and exits. The parent reads it in, prints it out using the line mentioned above, and finishes it with a "truly done" message.

That line "print while <READER>" is doing two important functions in the parent...reading from the input pipe, and then immediately writing it out.

So, yes, I don't see anything wrong with what you said. Oh, just one thing -- with the two forks, there are actually two children if you bring fibonacci in. This phrase helped me -- it is from the definition of pipe in the "Programming Perl" book (i.e., the camel book):

"This call [pipe] is almost always used right before a fork, after which the pipe's reader should close WRITEHANDLE, and the writer close READHANDLE. (Otherwise the pipe won't indicate EOF to the reader when the writer closes it.) Note that if you set up a loop of piped processes, deadlock can occur unless you are very careful. In addition, note that Perl's pipes use standard I/O buffering, so you may need to set $| on your WRITEHANDLE to flush after each output command, ..."

Hope this helps...

Ray




my $arg = shift || '10';

pipe(READER,WRITER) or die "Can't open pipe: $!\n";
### pipe(READHANDLE, WRITEHANDLE) ---> first is the name of a filehandle to read from ### 2nd is filehandle to write to. IF successful, pipe() returns a true result code

if ( fork == '0' ) {
    close READER;
    select WRITER; $| = '1';
    factorial($arg);
print "done with factorial\n";
    exit 0;
}

if ( fork == '0' ) {
    close READER;
    select WRITER; $| = '1';
    my $result = fibonacci($arg);
print "done with fibonacci here\n";
    exit 0;
}

#parent process closes WRITER and reads from READER
close WRITER;
print while <READER>;
print "truly done\n";


sub factorial {
  my $target = shift;
  for ( my $result = 1, my $i = 1; $i <= $target; $i++ ) {
print "hi\n";
       print "factorial($i) => ", $result *= $i, "\n";
  }
}

sub fibonacci {
  my $target = shift;
  my ($a,$b) = (1,0);
  for ( my $i = 1; $i <= $target; $i++ ) {
     my $c = $a + $b;
     print "hi2\n";
     print "fibonacci($i) => $c\n";
     ($a,$b) = ($b,$c);
  }
}





thank you!!

I will explorer this bit further and post a code based on it..

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to