1#!/bin/Bash2 3send_thread_num= -#设置进程数. 4tmp_fifofile="/tmp/$$.fifo"# The current process ID number that the script runs as the file name5 Mkfifo "$tmp _fifofile"# Create a new random FIFO pipeline file6Exec6<>"$tmp _fifofile"# Definition File Descriptor 6 point to this FIFO pipeline file7 RM "$tmp _fifofile"8 for((i=0;i< $SEND _thread_num;i++)); Do9 Echo# for Loop writes 13 blank lines to a FIFO pipeline fileTen Done>&6 One A forIinch`seq -`; Do# -Times forCycle Start -Read-U6 # Reads a line from File descriptor 6 (actually pointing to the FIFO pipeline) - the { - Echo$i # Print I - Sleep 3# Pause for 3 seconds - Echo>&6# Write a blank line to the FIFO pipeline file again. +} & - + # {} This part of the statement is placed in the background as a child process, so you do not have to wait 3 seconds after the execution A #下一个, this part of the Echo $i is almost complete at the same time, when 13 empty lines in the FIFO have been read for a loop at # Continue waiting for read in the FIFO data, when 13 sub-processes in the background wait 3 seconds, in order - # Queued to enter a blank line in the FIFO, so that the FIFO has data, the for statement continues to execute - -pid=$!#打印最后一个进入后台的子进程id - Echo$pid - in Done - wait#等到后台的进程都执行完毕. to +Exec6>&-#删除文件描述符6 - the *Exit0
Use association Learning with MKFIFO, exec
It's actually the concurrency process.
Shell concurrent multi-process simultaneous execution