Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Is there a way of launching multiple concurrent processes in perl without waiting on either to complete first? Basically I want to launch 3 concurrent processes from within a perl script and in doing so the openmosix cluster will then hopefully loadbalance each of these processes. But I am having trouble getting perl to launch concurrent processes. I am sure someone must have done this type of stuff before. Thanks

Replies are listed 'Best First'.
Re: launching concurrent processes
by polettix (Vicar) on Mar 30, 2005 at 15:11 UTC
    What did you try exactly? Anyway, try to look at perldoc -f fork and perldoc -f exec.

    Flavio

    Don't fool yourself.
      When using fork and exec, is there a way of not waiting for the child to complete? What I mean is that have fork create 3 clones of itself and not single or not wait for the child to finish and fork another child process and then round them up in the end.
        I really find it difficult to understand what you're asking, sorry for this.

        When fork() is called, you end up having two processes: the parent and the child. They both execute the same code (your Perl script), but you can understand if you're in the child testing whether the call returned 0. Then, you're supposed to call exec() (if you really need it) passing the control to the process you want to execute, but this isn't necessary if the code is in the parent script!

        The two processes created by fork() are independent, so they will be both running after the call, if this is what you're worried about. Unlike system(), which waits for the child process to complete, after fork() you can do whatever you want in the parent process, even calling fork() two more times to complete the creation of your three subprocesses.

        Simple snippet:

        #!/usr/bin/perl use strict; use warnings; sub child { print("Hey, I'm child $$\n"); sleep(2 + rand(5)); print("$$ exiting...\n"); exit(0); } foreach (1 .. 3) { my $pid = fork; die "fork(): $@, stopped" unless defined($pid); child() unless $pid; } # Only the parent reaches this point wait foreach (1 .. 3);
        which yelds, for example
        Hey, I'm child 1048 Hey, I'm child 1368 Hey, I'm child 424 1368 exiting... 424 exiting... 1048 exiting...

        Flavio

        Don't fool yourself.
Re: launching concurrent processes
by RazorbladeBidet (Friar) on Mar 30, 2005 at 15:12 UTC
    See perlfork, fork, perlipc
    --------------
    "But what of all those sweet words you spoke in private?"
    "Oh that's just what we call pillow talk, baby, that's all."
Re: launching concurrent processes
by FitTrend (Pilgrim) on Mar 30, 2005 at 15:17 UTC
Re: launching concurrent processes
by Anonymous Monk on Mar 31, 2005 at 14:41 UTC
    It's so simple it is not documented clearly. That once had me stumped for half a day, too, until a shell scripter pointed out the obvious:
    system "task1 &"; system "task2 &"; system "task3 &";
    Doing something sensible with the forked off STDOUTs is left as exercise. (Hehe, I always wanted to use that expression.)