如何在循环中运行固定数量的进程? [英] How to run a fixed number of processes in a loop?
问题描述
我有一个像这样的脚本:
I have a script like this:
#!/bin/bash
for i=1 to 200000
do
create input file
run ./java
done
我需要同时运行多个(8或16个)进程(java),但我不知道如何运行.我知道wait
可以帮助您,但它应该一直运行8个进程,而不要等前8个进程完成后再启动其他8个进程.
I need to run a number (8 or 16) of processes (java) at the same time and I don't know how. I know that wait
could help but it should be running 8 processes all the time and not wait for the first 8 to finish before starting the other 8.
推荐答案
bash
4.3在wait
命令-n
中添加了一个有用的新标志,这会导致wait
阻塞直到 any 完成单个后台作业,而不仅仅是给定子集(或全部)的成员.
bash
4.3 added a useful new flag to the wait
command, -n
, which causes wait
to block until any single background job, not just the members of a given subset (or all), to complete.
#!/bin/bash
cores=8 # or 16, or whatever
for ((i=1; i <= 200000; i++))
do
# create input file and run java in the background.
./java &
# Check how many background jobs there are, and if it
# is equal to the number of cores, wait for anyone to
# finish before continuing.
background=( $(jobs -p) )
if (( ${#background[@]} == cores )); then
wait -n
fi
done
有一个小的竞争条件:如果您处于最大负载,但是在运行jobs -p
后完成了一项任务,那么您仍然会阻塞,直到有另一项任务为止
完成.您对此无能为力,但是在实践中它不应该带来太多麻烦.
There is a small race condition: if you are at maximum load but a job completes after you run jobs -p
, you'll still block until another job
completes. There's not much you can do about this, but it shouldn't present too much trouble in practice.
在bash
4.3之前,您需要定期轮询一组后台作业,以查看池何时降至阈值以下.
Prior to bash
4.3, you would need to poll the set of background jobs periodically to see when the pool dropped below your threshold.
while :; do
background=( $(jobs -p))
if (( ${#background[@]} < cores )); then
break
fi
sleep 1
done
这篇关于如何在循环中运行固定数量的进程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!