http://qs321.pair.com?node_id=493734


in reply to Re^3: How to tell a child what to do?
in thread How to tell a child what to do?

I wrote it slightly differently, and it seems to work for me:
#!/usr/bin/perl use strict; use warnings; print "I'm the master ($$)\n"; my %pids; my $children = 2; for my $x (1 .. $children) { my ($fh, $pid); my $tries = 6; do {$pid = open $fh, "|-"; unless (defined $pid) { warn "fork: $!\n"; die "bailing out" unless $tries--; sleep 5; } } until defined $pid; if ($pid) { print "I'm master ($$) of child ($pid)\n"; $pids {$pid} = $fh; } else { printf "I'm a child ($$) of master (%d)\n", getppid; while (<>) { chomp; printf "Child ($$) received: [%s]\n", $_; } print "Child ($$) exiting\n"; exit; } } my $count = 0; while (my ($pid, $fh) = each %pids) { my $data = sprintf "[[%02d]]", ++$count; printf "Sending data %s to child (%d)\n", $data, $pid; print $fh $data, "\n"; } while (my ($pid, $fh) = each %pids) { printf "Closing handle to child (%d)\n", $pid; close $fh or warn "Failed to close handle for child ($pid)\n"; } print "Last line\n"; __END__ I'm the master (19054) I'm a child (19055) of master (19054) I'm master (19054) of child (19055) I'm a child (19056) of master (19054) I'm master (19054) of child (19056) Sending data [[01]] to child (19056) Sending data [[02]] to child (19055) Closing handle to child (19056) Child (19056) received: [[[01]]] Child (19056) exiting Closing handle to child (19055) Child (19055) received: [[[02]]] Child (19055) exiting Last line

Replies are listed 'Best First'.
Re^5: How to tell a child what to do?
by Eyck (Priest) on Sep 21, 2005 at 11:26 UTC

    Great. This is perfect.

    However, I don't know why my version fails, which worries me.

    Thanks again, I've got enough to tinker with now.

      I've played with the code a bit more, and it's failing for me as well. But not always. How often it fails, depends on the number of spawned children. Trying 1, 2, 3, 4, and 5 children, each 10 times, I get success rates of 100%, 90%, 40%, 10% and 0%.

      Something is wrong, but I do not know what. I'll have to consult Stevens.

        I think, that if you rewrite this with sysread/syswrite, or, like in merlyn's solution with length-based communication, it starts working.

        This may have something to do with perl's buffering, in particular, when instead of while (<>) I wrote this:

        my $q; $q=<STDIN>; print "Received(child $$): $q\n";
        it stopped hanging on close($fd).

        When I traced what is happening when it hangs, it looks like parent is waiting waitpid(..) (close($fd) on pipe is supposed to do that), while child blissfully unaware is running read...

        Now I can safely go and try to understand merlyn's code.