#!/usr/bin/perl use strict; use Proc::Fork; use IO::Pipe; my $num_children = shift; # How many children we'll create my %children; # Store connections to them my %childrenin; # Store connections from children $SIG{CHLD} = 'IGNORE'; # Don't worry about reaping zombies # Spawn off some children for my $num ( 1 .. $num_children ) { # Create a pipe for parent-child communication my $pipe = IO::Pipe->new; my $pipein = IO::Pipe->new; # Child simply echoes data it receives, until EOF run_fork { child { print "[$num] Child Start\n"; $pipe->reader; $pipein->writer; my $data; my $dataout = $pipein; while ($data = <$pipe>) { chomp($data); print STDERR "Child [$num]: $data \n"; print $dataout "[$num]: $data \n"; } exit; } }; # Parent here $pipe->writer; $children{$num} = $pipe; $pipein->reader; $childrenin{$num} = $pipein; } # Send some data to the kids for my $i ( 1 .. $num_children ) { my $child = $children{$i}; print $child "Line 1\n"; } # Read data from children # this is where I believe I'm going wrong. =comment for my $i ( 1 .. $num_children ) { my $childin = $childrenin{$i}; my $datain; while ( $datain = <$childin> ) { print "[Parent] $datain \n";; } } =cut