SPUG: Forked Children talking back to the parent?

Jim Flanagan jimfl at colltech.com
Wed Jan 17 11:12:21 CST 2001


on 1/17/01 7:43 AM, John Cokos at jcokos at ccs.net wrote:
 
> What I'm wanting to do, is fork off 10 child processes, and have each of them
> doing the same loop, each aware of the master array's
> contents, and each able to push things onto it, so that the other children,
> when they're in the loop see the new todo items...
> 
> Can the children intercommunicate?
> 
> If not, can they at least return a value back to the parent?
> In this case, we could have it fork 10, give them a task, get back the new
> todo items found, and die. Then, fork 10 new kids, etc

In general if, instead of using fork() to fork off children, you can use
code that looks similar to:

    $pid = open(CHILD, "-|") or die "Just because\n":
    if ($pid == 0) { # We're the child
        print "Hi Mom!\n";}
    else { # We're the parent
        print <CHILD>;
    }

The STDOUT of the child gets connected to the filehandle listed in the
open(), and the child can send results back to the parent. This is described
in `perldoc -tf open` and `man perlopentut `

If you want to try to manage 10 or so of these, then I'd look at using
IO::Pipe in combination with fork() instead of the native open(). A single
instance of doing this would look like:

    $pipe = new IO::Pipe;

    $pid = fork();
    if ($pid == 0) { #child
        $pipe->writer();
        print $pipe "Hi Mom!\n"; }
    else {
        $pipe->reader();
        print <$pipe>;
    }
    
--
Jim Flanagan          Collective Technologies
jimfl at colltech.com   http://www.colltech.com


 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     POST TO: spug-list at pm.org       PROBLEMS: owner-spug-list at pm.org
      Subscriptions; Email to majordomo at pm.org:  ACTION  LIST  EMAIL
  Replace ACTION by subscribe or unsubscribe, EMAIL by your Email-address
 For daily traffic, use spug-list for LIST ;  for weekly, spug-list-digest
  Seattle Perl Users Group (SPUG) Home Page: http://www.halcyon.com/spug/





More information about the spug-list mailing list