Alternative to Perl fork()

My goal was to split an input list in N and run N processes in parallel. When all N processes end, I want to concatenate input and finish the program.
Easier than using Perl’s fork(), it is to use a Makefile:

Files a and b are used by make to know that the targets completed. You should use a random file name to avoid collision with other programs.

Save this file as myMakefile or any name you want:

# the main target all will wait for execution of target x
all: x

# target x will wait for execution of targets clean, a and b. x only finishes when files a and b are created
x: clean a b
        cat /tmp/output_a /tmp/output_b > /tmp/output

        rm -f a b

        sleep 2; echo "a"; touch /tmp/output_a; touch a; 

        sleep 6; echo "b"; touch /tmp/output_b; touch b;

The name of the targets (a and b in the example) has to match the name of filenames that you generate in each target when your programs finishes (in the example, “sleep/echo/touch /tmp”).

This is how you run your makefile, 2 jobs at a time:

make -f myMakefile -j 2

If you copy/paste the example above, you will need to make sure the spaces in front of the commands are 1 single tab. Make is very picky about this.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s