Use Shell to achieve concurrency control

    The idea is to create a pipe, write a fixed number of rows of data into it, read one row before the process performs the operation, and write one row after the operation is performed. Can realize the concurrent operation of the controllable number of processes. Note that this idea is similar to the concept of semaphore.

#!/usr/bin/bash
#ping02
thread=8
tmp_fifofile=/tmp/$$.fifo

mkfifo $tmp_fifofile
exec 8<> $tmp_fifofile
rm $tmp_fifofile
#注意上面虽然删除文件了,但是文件描述符8还是在的

for i in `seq $thread`
do
#注意&8就相当于文件描述符8,下面这句话的作用是向文件中写入8个回车
        echo >&8
done

for i in {1...254}
do
        read -u 8
        #注意,只有当上面的这一行可以读到东西才会执行下面的循环
        {
        ip=192.168.122.$i
        ping -c1 -W1 $ip &>/dev/null
        if [ $? -eq 0 ]; then
                echo "$ip is up."
        else
                echo "$ip is down"
        fi
        echo >&8
        }&
done
wait
exec 8>&-
echo "all finish..."

 

Guess you like

Origin blog.csdn.net/xiaoan08133192/article/details/109343368