Guest User

Untitled

a guest
Jun 19th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.96 KB | None | 0 0
  1. use strict;
  2. use warnings;
  3. use Parallel::ForkManager;
  4.  
  5. use constant MAX_CHILDREN => 3;
  6.  
  7. {
  8. my $pm = Parallel::ForkManager->new(MAX_CHILDREN);
  9. my $pid;
  10.  
  11. my @array1 = (
  12. 'The','silent','witness',
  13. );
  14.  
  15. my $index = 0;
  16.  
  17. pipe(FROM_CHILD,TO_PARENT) or print $!."\n";
  18. pipe(FROM_PARENT,TO_CHILD) or print $!."\n";
  19. select((select(TO_CHILD),$|++)[0]);
  20. select((select(TO_PARENT),$|++)[0]);
  21.  
  22. for(my $i =0; $i <3; $i++){
  23. $pid = $pm->start() and next;
  24. print $array1[$index++%scalar @array1]."\n";
  25. $pm->finish();
  26. }
  27. $pm->wait_all_children;
  28. if($pid = $pm){
  29. print TO_CHILD "$index\n";
  30. chomp(my $child_says = <FROM_CHILD>);
  31. $index = $child_says;
  32. }else{
  33.  
  34. chomp(my $parent_says = <FROM_PARENT>);
  35. }
  36.  
  37. close(FROM_CHILD);close(FROM_PARENT);
  38. close(TO_CHILD); close(TO_PARENT);
  39. }
  40.  
  41. OUTPUT
  42.  
  43. The
  44. The
  45. The
  46. (then it hangs)
  47.  
  48. Was hoping $index would be passed from child to parent and vice versa so it should print:
  49. The
  50. silent
  51. witness
Add Comment
Please, Sign In to add comment