Implementation of Linux Bash multi-process parallel processing _linux shell

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.