[Detroit-pm] Trying to stick a fork in it!
Scott Webster Wood
treii28 at yahoo.com
Fri Mar 28 14:20:55 PDT 2008
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.
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)
--------------------------- cut here -----------------------------
#!/usr/bin/perl
use Socket;
use IO::Handle;
my (@c, at p, at pid); # c=child handle, p=parent handle, pid=process ID
for(my $s=0; $s<5; $s++) {
$c[$s] = new IO::Handle;
$p[$s] = new IO::Handle;
socketpair($c[$s], $p[$s], AF_UNIX, SOCK_STREAM, PF_UNSPEC)
or die "socketpair: $!";
$c[$s]->autoflush(1);
$p[$s]->autoflush(1);
if ($pid[$s] = fork()) { # is the parent
close $p[$s]; # close unused parent stream
&parent($c[$s], $s);
close $c[$s]; # close the stream coming back from the child
waitpid($pid[$s],0); # wait for child process to terminate
} else { # is the child
die "cannot fork: $!" unless defined $pid[$s]; # make sure pid == 0
close $c[$s]; # close the unused child stream
&child($p[$s], $s);
close $p[$s]; # close stream that was talking to the parent
exit(0);
}
}
sub parent() { # read data coming from child process(es)
my $fh = shift;
my $s = shift;
while ($line = <$fh>) { # read data from child line by line until done
chomp($line);
print "Parent $s Pid $$ just read this: `$line'\n";
};
}
sub child() { # send data back to parent through filehandle
my $fh = shift;
print $fh "Child Pid $$ is sending this\n";
print $fh "Child Pid $$ sends some more\n";
}
# filehandles didn't behave well using array references and my ultimate solution
# is going to create functions to perform tasks anyway so I just passed the
# handles to subroutines
-------------- cut ------------
$ ./test.pl
Parent 0 Pid 25960 just read this: `Child Pid 25961 is sending this'
Parent 0 Pid 25960 just read this: `Child Pid 25961 sends some more'
Parent 1 Pid 25960 just read this: `Child Pid 25962 is sending this'
Parent 1 Pid 25960 just read this: `Child Pid 25962 sends some more'
Parent 2 Pid 25960 just read this: `Child Pid 25963 is sending this'
Parent 2 Pid 25960 just read this: `Child Pid 25963 sends some more'
Parent 3 Pid 25960 just read this: `Child Pid 25964 is sending this'
Parent 3 Pid 25960 just read this: `Child Pid 25964 sends some more'
Parent 4 Pid 25960 just read this: `Child Pid 25965 is sending this'
Parent 4 Pid 25960 just read this: `Child Pid 25965 sends some more'
____________________________________________________________________________________
Be a better friend, newshound, and
know-it-all with Yahoo! Mobile. Try it now. http://mobile.yahoo.com/;_ylt=Ahu06i62sR8HDtDypao8Wcj9tAcJ
More information about the Detroit-pm
mailing list