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"; } }