Esteemed Monks - I seek meditations and enlightenment - yet again - on a possibly futile attempt to use "fork" in win32.

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


In reply to Useful fork with IPC on win32 ? by NetWallah

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.