#!/usr/bin/perl
use warnings;
use strict;
my ( $ppid, $in, $out) = $$;
pipe ( $in, $out) or die $!;
# "Daemon"
{
#local STDIN = $in;
local $| = 1;
my $child;
defined( $child = fork) or die $!;
last if $child;
close $out or die $!;
while (<$in>){
print "Parent got: $_";
}
exit 0;
}
# Spawn some client processess
{
#local STDOUT = $out;
select($out);
local $| = 1;
for my $kid (1..10) {
my $child;
defined( $child = fork) or die $!;
next if $child;
close $in;
select( undef, undef, undef, .001)
while kill $ppid, 0;
select( undef, undef, undef, rand(1000)/1000),
print 'Child ', $kid,
' pid=', $$,
' message=', 0 | rand 10000,
$/
for 1..20;
exit 0;
}
}
--
Mike
(Edit: Added 5.6.1-friendly version of code) |