Shell,并行运行四个进程

问题描述:

目前我被困在高效运行耗时的模拟上.目的是并行运行 4 个模拟,因为它是一个单线程应用程序和一个四核系统.我必须修改 shell 脚本:

Currently I'm stuck at running time consuming simulations efficiently. The intention is to run 4 simulations in parallel, because it's a single thread application and a quad core system. I have to variations of shell script:

./sim -r 1 &
./sim -r 2 &
./sim -r 3 &
./sim -r 4 &
wait
./sim -r 5 &
./sim -r 6 &
./sim -r 7 &
./sim -r 8 &
wait
... (another 112 jobs) 

使用此代码会一次又一次地等待.我还尝试将任务分成四个脚本并分别运行,结果是一个脚本完成了,而另一个脚本还有大约 30% 的剩余作业.我无法预测模拟需要多长时间.

with this code there is a wait again and again. I also tried to split the tasks into four scripts and run each, result is that one script is finished while the other has about 30% of remaining jobs. I can't predict how long a simulation will take.

有什么建议可以随时运行 4 个模拟吗?

Any suggestion to have 4 simulations running at any time?

在 Ubuntu 中安装 moreutils 包,然后使用 parallel 实用程序:

Install the moreutils package in Ubuntu, then use the parallel utility:

parallel -j 4 ./sim -r -- 1 2 3 4 5 6 7 8 ...