Copy Code code as follows:
#!/bin/bash
Send_thread_num=13
Tmp_fifofile= "/tmp/$$.fifo" # script runs the current process ID number as filename
Mkfifo "$tmp _fifofile" # Create a new random FIFO pipeline file
exec 6<> "$tmp _fifofile" # definition file descriptor 6 point to this FIFO pipe file
RM $tmp _fifofile
For ((i=0;i< $SEND _thread_num;i++));d o
Echo # for Loop writes 13 blank lines to the FIFO pipe file
Done >&6
For i in ' seq ';d o # 100 times for Loop start
Read-u6 # Reads rows from the file descriptor 6 (actually pointing to the FIFO pipeline)
{
echo $i # Print I
Sleep 3 # Pause for 3 seconds
Echo >&6 # Again writes a blank line to the FIFO pipeline file.
} &
# {} This part of the statement is put into the background as a child process, so do not have to wait 3 seconds each time after the execution
#下一个, this part of the Echo $i is almost simultaneous, when 13 empty lines in the FIFO are read and the For loop is finished
# continue to wait for read in the FIFO data, when 13 sub processes in the background wait 3 seconds, in order
# Queue to FIFO input blank line, so the FIFO has data, for statement continue to execute
pid=$! #打印最后一个进入后台的子进程id
Echo $pid
Done
Wait
EXEC 6>&-#删除文件描述符6
Exit 0