Linux Shell Multithreaded Programming

Source: Internet
Author: User

#!/bin/bash# ——————————————————————————— – #  This example illustrates a technique for simulating multithreading with the wait, read command  #  This technique is often used for multi-host checks, such as SSH login, ping and so on, this single process is slow and CPU-intensive situation  #  also explained the multi-threaded control  # ——————————————————————————— –function a_sub  { #  here defines a function, as a thread (subprocess)    sleep 3 #  thread's role is sleep 3s } Tmp_fifofile= "/tmp/$$.fifo"  mkfifo  $tmp _fifofile      #  Create a new FIFO type of file   exec 6<> $tmp _fifofile      #  Point fd6 to FIFO type  rm  $tmp _fifofile thread=15 #  define the number of threads here  for  ((i=0;i< $thread; i+ +));d o echo done >&6 #  In fact the $thread carriage return for  ((i=0;i<50;i++)) is placed in the Fd6;d o  # 50 cycles, which can be understood as 50 hosts, or other  read -u6 #  a READ&NBSP;-U6 command executes once, subtracts a carriage return from FD6, then executes down, When there is no carriage return in the &NBSP;#&NBSP;FD6, it is stopped here, so that the number of threads is controlled  { #  here the sub-process begins execution and is placed in the background  a_sub &&  { #  This can be used to determine the logical &nbsp of a child process;echo  "A_sub is finished"  } | |  { echo  "Sub error"  } echo >&6 #  when the process is finished, add a carriage return to the FD6, That is, Read -u6 minus the  } & done wait #  waiting for all the background child processes to end  exec 6> &- #  Close Df6 exit 0

Description

? commands in this program ?

Mkfifo tmpfile

and the commands in Linux

Mknod Tmpfile P

Effective ? The same results. The difference is that MKFIFO is a POSIX standard, so it is recommended to use it. This command creates a first-in, first-out pipe file and assigns it a file identifier of 6. Pipeline files are a way of communicating between processes, it is important to note that this sentence  

exec 6<> $tmp _fifofile # points fd6 to FIFO type

If this is not the case, when writing data to a file $tmp_fifofile or &6, the program is blocked until read reads out the data in the pipeline file. After executing this sentence, it is possible to write data to the FIFO type file continuously during the program operation without blocking, and the data will be saved for the Read program.


By running the command:

Time./multithread.sh >/dev/null


Final operation time: 50/15 = 3 groups (15 per group) +1 groups (5 <15 constitute a group) = 4 groups, each group takes time: 3 seconds,

Then 3 * 4 = 12 seconds.

Traditional non-multithreaded code operation time: 50 * 3 = 150 seconds.

Linux Shell Multithreaded Programming

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.