in reply to basic fork question

While ^gav's response is right on the money, You might want to try out some experiements with forking on your own before falling back on the module. Have you read the "fork" description in the camel book or the perlfunc doc's? If you are just getting started on understanding how they work you might start there.

To answer your questions directly:

1. You want to check for $pid before forking

# mostly from the camel book if ($pid = fork) { .... #parent } elsif (defined $pid) { ... #child exit; }

But, if your forking creates thousands of processes at once you will likely overload your system and get locked out. As long as you have an exit in the child, you should be okay as far as this is concerned.

2. To make the parent wait until all the child are finished you use a sigalert. I am not an expert on the syntax here, maybe others can add to that, but basically you can set an alert to have the parent wait until the child sends a signal to continue.

3. I am not sure I understand your third question. Store what? The array? The parent already has the array. Can you clarify?

UPDATE: Marcello is right, of course. I didn't mean my code to be usable but just a cursory example. I hope I didn't mislead anyone.


I admit it, I am Paco.

Replies are listed 'Best First'.
Re: Re: basic fork question
by Marcello (Hermit) on Jan 19, 2002 at 21:11 UTC
    I would like to suggest a better way of handling a fork:
    if (!defined($pid = fork())) { # FAILURE # parent process } elsif ($pid == 0) { # SUCCESS # child process } else { # SUCCESS # parent process }
    This one is more fail-safe, since you omitted the situation of a fork() failure.
A reply falls below the community's threshold of quality. You may see it by logging in.