|
Posted by dan byers on April 29, 2005, 12:36 pm
Please log in for more thread options
hey everybody, i have an issue i've been trying to work around for the past
few days, and i haven't gotten very far.
i hope i have the right group for this, apologies if not. perhaps this is
more of a generic perl forum issue.
i need a parent process that forks N children, which stick around and any
one will receive random data from their STDIN from the parent.
the following was implemented to spawn 5 children and store their handles in
an array. The parent will loop through the set and write data to them, and
everything exits.
the problems are the following:
1) the parent's loop that writes to each child seems to have to finish
before any of the children get written to. a more desired behavior is to
have the command flushed to the child as soon as the parent's print
statement finishes.
2) if I throw the parent's "write" sequence in a loop, the children can't be
rewritten to (obviously, as implemented, everything shuts down, but when i
mod the code to re-iterate through the array i see this behavior)
it would appear that i'm just not controlling the STDIN that is shared
amongst the children... is there a way to get around this? anyone have any
examples?
here's some code:
use strict;
use lib qw( );
use vars qw( );
use warnings;
use IO::Pipe;
BEGIN
{
$| = 1;
} # end BEGIN
my( @child );
for ( my $i = 0; $i < 5; $i += 1 )
{
my $pipe = new IO::Pipe or die 'pipe';
my $pid = fork();
if ( $pid > 0 )
{
# Parent
$pipe->writer();
print("Parent ($$) Forked Child $pidn");
push(@child, { Pipe => $pipe, PID => $pid } );
} else {
# Child
# Connect stdin to pipe
$pipe->reader();
my $fd = fileno($pipe);
open(STDIN, "<&$fd") or die "STDIN open: $!";
my $line = <STDIN>; # from the pipe ?
chomp $line;
print "Child ($$) Received: ($line)n";
close($pipe);
exit(0);
} # end if
} # end for
print("Parent ($$) Sending messages to the childrenn");
for ( my $i = 0; $i <= $#child; $i += 1 )
{
print("Parent ($$) sending 'Message $i' to child
$child[$i]->.n");
print $} ("Message $in");
#sleep(1)
} # end for
#print $} ("SOME TeXtn");
#$| = 1;
print("Parent ($$) Finished sending messages. Closing children.n");
for ( my $i = 0; $i <= $#child; $i += 1 )
{
print("Parent ($$): Closing child $child[$i]->.n");
close($child[$i]->);
waitpid($child[$i]->, 0);
} # end for
I really appreciate any assistance... I would prefer to not have to use
sockets for this... pipe's *should* be enough, i would think.
thanks!
Dan
|
|
Posted by xhoster on April 29, 2005, 8:23 pm
Please log in for more thread options
"dan byers" <magmalianathotmaildotcom> wrote:
> the problems are the following:
>
> 1) the parent's loop that writes to each child seems to have to finish
> before any of the children get written to. a more desired behavior is to
> have the command flushed to the child as soon as the parent's print
> statement finishes.
Add "$pipe->autoflush();" immediately after the "$pipe->writer();"
> 2) if I throw the parent's "write" sequence in a loop, the children can't
> be rewritten to (obviously, as implemented, everything shuts down, but
> when i mod the code to re-iterate through the array i see this behavior)
I can't really help you with hypothetical code.
> it would appear that i'm just not controlling the STDIN that is shared
> amongst the children... is there a way to get around this? anyone have
> any examples?
STDIN is not shared amongst the children. All the children have different,
unshared STDINs.
>
> BEGIN
> {
> $| = 1;
> } # end BEGIN
This turns off buffering for the currenly selected file handle, i.e.
STDOUT. It doesn't affect all the new filehandles later created by
IO::Pipe.
>
> my $fd = fileno($pipe);
> open(STDIN, "<&$fd") or die "STDIN open: $!";
>
> my $line = <STDIN>; # from the pipe ?
Why do this? Just leave STDIN alone and read directly from $pipe.
Xho
--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB
|
| Similar Threads | Posted | | A pipe or FH equivalent of LWP::UserAgent ? | February 25, 2005, 3:12 pm |
| Having a problem with a short pipe read | February 23, 2005, 8:58 am |
| writing modules in 'c' | August 27, 2005, 3:18 pm |
| Writing row at a time in Excel using OLE | June 14, 2007, 1:43 pm |
| Writing VB macro script in xls | May 20, 2008, 7:46 am |
| Reading AND writing Excel spreadsheets | April 30, 2005, 10:05 am |
| what module to use when writing automation tests? | August 25, 2008, 12:37 am |
| Writing a HTML/ASP SOAP client for a SOAP::Lite destination | March 16, 2005, 5:19 pm |
| Storing multiple cookies | June 9, 2007, 11:58 am |
| WWW::Mechanize module: multiple Select. | July 13, 2004, 7:36 pm |
|