I&#39;ve never actually done any forking in Perl and was curious, so thanks for posting the code. I wouldn&#39;t mind looking at the two-directional code as well, if it&#39;s no trouble. <br><br>- Mike<br><br><div class="gmail_quote">
On Fri, Mar 28, 2008 at 5:20 PM, Scott Webster Wood &lt;<a href="mailto:treii28@yahoo.com">treii28@yahoo.com</a>&gt; 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&#39;s what I came up with in case anyone is curious.<br>

<br>
I stripped out the bidirectional communication as I&#39;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 &#39;sub parent&#39; to &#39;sub child&#39; 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&lt;5; $s++) {<br>
 &nbsp;$c[$s] = new IO::Handle;<br>
 &nbsp;$p[$s] = new IO::Handle;<br>
<br>
 &nbsp;socketpair($c[$s], $p[$s], AF_UNIX, SOCK_STREAM, PF_UNSPEC)<br>
 &nbsp; &nbsp;or &nbsp;die &quot;socketpair: $!&quot;;<br>
<br>
 &nbsp;$c[$s]-&gt;autoflush(1);<br>
 &nbsp;$p[$s]-&gt;autoflush(1);<br>
<br>
<br>
 &nbsp;if ($pid[$s] = fork()) { # is the parent<br>
 &nbsp; &nbsp;close $p[$s]; &nbsp; &nbsp; &nbsp; &nbsp;# close unused parent stream<br>
 &nbsp; &nbsp;&amp;parent($c[$s], $s);<br>
 &nbsp; &nbsp;close $c[$s]; &nbsp; &nbsp; &nbsp; &nbsp;# close the stream coming back from the child<br>
 &nbsp; &nbsp;waitpid($pid[$s],0); # wait for child process to terminate<br>
 &nbsp;} else { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # is the child<br>
 &nbsp; &nbsp;die &quot;cannot fork: $!&quot; unless defined $pid[$s]; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# make sure pid == 0<br>
 &nbsp; &nbsp;close $c[$s]; &nbsp; &nbsp; &nbsp; &nbsp;# close the unused child stream<br>
 &nbsp; &nbsp;&amp;child($p[$s], $s);<br>
 &nbsp; &nbsp;close $p[$s]; &nbsp; &nbsp; &nbsp; &nbsp;# close stream that was talking to the parent<br>
 &nbsp; &nbsp;exit(0);<br>
 &nbsp;}<br>
}<br>
<br>
sub parent() { # read data coming from child process(es)<br>
 &nbsp;my $fh = shift;<br>
 &nbsp;my $s &nbsp;= shift;<br>
 &nbsp;while ($line = &lt;$fh&gt;) { # read data from child line by line until done<br>
 &nbsp; &nbsp;chomp($line);<br>
 &nbsp; &nbsp;print &quot;Parent $s Pid $$ just read this: `$line&#39;\n&quot;;<br>
 &nbsp;};<br>
}<br>
<br>
sub child() { # send data back to parent through filehandle<br>
 &nbsp;my $fh = shift;<br>
 &nbsp;print $fh &quot;Child Pid $$ is sending this\n&quot;;<br>
 &nbsp;print $fh &quot;Child Pid $$ sends some more\n&quot;;<br>
}<br>
<br>
# filehandles didn&#39;t behave well using array references and my ultimate solution<br>
# &nbsp;is going to create functions to perform tasks anyway so I just passed the<br>
# &nbsp;handles to subroutines<br>
<br>
-------------- cut ------------<br>
$ ./test.pl<br>
Parent 0 Pid 25960 just read this: `Child Pid 25961 is sending this&#39;<br>
Parent 0 Pid 25960 just read this: `Child Pid 25961 sends some more&#39;<br>
Parent 1 Pid 25960 just read this: `Child Pid 25962 is sending this&#39;<br>
Parent 1 Pid 25960 just read this: `Child Pid 25962 sends some more&#39;<br>
Parent 2 Pid 25960 just read this: `Child Pid 25963 is sending this&#39;<br>
Parent 2 Pid 25960 just read this: `Child Pid 25963 sends some more&#39;<br>
Parent 3 Pid 25960 just read this: `Child Pid 25964 is sending this&#39;<br>
Parent 3 Pid 25960 just read this: `Child Pid 25964 sends some more&#39;<br>
Parent 4 Pid 25960 just read this: `Child Pid 25965 is sending this&#39;<br>
Parent 4 Pid 25960 just read this: `Child Pid 25965 sends some more&#39;<br>
<div><div></div><div class="Wj3C7c"><br>
<br>
<br>
<br>
 &nbsp; &nbsp; &nbsp;____________________________________________________________________________________<br>
Be a better friend, newshound, and<br>
know-it-all with Yahoo! Mobile. &nbsp;Try it now. &nbsp;<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>