Examples of shell multithreaded programming

Source: Internet
Author: User

#!/bin/bash#a_sub () {    sleep 5}multi_thread () {    thread=5              #  define the number of threads here      tmp_fifo= "/tmp/$.fifo"     /usr/bin/mkfifo  $TMP _fifo    #  Create a new FIFO type of file     exec 6<> $TMP _fifo    /bin/rm -f  $TMP _fifo      #  point fd6 to FIFO type     for  ((i=0;i<$ thread;i++));d O        /bin/echo     done  >&6             #  The fact is that the $thread carriage return character}sleep_main () {    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 the FD6, and then executes down         When there is no carriage return in the &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;#&NBSP;FD6, it stops here. This enables thread count control         {              #  here the sub-process begins execution and is placed in the background              a_sub            /bin/echo  >&6   #  when the process is finished, add a carriage return to FD6, that is, Read -u6 minus the          } &    done    wait                 #  wait for all background sub-processes to end      exec 6>&-             #   Close DF6&NBSP;EXIT&Nbsp;0 }multi_thread                      sleep_main

Commands in this program

Mkfifo tmpfile

And the commands in Linux

Mknod tmpfile

The effect is the same. 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 _fifo # points fd6 to FIFO type

If this is not the case, when writing data to a file $tmp_fifo 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.


This article is from the "Baby God" blog, make sure to keep this source http://babyshen.blog.51cto.com/8405584/1876312

Examples of 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.