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

Hi,

I'm trying to write a perl script that will execute 2 other perl scripts simultaneously with 2 separate processors in unix. Here's an incredibly simple example that, er, isn't working quite right...

#!usr/local/bin/perl #Usage: getslack.p file1 file2 if (fork()==0) { print "child1\n"; system ("scrub.ast.p $ARGV[0]"); exit; } if (fork()==0) { print "child2\n"; system ("scrub.pt.p $ARGV[1]"); exit; } wait; wait; print "Done!\n";
Each of the srub.*.p scripts reads in a single (c. 200 Meg) file and writes out a couple of small summary files, with all files ultimately referenced with a close().

Currently, both "child1" and "child2" are printing to the screen, and the *ast.p output looks good, but the *pt.p process never shows up in a cpu. (Both scripts work fine when run independently.) I ultimately get a "Done!" and the *ast.p results.

I would appreciate any hints! Some of the other fork examples I've seen here are far more complicated that what I need to do.

Thank You.

Edit by tye, remove BR, add CODE

Replies are listed 'Best First'.
Re: How to use multiple processors with fork in unix?
by Abigail-II (Bishop) on May 14, 2003 at 02:22 UTC
    Ehm, you ask in your subject a different question than in your post itself. First, the question in your title. The way to create new processes in Unix is fork. Nothing else. On which processor that new process is going to be run (and when) is the task of the scheduler, and not Perl. Different OSses have different schedulers.

    As for your other question, there's not much I can say. At first glance, I'd expect the problem to be with the srub.pt.p process, or how you call it. Why aren't you checking $?, or the return value of system? What happens if you let both children execute srub.ast.p? Could it be that @ARGV contains characters that are special for the shell? (Perhaps you should use the list form of system).

    Abigail

Re: How to use multiple processors with fork in unix?
by Skeeve (Parson) on May 14, 2003 at 08:37 UTC
    I've changed your code to read:
    if (fork()==0) { print "child1\n"; system ("ls -al $ARGV[0]"); exit; } if (fork()==0) { print "child2\n"; system ("df -k $ARGV[1]"); exit; } wait; wait; print "Done!\n";
    and it works fine. So your problem might be one or both of:
    1. The programs you call don't work as you think they should.
    2. You can't seperate the ouput by looking at it. The lines of output usually are mixed.