NetWallah has asked for the wisdom of the Perl Monks concerning the following question:
This year-old discussion illustrates the issue I'm currently facing. That discussion, it seems to me remains unresolved. I'm running ActiveState perl 5.6.0 - attempting to use pipes limits me to 2 threads, after which the program hangs attempting to open the third pipe. I tried to use Net::Shared, as suggested in the discussion - This errors out with "Unknown error at fork-test.pl line 12" on the "store" (See code below).
I'm not hung up on using any particular mechanism - I'm just looking for a consistent, easy to use "for-dummies" type approach to doing useful work by forking multiple processes, and returning results to the parent process.
The model I'ld like is to have a work queue, and as each child-process gets idle, it grabs a unit of work (some kind of lock needed, of course). When the work queue is empty, everybody quits and goes home. The results of the work should be returned to the parent.
In truly glorified mode, I'd like to be able to keep starting child processes, until a specified resource limit is reached, say 75% memory and/or CPU - but that is dreaming - and while I'm at it, it would be nice if the child processes started self-terminating when resources exceed another threshold for a certain time.
Back in the real world -just some easy IPC with multiple child processes in Win32 would be great. Small, legible working examples would be appreciated.
use strict; use Net::Shared; my $num_children = 2; # How many children we'll create my @children; # Store connections to them $SIG{CHLD} = 'IGNORE'; # Don't worry about reaping zombies #my $LF = 0x0A ; # \012; == \cJ == ASCII 10 my $listen = new Net::Shared::Handler; my $new_shared = new Net::Shared::Local (name=>"new_shared");#, port=> +3252, debug=>0); $listen->add(\$new_shared); $listen->store($new_shared, ""); for my $num (1..$num_children){ print "Opening child $num..."; defined(my $child = fork) or die "fork() failed: $!"; if ($child == 0){ &childproc($num); exit; }else{ print "Parent here .. In loop $num\n"; push @children, {pid=>$child}; print "parent added child $num ref \n"; }; } &parent(); $listen->destroy_all; exit 0; # End of prog ############################# sub childproc(){ my $param = shift(); print STDERR "Child $param says Hello\n"; # Child simply echoes data it receives, until EOF my $var = "assigned at the child"; $listen->store($new_shared, $var); exit; } ################# sub parent(){ print "In parent process Child pids=@children \n"; # Send some data to the kids for my $i (1..20){ # pick a child at random my $num = int rand $num_children; } sleep 1; while () { my $var = $listen->retrieve($new_shared); next unless $var; print "Parent says that \$var was \"", $var,"\".\n" if $var; last; } foreach(@children){ my $endpid=waitpid $_->{pid}, 0 ; print "Parent process endpid=$endpid Child($_->{pid}) status=$?" . "(div 256=" . $? / 256 .")\n"; } }
update (broquaint): added <readmore> tag
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Useful fork with IPC on win32 ?
by Thelonius (Priest) on May 27, 2003 at 10:49 UTC | |
|
Re: Useful fork with IPC on win32 ?
by meredith (Friar) on May 27, 2003 at 11:47 UTC | |
|
Re: Useful fork with IPC on win32 ?
by zakzebrowski (Curate) on May 27, 2003 at 10:39 UTC | |
|
Re: Useful fork with IPC on win32 ?
by hangmanto (Monk) on May 27, 2003 at 16:36 UTC | |
|
Re: Useful fork with IPC on win32 ?
by Jenda (Abbot) on May 27, 2003 at 16:26 UTC |