Argel has asked for the wisdom of the Perl Monks concerning the following question:
Since he's out today I thought I would try my very inexperienced hand at this. The closest I could get is below, which is based on an exmaple in Network Programming with Perl. Since I'm not really familair with this type of programming I used pipe() instead of named pipes.
To reitierate, the question is is there a way to have each child process actually read just one line from the pipe? Any pointers (and other terrible C puns) are appreciated!!
#!/usr/local/bin/perl use strict; use warnings; sub child { my( $child ) = @_; if( fork == 0 ) { # Child close WRITER; select STDOUT; $| = 1; select READER; $| = 1; while( 1 ) { my $line = <READER>; print STDOUT "$child: $line"; sleep 1; } } return; } pipe( READER, WRITER ) or die "Can't open pipe: $!"; # Create children child( 'a' ); child( 'b' ); child( 'c' ); child( 'd' ); child( 'e' ); # Parent close READER; select WRITER; $| = 1; my $str = ""; foreach ( 0..50 ) { $str .= "$_\n"; } while( 1 ) { print WRITER $str; }
Update: Put the sleep 1 in and merged some of the child() lines.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Pipes: Is it possible for a child to just read one line? (records)
by tye (Sage) on Oct 18, 2006 at 22:56 UTC | |
|
Re: Pipes: Is it possible for a child to just read one line?
by ikegami (Patriarch) on Oct 18, 2006 at 22:32 UTC | |
by Argel (Prior) on Oct 18, 2006 at 22:44 UTC | |
by Argel (Prior) on Oct 19, 2006 at 20:59 UTC |