I've never actually done any forking in Perl and was curious, so thanks for posting the code. I wouldn't mind looking at the two-directional code as well, if it's no trouble. <br><br>- Mike<br><br><div class="gmail_quote">
On Fri, Mar 28, 2008 at 5:20 PM, Scott Webster Wood <<a href="mailto:treii28@yahoo.com">treii28@yahoo.com</a>> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
OK, I briefly tried threading with some success until I found out the final tool was going to be on a machine running a pre-5.8 version of perl so I went back to pipes and sockets and here's what I came up with in case anyone is curious.<br>
<br>
I stripped out the bidirectional communication as I'm only talking from-child-to-parent but if anyone wants to see the two-directional code, just let me know (basically just imagine adding the code after the variable initialization from 'sub parent' to 'sub child' and vice versa and changing the verbage)<br>
<br>
<br>
--------------------------- 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>
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>
}<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>
$ ./test.pl<br>
Parent 0 Pid 25960 just read this: `Child Pid 25961 is sending this'<br>
Parent 0 Pid 25960 just read this: `Child Pid 25961 sends some more'<br>
Parent 1 Pid 25960 just read this: `Child Pid 25962 is sending this'<br>
Parent 1 Pid 25960 just read this: `Child Pid 25962 sends some more'<br>
Parent 2 Pid 25960 just read this: `Child Pid 25963 is sending this'<br>
Parent 2 Pid 25960 just read this: `Child Pid 25963 sends some more'<br>
Parent 3 Pid 25960 just read this: `Child Pid 25964 is sending this'<br>
Parent 3 Pid 25960 just read this: `Child Pid 25964 sends some more'<br>
Parent 4 Pid 25960 just read this: `Child Pid 25965 is sending this'<br>
Parent 4 Pid 25960 just read this: `Child Pid 25965 sends some more'<br>
<div><div></div><div class="Wj3C7c"><br>
<br>
<br>
<br>
____________________________________________________________________________________<br>
Be a better friend, newshound, and<br>
know-it-all with Yahoo! Mobile. Try it now. <a href="http://mobile.yahoo.com/;_ylt=Ahu06i62sR8HDtDypao8Wcj9tAcJ" target="_blank">http://mobile.yahoo.com/;_ylt=Ahu06i62sR8HDtDypao8Wcj9tAcJ</a><br>
_______________________________________________<br>
Detroit-pm mailing list<br>
<a href="mailto:Detroit-pm@pm.org">Detroit-pm@pm.org</a><br>
<a href="http://mail.pm.org/mailman/listinfo/detroit-pm" target="_blank">http://mail.pm.org/mailman/listinfo/detroit-pm</a><br>
</div></div></blockquote></div><br>