Monks,

I have need of your guidance, for the following problem baffles my impure mind.

The following code works insofar as the Dumper output contains what I expect, but there is no parallel processing -- the children seem to operate sequentially rather than simultaneously.

If I execute the code in the second listing, I can't capture the output, but the children fork and work in parallel.

What am I doing wrong?

Thanks

Listing 1.
#!/usr/bin/perl -w use strict; use warnings; use Data::Dumper; use IO::Handle; my @servers = qw/server1 server2 server3/; chomp @servers; my %results = (); my @kids = (); for my $host (@servers) { pipe(CHILD_RDR,PARENT_WTR); PARENT_WTR->autoflush(1); if (my $pid = fork) { close PARENT_WTR; push @kids,$pid; $results{$host} = receive_message(\*CHILD_RDR); close CHILD_RDR; } else { die "Cannot fork: $!\n" unless defined $pid; close CHILD_RDR; my $cmd = q{/usr/bin/ssh } . $host . q{ /bin/date 2>/dev/null} +; my $result = qx{$cmd}; chomp $result; send_message(\*PARENT_WTR,$result); close PARENT_WTR; exit; } } for (@kids) { waitpid($_,0); } print Dumper %results; sub send_message { my $handle = shift; my $msg = shift; print $handle $msg; return; } sub receive_message { my $handle = shift; my $msg; chomp($msg = <$handle>); return $msg; }
Listing 2.
#!/usr/bin/perl -w use strict; use warnings; use Data::Dumper; use IO::Handle; my @servers = qw/server1 server2 server3/; chomp @servers; my @kids = (); for my $host (@servers) { if (my $pid = fork) { push @kids,$pid; } else { die "Cannot fork: $!\n" unless defined $pid; my $cmd = q{/usr/bin/ssh } . $host . q{ /bin/date 2>/dev/null} +; my $result = qx{$cmd}; chomp $result; print "$host $result\n"; exit; } } for (@kids) { waitpid($_,0); }

In reply to Forking children operate sequentially?! by darwinscusp

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.