Shell concurrent multi-process simultaneous execution

Source: Internet
Author: User

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

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.