Shell multithreaded Scripts

Source: Internet
Author: User


<--Directory--

1) Multithreading Overview

2) Multithreaded script content

3) Multithreaded script execution time

4) Single Thread script content

5) Single Thread script execution time


"Multithreading Overview"

# multithreading refers to executing tasks concurrently instead of a single serial to perform tasks, which enables faster and more efficient utilization of resources

# Let's describe how to control the number of concurrent numbers accurately

# The following example shows a technique for simulating multithreading with wait and read commands

# This technique is often used for multi-host checks, such as SSH login, ping, and so on, which are slow and CPU-intensive.

# also explains the control of multithreading



"Multithreaded script content"

#!/bin/bash

function a_sub { #) here defines a functions as a thread (child process)

Sleep 3 # The role of the thread is sleeping 3 seconds

}

Tmp_fifofile= "/tmp/$$.fifo"

Mkfifo $tmp _fifofile # Create a new FIFO-type file

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

RM $tmp _fifofile

Thread=5 # Defines the number of threads here, I have defined five threads for him, and one is five concurrent executions

For ((i=0;i< $thread; i++));d o

Echo

Done >&6 # actually put $thread in the Fd6

For ((i=0;i<20;i++));d o # 20 cycles, which can be understood as 20 hosts, or other

Read-u6

# a READ-U6 command executes once, subtracts a carriage return from the FD6, then executes down,

# when there is no carriage return in the FD6, it stops here, allowing for a thread count control

{ # Here the child process starts executing and is placed in the background

A_sub && { # here can be used to determine the logic of a child process

echo "Hellow World"

} || {

echo "Hellow Error"

}

echo >&6 # When the process is finished, add a carriage return to the FD6, which adds up to the Read-u6 minus the

} &

Done

Wait

EXEC 6>&- # close Df6

Exit 0


"Multithreaded Script Execution Time"

[[Email protected] opt]# time SH a.sh

Hellow World

Hellow World

Hellow World

Hellow World

Hellow World

Hellow World

Hellow World

Hellow World

Hellow World

Hellow World

Hellow World

Hellow World

Hellow World

Hellow World

Hellow World

Hellow World

Hellow World

Hellow World

Hellow World

Hellow World


Real 0m12.015s

User 0m0.002s

SYS 0m0.017s


Analysis:

Sleep 3s, the number of threads is 5, a total of 20 cycles, so, this script altogether the execution time is approximately 12 seconds

So execute five threads at a time, that is, five hosts, execute four times altogether

That

5X4=20, five hosts are circulating four times

So 4 x 3s = 12s three seconds for one cycle


"Single-threaded script content"

[email protected] opt]# cat b.sh

#!/bin/bash

For ((i=0;i<20;i++))

Do

echo "Helloworld!!!"

Sleep 3

Done


"Single-threaded script execution Time"

[[Email protected] opt]# time SH b.sh

Helloworld!!!

Helloworld!!!

Helloworld!!!

Helloworld!!!

Helloworld!!!

Helloworld!!!

Helloworld!!!

Helloworld!!!

Helloworld!!!

Helloworld!!!

Helloworld!!!

Helloworld!!!

Helloworld!!!

Helloworld!!!

Helloworld!!!

Helloworld!!!

Helloworld!!!

Helloworld!!!

Helloworld!!!

Helloworld!!!


Real 1m0.041s

User 0m0.008s

SYS 0m0.023s


Conclusion:

Multi-threaded execution task completion time 12s

Single-threaded execution task completion time 60s

This article is from the "Wsyht blog" blog, make sure to keep this source http://wsyht2015.blog.51cto.com/9014030/1789475

Shell multithreaded Scripts

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.