先入れ先出しプロセスの数が増え続けています。

先入れ先出しプロセスの数が増え続けています。

FIFOの問題があります。

なぜ先入選出なのか?スレッド制限は2つですが、プロセス数は増え続けます。 1つのプロセスが終了し、2つのプロセスが開始され、3つのプロセスが得られます。

連続成長2n-1

#!bin/bash
#exec clear file
mkdir -p xxx
thread_num=2
 
[ ! -p tmp ] && mkfifo tmp
exec 9<>tmp
 
for ((i=0;i<$thread_num;i++)); do
    echo >&9
done
echo "================start sh===================="
for i in  `cat shfiles.txt`;
do
        read -u 9
        {
                sh $i
                mv $i xxx/
                echo >&9
        } &
done
wait
exec 9>&-
rm tmp
echo "=================done===================="
exit 0

@Paul_Pedant
今はコメントを追加できません。このコードは簡素化され、root アカウントを使用してフォルダーで実行します。

shfiles.txtSH ファイルパスのコレクションです。

実行されたファイルには、次のようにperforceコマンドがあります。

20210326/archive13.sh

20210326/archive14.sh

p4 archive -D depot -t xxxxx#1 

私はps -aux | grep sh現在のコースを見るのに慣れています。それは成長して終わっています。

各プロセスが終了すると、2つのプロセスが作成されます。だから、ますます多くのプロセスがあります。

この問題を解決したようですが、理由がわかりません。 exec 7<>tmpのようにfifo 9 -> 7を修正します。他のプロセスで9を使用しますか?

助けてくださった皆さんに感謝します。私これ持っているps。

sh -x ShArchiveCommand.sh 
sh -x ShArchiveCommand.sh 
sh -x ShArchiveCommand.sh 
20210326/ArchiveTList12.sh
20210326/ArchiveTList13.sh

それから1時間後

sh -x ShArchiveCommand.sh 
sh -x ShArchiveCommand.sh 
20210326/ArchiveTList12.sh
sh -x ShArchiveCommand.sh 
20210326/ArchiveTList14.sh
sh -x ShArchiveCommand.sh
20210326/ArchiveTList15.sh

マイチェック ArchiveTList12 はまだ完了していません。

新しい例外

sh -x ShArchiveCommand.sh 
sh -x ShArchiveCommand.sh 
20210326/ArchiveTList12.sh
sh -x ShArchiveCommand.sh 
20210326/ArchiveTList13.sh
sh -x ShArchiveCommand.sh 
20210326/ArchiveTList14.sh

ArchiveTList12を確認しましたが、ArchiveTList13はすべて完了していません。

ベストアンサー1

アプローチが面白くてフォローしてみました。

実行する操作を説明する診断スクリプトがあります。具体的には、各ティック(「実行許可」)にシーケンス番号と時間(例:)をスタンプし、読み取る[4] Sent 19:53:54ときに識別できます。シーケンスが終了すると、残りのshfiles.txtティックも更新されます。

また、すべてのデバッグにプレフィックスを表示しました[ x ] && 。この行は永久に削除するか、xを空白に変更することで無効にすることができます。ファイル名をすべて変更しました。

私のコマンドファイルはmoonie.cmds次のとおりです(各コマンドは自分自身を識別し、スケジュールを不規則にします)。

echo One && sleep 8
echo Two && sleep 3
echo Three && sleep 6
echo Four && sleep 5
echo Five && sleep 9

これは単純なコマンドなので、bash -c.script名を使用してもかまいませんが、.cmdsファイルのパラメータと参照は難しい場合があります。ファイルがどのように見えるmvかわからないため、スクリプト(?)が実行後に別のディレクトリに移動する理由を理解できません。.cmds

pstree私のbashと外部スクリプトpidは一定で、非同期サブシェルとsleepは変数で構成され、一度に2つずつ実行されることを示しましたthreads。 1、2、3、10個のスレッドでテストしました。

私のスクリプトは次のとおりです

#! /bin/bash

Threads=2

mkdir -p moonie.dir     #.. Not used.

#.. Clean the named pipe and attach to fd9. 
rm -f moonie.fifo
mkfifo moonie.fifo
exec 9<>moonie.fifo
 
#.. Seed the fifo with required thread count.
for ((Seq = 1; Seq <= Threads; ++Seq)); do
    printf '[%s] Sent %(%T)T\n' "${Seq}" -1 >&9
done

echo "================ start sh ===================="

[ x ] && sleep 1    #.. Show we are waiting for the Ticks.

while IFS='' read -r Cmd; do    #.. Work through the command list.

    IFS='' read -r -u 9 Tick    #.. Wait for a free thread. 
    [ x ] && printf '%(%T)T Read Tick %s\n' -1 "${Tick}"

    {   #.. Launch a thread asynchronously.
        [ x ] && printf '%(%T)T Begin %s\n' -1 "${Cmd}"
        bash -c "${Cmd}"
        [ x ] && printf '%(%T)T Ended %s\n' -1 "${Cmd}"
        #.. Refresh its thread on completion.
        printf '[%s] Sent %(%T)T\n' "${Seq}" -1 >&9
    } &
    (( ++Seq ))     #.. Number Ticks for diagnostics.

    [ x ] && sleep 0.3              #.. Wait for the job to register.
    [ x ] && pstree -p "${PPID}"    #.. Show the running job hierarchy.

done < moonie.cmds

wait    #.. Wait for all threads to complete.

#.. Flush superfluous Ticks (timeout read because the fifo will hang).
while IFS='' read -r -t 1 -u 9 Tick; do
    [ x ] && printf '%(%T)T Discard Tick %s\n' -1 "${Tick}"
done

exec 9>&-       #.. Disconnect and remove the fifo.
rm moonie.fifo

echo "================= done ===================="
exit 0

デバッグセッションは次のとおりです。

================ start sh ====================
19:53:51 Read Tick [1] Sent 19:53:50
19:53:51 Begin echo One && sleep 8
One
bash(12037)───moonie(16150)─┬─moonie(16155)───sleep(16157)
                            └─pstree(16158)
19:53:51 Read Tick [2] Sent 19:53:50
19:53:51 Begin echo Two && sleep 3
Two
bash(12037)───moonie(16150)─┬─moonie(16155)───sleep(16157)
                            ├─moonie(16159)───sleep(16161)
                            └─pstree(16162)
19:53:54 Ended echo Two && sleep 3
19:53:54 Read Tick [4] Sent 19:53:54
19:53:54 Begin echo Three && sleep 6
Three
bash(12037)───moonie(16150)─┬─moonie(16155)───sleep(16157)
                            ├─moonie(16164)───sleep(16166)
                            └─pstree(16167)
19:53:59 Ended echo One && sleep 8
19:53:59 Read Tick [3] Sent 19:53:59
19:53:59 Begin echo Four && sleep 5
Four
bash(12037)───moonie(16150)─┬─moonie(16164)───sleep(16166)
                            ├─moonie(16168)───sleep(16170)
                            └─pstree(16171)
19:54:00 Ended echo Three && sleep 6
19:54:00 Read Tick [5] Sent 19:54:00
19:54:00 Begin echo Five && sleep 9
Five
bash(12037)───moonie(16150)─┬─moonie(16168)───sleep(16170)
                            ├─moonie(16172)───sleep(16174)
                            └─pstree(16175)
19:54:04 Ended echo Four && sleep 5
19:54:09 Ended echo Five && sleep 9
19:54:09 Discard Tick [6] Sent 19:54:04
19:54:09 Discard Tick [7] Sent 19:54:09
================= done ====================

おすすめ記事