Shell script emulates multithreaded feature sharing _linux shell

Source: Internet
Author: User

Copy Code code as follows:

#!/bin/bash
function Pinghost {
Ping $1-c 1-w |grep rtt|cut-d "/"-f6
}
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
thread=10
For ((i=0;i< $thread i++));d O # for Loop writes 10 blank lines to the FIFO pipe file
Echo
Done >&6
While read domain
Todo
Read-u6 # Reads rows from the file descriptor 6 (actually pointing to the FIFO pipeline)
{
Pinghost ${domain}; # Execute Pinghost function
Echo >&6 # Again writes a blank line to the FIFO pipeline file.
}& # put in the background to perform
DoneWait #因为之前的进程都是后台执行, so it is only after waiting that all the processes have been executed that the entire script runs out.
EXEC 6>&-#删除文件描述符6
Exit 0

Description: {} This part of the statement is put into the background as a child process execution, this part is almost simultaneous completion, when 10 empty lines in the FIFO read the while loop
Continue waiting for read in the FIFO data, when the background of the 10 sub processes, in order to queue to FIFO input blank line, so that the FIFO data, for statements continue to execute.

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.