[Detroit-pm] Trying to stick a fork in it!

Scott Webster Wood treii28 at yahoo.com
Sat Mar 29 16:57:41 PDT 2008


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.

SW

----- Original Message ----
From: Mike Ward <unforgiven24 at gmail.com>

I wouldn't mind looking at the two-directional code as well, if it's no trouble. 

--------------------------- 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;
  print $fh "Parent Pid $$ is sending this\n";
  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";
 chomp($line = <$fh>);
 print "Child $s Pid $$ just read this: `$line'\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 ------------
bash-3.2$ ./test.pl 
Parent 0 Pid 3784 just read this: `Child Pid 3800 is sending this'
Parent 0 Pid 3784 just read this: `Child Pid 3800 sends some more'
Child  Pid 3800 just read this: `Parent Pid 3784 is sending this'
Child  Pid 3808 just read this: `Parent Pid 3784 is sending this'
Parent 1 Pid 3784 just read this: `Child Pid 3808 is sending this'
Parent 1 Pid 3784 just read this: `Child Pid 3808 sends some more'
Parent 2 Pid 3784 just read this: `Child Pid 3288 is sending this'
Parent 2 Pid 3784 just read this: `Child Pid 3288 sends some more'
Child  Pid 3288 just read this: `Parent Pid 3784 is sending this'
Parent 3 Pid 3784 just read this: `Child Pid 2956 is sending this'
Parent 3 Pid 3784 just read this: `Child Pid 2956 sends some more'
Child  Pid 2956 just read this: `Parent Pid 3784 is sending this'
Child  Pid 744 just read this: `Parent Pid 3784 is sending this'
Parent 4 Pid 3784 just read this: `Child Pid 744 is sending this'
Parent 4 Pid 3784 just read this: `Child Pid 744 sends some more'








      ____________________________________________________________________________________
You rock. That's why Blockbuster's offering you one month of Blockbuster Total Access, No Cost.  
http://tc.deals.yahoo.com/tc/blockbuster/text5.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.pm.org/pipermail/detroit-pm/attachments/20080329/77d7a44f/attachment.html 


More information about the Detroit-pm mailing list