Laz*_*zer 19 unix parallel-processing shell csh
我有一个C shell脚本,它执行以下操作:
#!/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
Run Code Online (Sandbox Code Playgroud)
有一些办法可以让线1,2并3在运行并行而不是一个接另一个?
Zan*_*ynx 13
将其转换为具有适当依赖关系的Makefile.然后你可以使用make -j让并行运行所有可能的东西.
请注意,Makefile中的所有缩进都必须是TAB.TAB显示Make运行命令的位置.
另请注意,此Makefile现在使用GNU Make扩展(通配符和子函数).
它可能看起来像这样:
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 $< $@
Run Code Online (Sandbox Code Playgroud)
在bash我会做;
ex file1 r1 &
ex file2 r2 &
ex file3 r3 &
wait
... continue with script...
Run Code Online (Sandbox Code Playgroud)
并将它们产生并行运行.您可以查看此SO线程以获取另一个示例.
#!/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
Run Code Online (Sandbox Code Playgroud)
我稍微修改了代码以使用大括号扩展{1..3}而不是硬编码数字,因为我刚刚意识到您说的是文件比3还要多。大括号扩展通过将大括号内的'3'替换为任意数字而使缩放到大数字变得微不足道。你需要。