0

PID と親/子プロセスの使用について少し混乱しています。私はそれらを読んでいて、プログラムが開始されるとそれ自体(子)の正確なコピーを作成し、それぞれが一意のPIDを持っているという事実を理解しています。しかし、シェルでそれを使用して、そのシェルプログラムの特定の側面がいつ終了したかを通知できるかどうかはわかりません。

より良い例 (疑似コード):

 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 に答える