Inter-process communication without FIFOs

后端 未结 5 1697
一向
一向 2021-01-31 22:16

Inside a BASH script we can have multiple processes running in background which intercommunicate using named pipes, FIFOs registered on the filesystem. An example of this could

5条回答
  •  一生所求
    2021-01-31 22:50

    Have you considered the use of signals? If the only thing you need is to trigger an event (without passing arguments), using kill and trap works perfectly (be careful of the semantics though, use SIGUSR1 for instance).

    You might need to rework the logic though, as in the example below:

    subprocess_finished()
    {
        np=$( jobs -p | wc -l )
    }
    
    start_processing()
    {
        myfile="$1"
        # DO SOMETHING HERE!!
        kill -SIGUSR1 $2
    }
    
    CPUS=$( lscpu | grep "^CPU(s):" | rev | cut -f 1 -d ' ' | rev )
    POLLPERIOD=5  # 5s between each poll
    np=0
    trap subprocess_finished SIGUSR1
    
    for myfile in *
    do 
            start_processing "$myfile" $$ &
            np=$( jobs -p | wc -l )
            echo "$( date +'%Y-%m-%d %H:%M:%S' ) [$!] Starting #$np on $CPUS: $myfile"
    
            if [ $np -eq $CPUS ] 
            then
                # Wait for one CPU to be free
                trap subprocess_finished SIGUSR1
                while [ $np -eq $CPUS ]
                do
                    sleep $POLLPERIOD
                done
            fi
        done
    done
    
    # wait for the last subprocesses
    while [ ! -z "$( jobs -rp )" ]
    do
        sleep $POLLPERIOD
    done
    

提交回复
热议问题