Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Many-to-One pipe

by Zaxo (Archbishop)
on Aug 07, 2002 at 05:10 UTC ( #188235=snippet: print w/ replies, xml ) Need Help??

Description:

I composed this ditty to demonstrate how to let one child process collect data from many client processes forked from the same parent. This was motivated by suaveant's question about speedy local IPC at Fastest way to talk to a perl daemon.

This demo program shows how the file descriptor for the write end of a pipe is duplicated on fork, allowing many processes to share it.

The select... while kill $ppid, 0; line in the client code is there just to keep the kids idle until the parent has departed. That is to avoid waiting or ignoring SIGCHLD. A persistent parent server would need to clean up after the kids.

#!/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 $!;
    print while <STDIN>;
    exit 0;
}

# Spawn some client processess
{
    local *STDOUT = $out;
    local $| = 1;
    for my $kid (1..50) {
        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;
    }
}
Comment on Many-to-One pipe
Download Code
Re: Many-to-One pipe
by RMGir (Prior) on Aug 07, 2002 at 12:14 UTC
    (Edit: See particle's comment; all that's missing is an '*' on the locals... Removed my questions wondering why it didn't work.)

    Here's a version that works for me. I reduced the limits so it didn't spam quite as much :)

    #!/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)
      Hmmm, 5.6.1 doesn't like local STDIN = $in; or local STDOUT = $out;.

      that's because it's missing the sigil... local *STDIN = $in; or local *STDOUT = $out;

      Is that a 5.8 feature? I've never seen that construct before. It'd be nice to have, though!

      this is a standard feature (assuming the syntax is correct.) the first one will fail under 5.008 with the same errors. my correction should work fine under any recent perl.

      ~Particle *accelerates*

Back to Snippets Section

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: snippet [id://188235]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (9)
As of 2014-07-10 07:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (202 votes), past polls