Take the 2-minute tour ×

I have a C shell script that does something like this:

gcc example.c -o ex
gcc combine.c -o combine
ex file1 r1     <-- 1
ex file2 r2     <-- 2
ex file3 r3     <-- 3
#... many more like the above
combine r1 r2 r3 final
\rm r1 r2 r3

Is there some way I can make lines 1, 2 and 3 run in parallel instead of one after the another?

share|improve this question
add comment

6 Answers

up vote 11 down vote accepted

Convert this into a Makefile with proper dependencies. Then you can use make -j to have Make run everything possible in parallel.

Note that all the indents in a Makefile must be TABs. TAB shows Make where the commands to run are.

Also note that this Makefile is now using GNU Make extensions (the wildcard and subst functions).

It might look like this:

export PATH := .:${PATH}

FILES=$(wildcard file*)
RFILES=$(subst file,r,${FILES})

final: combine ${RFILES}
    combine ${RFILES} final
    rm ${RFILES}

ex: example.c

combine: combine.c

r%: file% ex
    ex $< $@
share|improve this answer
Your -l combine should be -o combine –  SiegeX May 7 '10 at 19:39
Would you put the final rule first to make it the default? –  glenn jackman May 7 '10 at 19:41
What was I thinking? Fixed. –  Zan Lynx May 7 '10 at 23:06
I like your answer but I just realized his question states more than just 3 possible files and this doesn't seem to scale all that well. –  SiegeX May 7 '10 at 23:43
@SiegeX: It is very possible to do wildcards in a Makefile. Then it could process every file* in the directory. –  Zan Lynx May 9 '10 at 15:59
show 1 more comment

In bash I would do;

ex file1 r1  &
ex file2 r2  &
ex file3 r3  &
... continue with script...

and spawn them out to run in parallel. You can check out this SO thread for another example.

share|improve this answer
add comment

gcc example.c -o ex
gcc combine.c -o combine

# Call 'ex' 3 times in "parallel"
for i in {1..3}; do
  ex file${i} r${i} &

#Wait for all background processes to finish

# Combine & remove
combine r1 r2 r3 final
rm r1 r2 r3

I slightly altered the code to use brace expansion {1..3} rather than hard code the numbers since I just realized you said there are many more files than just 3. Brace expansion makes scaling to larger numbers trivial by replacing the '3' inside the braces to whatever number you need.

share|improve this answer
add comment

you can use cmd & and wait after

echo start
sleep 1 &
sleep 1 &
sleep 1 &
echo ok


$ time ./csh.sh 
[1] 11535
[2] 11536
[3] 11537
[3]    Done                   sleep 1
[2]  - Done                   sleep 1
[1]  + Done                   sleep 1

real    0m1.008s
user    0m0.004s
sys 0m0.008s
share|improve this answer
add comment

GNU Parallel would make it pretty like:

seq 1 3 | parallel ex file{} r{}

Depending on how 'ex' and 'combine' work you can even do:

seq 1 3 | parallel ex file{} | combine

Learn more about GNU Parallel by watching http://www.youtube.com/watch?v=LlXDtd_pRaY

share|improve this answer
add comment

You could use nohup ex :

nohup ex file1 r1 &    
nohup ex file2 r2 &
nohup ex file3 r3 &
share|improve this answer
add comment

Your Answer


