Shell simulates concurrent execution

Source: Internet
Author: User

Reference: http://www.51testing.com/html/28/116228-238978.htmlhttp://blog.chinaunix.net/uid-27571599-id-3473078.html         in bash, use background tasks to achieve multi-process tasks. In uncontrolled mode, regardless of the number of tasks, all are performed in the background. That is, in this case, how many "processes" are executing at the same time as the number of tasks.   Example One: normal script (script function: View IP list in a file cycle Test host connectivity) [[email protected] ~]# cat ip.txt  192.168.0.24192.168.0.25192.168.0.26192.168.0.27192.168.0.28192.168.0.29192.168.0.40192.168.0.41192.168.0.42192.168.0.431 92.168.0.81[[email protected] ~]# cat ping_host.sh#!/bin/bash array=$ (cat ip.txt ) #echo   $array  for host in ${array[@]}do         {               ping $ host -c 10 -w 1 -i 0.01 -q >/dev/null 2>&1                ret=$?                if [  $ret  -eq 0 ];then                          echo  "$host  isok"                 else                         echo  "$host  isbad "               fi         }done[[email protected] ~]# time sh ping_ host.sh192.168.0.24 is bad192.168.0.25 is bad..........................117.79.235.238 is  Ok210.14.130.130 is ok210.14.156.210 is ok real    0m30.688suser    0m0.099ssys    0m0.501s  instance two: "Multi-process" implementation (no precise control of concurrency) [[Email protected] ~]# cat ping_ host.sh#!/bin/bash array=$ (cat ip.txt) #echo   $array  for host in ${array[@]} do        {                 ping  $host  -c 10 -w 1 -i0.01 -q  >/dev/null 2>&1                 ret=$?                 if [  $ret  -eq 0];then                         echo "$host  is ok "                 else                         echo "$host  is bad"                  fi        }&done[[email protected]  ~]# time sh ping_host.sh real   0m0.092suser    0m0.006ssys    0m0.014s  Note: You add a background execution & symbol on top of it, which should be the concurrency of all recurring tasks: situations where the number of processes is not controllable   Example three: multi-threaded Implementation This example describes a wait, read command to simulate multi-threaded, this technique is often used for multi-host check, such as SSH login, ping, etc. this single process is slow and not CPU-intensive situation, also explained the control of multithreading. [[email protected] ~]# cat ping_host.sh#!/bin/bash tmp_fifofile= "/tmp/$.fifo" Mkfifo   $tmp _fifofile                  exec 6<> $tmp _fifofile                 rm  $tmp_fifofile thread=5                           for  ((i=0;i< $thread; i++)); doechodone >&6                           array=$ (cat ip.txt) #echo  $ array for host in ${array[@]}do                                 read -u6                            {                                                          ping$host -c 10 -w 1  -i 0.01 -q >/dev/null 2>&1    ret=$?     if [$ret  -eq 0 ];then            echo  "$host  is ok"     else            echo  "$host  is bad"     fi        echo >&6                                      } &done wait                                                    exec 6>&-                                             exit 0[[email protected]  ~]# time sh ping_host.sh192.168.0.27 is bad192.168.0.26 is  bad192.168.0.25 is bad..................................210.14.156.210 is ok210.14.130.130 is  Ok117.79.235.238 is ok real    0m6.247suser   0m0.056ssys     0m0.349s


This article from "Small http Happy Life" blog, declined reprint!

Shell simulates concurrent 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.