On Wednesday 03 March 2004 20:47, Price, Jason generously enriched virtual reallity by making up this one:
Hi > Thanks for the input - it's quite helpful. and nice:-) > However, I don't fully > understand some of the code - maybe you could help clear it up for me. The > parts I'm unclear on are: > > - the usage of "pipe READER, WRITER", and then the subsequent references to > READER and WRITER. pipe takes two filehandles: a readhandle and a writehandle. all children inherit filehandles of the parent process as a copy. Now the children close the readhandle, since they only want to repord back to the parent and make the writehandle the default filehandle for output using the select command. The parent on the other hand doesnt need the writehandle and reads from the readhandle until eof, which occours after every child has closed the readhandle(by exiting) - the OS keeps track of the handle and closes it only after the last process using it closed it. > - the usage of $| $| > 0 enables autoflush, ie turns buffering off and thus gives you a "hot pipe" - eg., STDOUT is usally line bufferd. Default for $| is 0. > - "1 while wait() > 0" translates to: "Do nothing while i still have children out there." wait() waits for the child to teminate and returns its pid once it died. > > Hmm...I guess that's the majority of the script. :) I can follow what it > does, but I'm not entirely sure why it does it. > > Also, is there any way I can self-contain the output from each child > process? what about prependin a "$$ says:" to each line of child output and doing a m // in the parent process? (Not to smart but all I can come up with and less complicated then having seperate handles for each child :-) Id be interested in how you solf that. HTH, Wolf > Thanks. > > Jason > > -----Original Message----- > From: Bob Showalter [mailto:[EMAIL PROTECTED] > Sent: Wednesday, March 03, 2004 1:17 PM > To: 'Price, Jason'; '[EMAIL PROTECTED]' > Subject: RE: Forking > > Price, Jason wrote: > > Not sure if this is the right list for this - if it's not, please > > direct me to the proper list. > > You've come to the right place. > > > Anyway, I'm trying to get my hands around forking, and was hoping you > > all could help me out. Basically, I'm trying to find a way to fire > > off a remote script on numerous boxes in parallel, returning their > > results to the parent script. Here's the basic flow I'm after: > > > > 1. User brings up web page for an on-demand report. Provides user > > input, hits submit, which fires off the parent script. > > 2. Parent script takes user input, and fires off a remote script > > located on all servers provided by user input. > > 3. Remote scripts return results to an array in the parent script. > > 4. Parent script compiles results and formats output for web display. > > > > The process currently works, but runs each remote server in series, > > which takes a considerable amount of time. I've had a hell of a time > > finding a good explanation of forking, and I only seem to be able to > > figure out how to fork one process at a time. I'm also unsure if the > > parent can utilize variables populated in a child, or if they're > > completely independent after the fork. > > No. The parent cannot see any variables in the child. You need to use some > form of IPC to communicate between the processes. I would suggest using a > pipe. Here's an example of a parent that forks off three children, and then > reads data back from the children through a common pipe: > > #!/usr/bin/perl > > use strict; > $| = 1; > my $nc = 3; # number of children to create > pipe READER, WRITER; # pipe for communication > > for my $c (1 .. $nc) { > # create a child process > defined(my $pid = fork) or die "Couldn't fork: $!"; > next if $pid; # parent loops to create next child > > # child does it's thing and writes back to parent through pipe > close READER; > select WRITER; > $| = 1; > print "Hello, I am child $c, and my PID is $$\n"; > sleep rand(5) + 1; > print "Goodbye from child $c\n"; > exit; # child exits (IMPORTANT!) > } > > # parent reads from children > # pipe will close when last child exits > close WRITER; > while(<READER>) { > print $_; > } > > 1 while wait() > 0; # reap all exit statuses > > Sample output: > > $ perl myscript.pl > Hello, I am child 1, and my PID is 16774 > Hello, I am child 2, and my PID is 16775 > Hello, I am child 3, and my PID is 16776 > Goodbye from child 2 > Goodbye from child 1 > Goodbye from child 3 > > If you need explanation of any of that, let me know. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>