in reply to Getting messages from child processes via pipes
Here a tweak, there a tweak, everywhere a code tweak :)
#!/usr/bin/perl # http://perlmonks.org/?node_id=1206479 use strict; use warnings; pipe (my $reader, my $writer); $writer->autoflush(1); my $parent=$$; print "parent: $parent\n"; for (0..2) # Forking children { defined fork or die "Cant fork: $!"; } if($$ == $parent) # Master process { close $writer; print "parent $$\n"; while (1) { my $line = <$reader>; print "<<<<<<<<<<<<<<<<<<< $line"; } } else # Fork process { close $reader; while(1) { print "\tchild ", $$ - $parent, "\n"; print $writer $$ - $parent, "\n"; sleep 1; } exit 0; }
Outputs
parent: 4030 parent 4030 child 1 <<<<<<<<<<<<<<<<<<< 1 child 2 <<<<<<<<<<<<<<<<<<< 2 child 5 <<<<<<<<<<<<<<<<<<< 5 child 4 <<<<<<<<<<<<<<<<<<< 4 child 3 <<<<<<<<<<<<<<<<<<< 3 child 7 child 6 <<<<<<<<<<<<<<<<<<< 7 <<<<<<<<<<<<<<<<<<< 6 child 1 <<<<<<<<<<<<<<<<<<< 1 child 5 <<<<<<<<<<<<<<<<<<< 5 child 4 <<<<<<<<<<<<<<<<<<< 4 child 2 <<<<<<<<<<<<<<<<<<< 2 child 3 <<<<<<<<<<<<<<<<<<< 3 child 7 <<<<<<<<<<<<<<<<<<< 7 child 6 <<<<<<<<<<<<<<<<<<< 6 child 1 <<<<<<<<<<<<<<<<<<< 1 child 4 <<<<<<<<<<<<<<<<<<< 4 child 5 child 7 <<<<<<<<<<<<<<<<<<< 5 child 3 child 2 <<<<<<<<<<<<<<<<<<< 7 <<<<<<<<<<<<<<<<<<< 3 child 6 <<<<<<<<<<<<<<<<<<< 6 <<<<<<<<<<<<<<<<<<< 2
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Getting messages from child processes via pipes
by Frizoker (Initiate) on Jan 01, 2018 at 17:14 UTC |