Implementation of multi-process parallel processing in Linux bash

Source: Internet
Author: User

CopyCodeThe Code is as follows :#! /Bin/bash

Send_thread_num = 13
Tmp_domainofile = "/tmp/$. FIFO" # ID of the current process running the script as the file name
Mkfifo "$ tmp_domainofile" # create a random FIFO pipeline File
Exec 6 <> "$ tmp_domainofile" # define file descriptor 6 to point to this FIFO pipeline File
Rm $ tmp_kerberofile
For (I = 0; I <$ send_thread_num; I ++); do
Echo # For Loop write 13 blank lines to the FIFO pipeline File
Done> & 6

For I in 'seq 100'; do #100 for loop start
Read-U6 # Read rows from file descriptor 6 (actually pointing to the FIFO pipeline)
{
Echo $ I # print I
Sleep 3 # Pause for 3 seconds
Echo> & 6 # Write an empty row to the FIFO pipeline file again.
}&

# {} This part of the statement is put into the background for execution as a sub-process, so you do not have to wait 3 seconds each time to execute
# Next, this part of ECHO $ I is almost completed at the same time. When 13 blank rows in the first-in-first-out are read, the For Loop
# Continue to wait for the read to read the FIFO data. When the 13 sub-processes in the background wait 3 seconds
# Enter blank rows in the queue to the FIFO, so that data in the FIFO is available, and the for statement continues to be executed.

PID = $! # Print the ID of the last sub-process that enters the background
Echo $ PID

Done
Wait
Exec 6> &-# delete file descriptor 6

Exit 0

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.