in reply to Is fork() my answer to using dual-cpu?

fork is the easy answer, but your forking loop isn't quite right. You will always end up spawning two processes, because both the parent and the child exec within the loop. Try
for my $x (1..$total_number_of_runs) { my $pid = fork; if (not defined $pid) { die("Unable to create child process: $!\n"); } exec("shell_script < $input_file") unless $pid; #thanks, bluto }
The parent launches all the children, then exits.

Caution: Contents may have been coded under pressure.

Replies are listed 'Best First'.
Re^2: Is fork() my answer to using dual-cpu?
by bluto (Curate) on Oct 25, 2005 at 19:12 UTC
    You'll want that exec to be something like...

    exec("shell_script < $input_file") unless $pid;

    ...since otherwise the parent will exec as well. FWIW, when I use fork() most of the time I write it in the following "template", comments and all ...

    my $pid = fork(); die "failed to fork: $!" unless defined $pid; unless ($pid) { # child die; } # parent

    ... and then fill in the code later. That way it's harder for me to mess it up. :-)