0

我对 PID 和父/子进程的使用有点困惑。我一直在阅读它们,并且据我所知,当程序启动时,它会复制自己(孩子)的精确副本,并且每个程序都有唯一的 PID。但我不确定是否可以在 shell 中使用它来告诉我该 shell 程序的某些方面何时完成。

举一个更好的例子(伪代码):

 for ((i = 0; i < 10; i++))
  for a_name in "${anArray[@]}";do
     a series of math equations that allow the values associated with a_name to run in the background simultaneously using '&' earlier in the code         
  done
wait

  for a_name in "${anArray[@]}";do
     same as above but diff equations
  done
wait
done

我希望能够看到命令中任何给定值何时完成,以便该特定值可以移动到下一个 for 循环和命令。

我已经看到 wait 可以将作业标识符作为参数(wait%1 或 wait $PPID),但我不确定这些将如何实现。

有没有人有关于如何使用 PID 的建议和/或有一个超级好的教程的链接?(我的意思是超级好,我需要一些外行的术语)

谢谢!

4

1 回答 1

1

您可以等待一个进程:

command ${array[0]} &
waiton=$!
# Do some more stuff which might finish before process $waiton
wait $waiton

您可以等待所有子进程:

someLongRunningCommand &
(
    for value in "${array[@]}"; do
        command "$value" &
    done
    wait
)
# wait with no arguments waits on all children processes of the current
# process. That doesn't include `someLongRunningCommand`, as it is not
# a child of the process running the subshell.

其他情况比较棘手,可以通过 , 或其他方法更好地xargs处理parallel

于 2012-07-17T16:15:30.477 回答