<html><head><style type="text/css"><!-- DIV {margin:0px;} --></style></head><body><div style="font-family:times new roman, new york, times, serif;font-size:12pt"><div style="font-family: times new roman,new york,times,serif; font-size: 12pt;">Like I said, just flip the content between the two routines. I hadda tweak a bit because you can't have both waiting in a while loop or they both wait forever so I just sent a single line from the parent down. And keep in mind, the output looks screwy because the parents will be in order, but the children fire somewhat randomly between depending on when they execute their code.<br><br>SW<br><br>----- Original Message ----<br><div style="font-family: times new roman,new york,times,serif; font-size: 12pt;">From: Mike Ward <unforgiven24@gmail.com><br><br>I wouldn't mind looking at the two-directional code as well, if it's no trouble. <br><br><div class="gmail_quote"><blockquote class="gmail_quote"
style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">--------------------------- cut here -----------------------------<br>
#!/usr/bin/perl<br>
<br>
use Socket;<br>
use IO::Handle;<br>
<br>
my (@c,@p,@pid); # c=child handle, p=parent handle, pid=process ID<br>
for(my $s=0; $s<5; $s++) {<br>
$c[$s] = new IO::Handle;<br>
$p[$s] = new IO::Handle;<br>
<br>
socketpair($c[$s], $p[$s], AF_UNIX, SOCK_STREAM, PF_UNSPEC)<br>
or die "socketpair: $!";<br>
<br>
$c[$s]->autoflush(1);<br>
$p[$s]->autoflush(1);<br>
<br>
<br>
if ($pid[$s] = fork()) { # is the parent<br>
close $p[$s]; # close unused parent stream<br>
&parent($c[$s], $s);<br>
close $c[$s]; # close the stream coming back from the child<br>
waitpid($pid[$s],0); # wait for child process to terminate<br>
} else { # is the child<br>
die "cannot fork: $!" unless defined $pid[$s]; # make sure pid == 0<br>
close $c[$s]; # close the unused child stream<br>
&child($p[$s], $s);<br>
close $p[$s]; # close stream that was talking to the parent<br>
exit(0);<br>
}<br>
}<br>
<br>
sub parent() { # read data coming from child process(es)<br>
my $fh = shift;<br>
my $s = shift;<br>
print $fh "Parent Pid $$ is sending this\n";<br>
while ($line = <$fh>) { # read data from child line by line until done<br>
chomp($line);<br>
print "Parent $s Pid $$ just read this: `$line'\n";<br>
};<br>
}<br>
<br>
sub child() { # send data back to parent through filehandle<br>
my $fh = shift;<br>
print $fh "Child Pid $$ is sending this\n";<br>
print $fh "Child Pid $$ sends some more\n";<br> chomp($line = <$fh>);<br> print "Child $s Pid $$ just read this: `$line'\n";<br>}<br>
<br>
# filehandles didn't behave well using array references and my ultimate solution<br>
# is going to create functions to perform tasks anyway so I just passed the<br>
# handles to subroutines<br>
<br>
-------------- cut ------------<br>bash-3.2$ ./test.pl <br>Parent 0 Pid 3784 just read this: `Child Pid 3800 is sending this'<br>Parent 0 Pid 3784 just read this: `Child Pid 3800 sends some more'<br>Child Pid 3800 just read this: `Parent Pid 3784 is sending this'<br>Child Pid 3808 just read this: `Parent Pid 3784 is sending this'<br>Parent 1 Pid 3784 just read this: `Child Pid 3808 is sending this'<br>Parent 1 Pid 3784 just read this: `Child Pid 3808 sends some more'<br>Parent 2 Pid 3784 just read this: `Child Pid 3288 is sending this'<br>Parent 2 Pid 3784 just read this: `Child Pid 3288 sends some more'<br>Child Pid 3288 just read this: `Parent Pid 3784 is sending this'<br>Parent 3 Pid 3784 just read this: `Child Pid 2956 is sending this'<br>Parent 3 Pid 3784 just read this: `Child Pid 2956 sends some more'<br>Child Pid 2956 just read this: `Parent Pid 3784 is sending this'<br>Child Pid 744 just read this: `Parent Pid 3784
is sending this'<br>Parent 4 Pid 3784 just read this: `Child Pid 744 is sending this'<br>Parent 4 Pid 3784 just read this: `Child Pid 744 sends some more'<br><br></blockquote></div></div><br></div></div><br>
<hr size=1><a href="http://us.rd.yahoo.com/evt=47519/*http://tc.deals.yahoo.com/tc/blockbuster/text1.com
">No Cost - Get a month of Blockbuster Total Access</a> now. Sweet deal for Yahoo! users and friends.</body></html>