问题
I have a C shell script that does something like this:
#!/bin/csh
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?
回答1:
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 $< $@
回答2:
In bash I would do;
ex file1 r1 &
ex file2 r2 &
ex file3 r3 &
wait
... continue with script...
and spawn them out to run in parallel. You can check out this SO thread for another example.
回答3:
#!/bin/bash
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} &
done
#Wait for all background processes to finish
wait
# 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.
回答4:
you can use cmd & and wait after
#!/bin/csh echo start sleep 1 & sleep 1 & sleep 1 & wait echo ok
test:
$ time ./csh.sh start [1] 11535 [2] 11536 [3] 11537 [3] Done sleep 1 [2] - Done sleep 1 [1] + Done sleep 1 ok real 0m1.008s user 0m0.004s sys 0m0.008s
回答5:
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
回答6:
You could use nohup ex :
nohup ex file1 r1 &
nohup ex file2 r2 &
nohup ex file3 r3 &
回答7:
xargs
can do it:
seq 1 3 | xargs -n 1 -P 0 -I % ex file% r%
-n 1
is for "one line per input", -P
is for "run each line in parallel"
来源:https://stackoverflow.com/questions/2791069/how-to-use-parallel-execution-in-a-shell-script