in reply to Re^2: Stop fork in Parallel::ForkManager
in thread Stop fork in Parallel::ForkManager

You answered my question perfectly (I confess I'm new to the IPC game; actually I started reading that part of the Llama today). Good links. :)

In regards to the original question...I found this code on wikipedia, and it seems to do what you want(?).

#!/usr/bin/perl $pid = fork(); #Declare fork if ($pid == 0) { #Jump into the Child process for ($j = 0; $j < 10; $j++) { print "child: $j\n"; sleep 1; } exit(0); #Exit the fork [child process] } elsif ($pid > 0) { for ($i = 0; $i < 10; $i++) { print "parent: $i\n"; sleep 1; } exit(0); #Exit parent }

So the thing here is to test the pid. In your code, you would say:

$pm->finish if ($pid == 0 && $content =~ m/string/); #...no match, do more work... $pm->finish;

Or am I totally missing the boat here (likely)? :)

Replies are listed 'Best First'.
Re^4: Stop fork in Parallel::ForkManager
by casimo (Sexton) on Sep 27, 2009 at 23:50 UTC
    Thanks for all the replies

    Urthas, you did not miss the boat here because your code looks to be a crude version of what I am looking for and I'm thinking a more elegant solution doesn't exist.

    the reason I call your code crude is because, while it works, it kills off each child one-by one instead of killing the remaining children all at once. I guess I was looking for a bomb ...but bullets will do

    thank you
Re^4: Stop fork in Parallel::ForkManager
by casimo (Sexton) on Sep 28, 2009 at 16:33 UTC
    thanks for the replies.

    Urthas your solution is usable and I appreciate it.
Re^4: Stop fork in Parallel::ForkManager
by sierpinski (Chaplain) on Sep 30, 2009 at 12:26 UTC
    You can also supply $pm->finish() with a parameter. I use it on my monitor scripts to keep track of how many successful connections to servers I've made. I return 0 for failure and 1 for success, so I just add them up and get 'X out of Y servers'. Works perfectly.

    I don't know if you can return more than a scalar offhand, but if you could return any type of variable (array/hash/etc), it could be very useful for communicating between a child and parent process. It just doesn't help at all with child->child communication.
    /\ Sierpinski
      I don't know if you can return more than a scalar offhand...

      Only an integer, since it is exit value. See forks :)