LoginSignup
1
0

More than 3 years have passed since last update.

kshで並行処理

Posted at

プログレス表示だったものを改造して並行処理にしてみた。

元ネタ(記憶で書いているので雰囲気のみ)

progress.ksh
#!/usr/bin/ksh
./heavy_process &
PID=$!
while ! ps -ef | grep -w ${PID} ; do
    echo ".\c"
    sleep 5
done
echo

改造あとの並行処理版。

parallel.ksh
#!/usr/bin/ksh93
typeset -A PID
cat some.list | while read S ; do
  ./some_process ${S} &
  PID[${S}]=$!
done
while [[ ${#PID[*]} -ne 0 ]] ; do
  for S in ${!PID[*]} ; do
    if ! ps -e | grep -w ${PID[${S}]} >/dev/null 2>&1 ; then
        unset PID[${S}]
    fi
  done
  sleep 5
  echo ".\c"
done
echo

some_processは引数に対してなにかやる感じ。
これであとは見た目を整えればOK。
ksh93を使っているのはハッシュが使えるからってだけで使用してます。
perlとかで作るならforkするのが素直でおすすめ。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0