Bash`wait`命令,等待超过1个PID完成执行 [英] Bash `wait` command, waiting for more than 1 PID to finish execution

查看:395
本文介绍了Bash`wait`命令,等待超过1个PID完成执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近发布了一个问题,询问是否可以

I recently posted a question asking if it was possible to prevent PID's from being re-used.

到目前为止,答案似乎是否定的. (那很好.)

So far the answer appears to be no. (Which is fine.)

但是,用户 Diego Torres Milano 为该问题添加了答案,我的问题是关于该答案.

However, the user Diego Torres Milano added an answer to that question, and my question here is in regards to that answer.

迭戈回答了,

如果您担心重用PID,则等待 其他答案说明,您可以使用

If you are afraid of reusing PID's, which won't happen if you wait as other answers explain, you can use

echo 4194303 > /proc/sys/kernel/pid_max

减少恐惧感;-)

我实际上不明白为什么迭戈在这里使用数字4194303,但这是另一个问题.

I don't actually understand why Diego has used the number 4194303 here, but that's another question.

我的理解是我的以下代码有问题:

My understanding was that I had a problem with the following code:

for pid in "${PIDS[@]}"
do
    wait $pid
done

问题是我在一个数组中有多个PID,并且for循环将对数组中的每个PID依次运行wait命令,但是我无法预测进程将以与它们相同的顺序完成PID存储在此数组中.

The problem being that I have multiple PIDs in an array, and that the for loop will run the wait command sequentially with each PID in the array, however I cannot predict that the processes will finish in the same order that their PIDs are stored in this array.

ie;可能发生以下 :

ie; the following could happen:

  • 开始等待数组索引0中的PID
  • 数组索引1中具有PID的进程终止
  • 新作业在系统上运行,导致存储在PID数组索引1中的PID 重新用于其他进程
  • wait随着数组索引0中的PID退出而终止
  • 开始等待数组索引0中的PID,除了现在这是一个不同的过程,我们不知道它是什么
  • 重复使用wait当前正在等待 的PID的运行过程终止.也许是邮件服务器的PID或系统管理员已启动的东西.
  • wait一直等待,直到找到下一个严重的Linux错误并重新启动系统或断电为止.
  • Start waiting for PID in array index 0
  • Process with PID in index 1 of array terminates
  • New job(s) run on system, resulting in PID which is stored in index 1 of PID array being reused for another process
  • wait terminates as PID in array index 0 exits
  • Start waiting for PID in array index 0, except this is now a different process and we have no idea what it is
  • The process which was run which re-used the PID which wait is currently waiting for never terminates. Perhaps it is the PID of a mail server or something which a system admin has started.
  • wait keeps waiting until the next serious linux bug is found and the system is rebooted or there is a power outage

迭戈说:

如果您等待其他答案解释,则不会发生

which won't happen if you wait as other answers explain

ie;我上面描述的情况不会发生.

ie; that the situation I have described above cannot happen.

迭戈正确吗?

  • 如果是这样,为什么我上面描述的情况不会发生?

还是迭戈不正确?

  • 如果可以,那么我今天晚些时候发布一个新问题...

在我看来,这个问题可能令人困惑,除非您知道PID是后台启动的进程的PID.即;

It has occured to me that this question might be confusing, unless you are aware that the PID's are PID's of processes launched in the background. ie;

my_function &
PID="$!"
PIDS+=($PID)

推荐答案

让我们看看您的选择.

for i in 1 2 3 4 5; do
    cmd &
done
wait

这样做的好处是简单,但是您不能使机器忙碌.如果要在旧工作完成后开始新工作,则不能.在完成所有后台作业之前,您的机器利用率越来越低,此时您可以开始一批新的作业.

This has the benefit of being simple, but you can't keep your machine busy. If you want to start new jobs as old ones complete, you can't. You machine gets less and less utilized until all the background jobs complete, at which point you can start a new batch of jobs.

相关的功能是通过将多个参数传递给wait来等待一部分作业的能力:

Related is the ability to wait for a subset of jobs by passing multiple arguments to wait:

unrelated_job &
for i in 1 2 3 4 5; do
  cmd & pids+=($!)
done
wait "${pids[@]}"   # Does not wait for unrelated_job, though

以任意顺序等待单个作业

for i in 1 2 3 4 5; do
   cmd & pids+=($!)
done

for pid in "${pids[@]}"; do
   wait "$pid"
   # do something when a job completes
done

这样做的好处是可以让您在工作完成后进行工作,但是 仍然存在一个问题,即除$pid以外的其他作业 可能会先完成,从而使您的计算机未得到充分利用,直到$pid实际完成.但是,即使确实在等待之前完成,您仍然可以获得每个作业的退出状态.

This has the benefit of letting you do work after a job completes, but still has the problem that jobs other than $pid might complete first, leaving your machine underutilized until $pid actually completes. You do, however, still get the exit status for each individual job, even if it completes before you actually wait for it.

for i in 1 2 3 4 5; do
   cmd & pids+=($!)
done

for pid in "${pids[@]}"; do
   wait -n
   # do something when a job completes
done

在这里,您可以等到 a 作业完成,这意味着您可以使计算机尽可能保持繁忙.唯一的问题是,您不必知道哪个作业就可以完成,而无需使用jobs来获取活动进程的列表并将其与pids进行比较.

Here, you can wait until a job completes, which means you can keep your machine as busy as possible. The only problem is, you don't necessarily know which job completed, without using jobs to get the list of active processes and comparing it to pids.

shell本身并不是进行作业分配的理想平台,这就是为什么要设计许多用于管理批处理作业的程序的原因:xargsparallelslurmqsub等.

The shell by itself is not an ideal platform for doing job distribution, which is why there are a multitude of programs designed for managing batch jobs: xargs, parallel, slurm, qsub, etc.

这篇关于Bash`wait`命令,等待超过1个PID完成执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆