当前位置:网站首页>Shell script concurrency

Shell script concurrency

2020-12-07 08:06:59 osc_ 2ak6wwpl

1、 How to understand shell Concurrent :
When we're executing a for loop , Suppose this for A cycle needs a loop 500 Time , Every cycle requires 1 second , When this for When the script of the loop is finished , Need 500 second , In this way, the efficiency is a little low ( Especially when the magnitude is high , It will be more obvious ), So we need shell The script for No matter how many times it's cycled , Let them do it at the same time , There's no need to cycle in sequence , This is it. shell Concurrency of scripts .
2、 How to achieve shell Script concurrency :
Implementation is very simple , It's in the loop {} Add a & Symbol , The command representing the body of each loop is put into the background
3、 Classic examples :
Judge 192.168.80.0/24 The Internet , The number of hosts currently online and IP




#!/bin/bash
. /etc/init.d/functions
cmd="ping -c 2"
for ((i=1;i<=254;i++));do
{
    $cmd 192.168.80.$i &>/dev/null
    if [ $? = 0  ];then
    action "192.168.80.$i"  /bin/true
    fi
}&      # take {} The loop body is put into the background for execution 
done
wait        # Wait for the command put in the background from above to be executed , It's going down ( If there's a command to execute later , Better add wait), Because once the command is executed in the background , This task will be handed over to the operating system ,shell The script will not wait until the execution of the above is completed , Will continue to carry on .

Expand your knowledge :
We know what is shell Concurrency of scripts , Just imagine , If there is 1000 Tasks are concurrent , There will be 1000 Threads produce , If the resources of the system can't keep up with , It puts a lot of pressure on the computer system , As a result, the processing speed of the operating system will be slower and slower . And how can we solve this problem ? When this happens , We can introduce named pipe files to handle .
The idea of named pipeline processing :
It's equivalent to having 10 A boiling room , Equipped with 10 Key to , At this time there is 100 You have to turn on the water , So before 10 The individual grabs the key , Just go ahead and turn on the water , hinder 90 The individual needs to return the key after the first person comes out , Going in with the key and opening the water , So you can control 100 Personal task of opening water , At the same time, it will not consume too much system resources at one time , Increase the pressure , Reduce processing speed .
Knowledge point :
1、 The properties of named pipes
If the pipeline content is empty , The block
The pipeline has read one less , The nature of saving one and reading another , Put back can be repeated
Can achieve queue control
2、 If the pipe puts a piece of content, no one takes it , It will block
Solve the above problems , You can use the file descriptor
The file descriptor has all the features of a pipeline , At the same time, it also has a feature that the pipeline does not have : Infinite existence does not block , Infinity does not block , No need to pay attention to the pipeline content
How it was created :
1、 Create named pipes
mkfifo /tmp/fl
2、 Create a file descriptor 100, And associated to the pipeline file
exec 100&lt;&gt;/tmp/fl
3、 Call file descriptor , Store content in the pipeline , At the same time, it means that the used up pipeline content is put back into the pipeline
echo &gt;&100
4、 Read the contents of the file descriptor Association pipeline
`read -u100``
5、 Turn off reading and writing of file descriptors




















exec 100<&-
exec 100>&-

Examples demonstrate : Quote the above network test case

#!/bin/bash
. /etc/init.d/functions
cmd="ping -c 2"
mkfifo /tmp/fl
exec 100<>/tmp/fl
rm -rf /tmp/fl
for ((n=1;n<=100;n++));do
    echo >&100
done
for ((i=1;i<=254;i++));do
    read -u100
{
    $cmd 192.168.80.$i &>/dev/null
    if [ $? = 0  ];then
    echo "192.168.80.$i is ok" 
    else
    echo "192.168.80.$i is no"  
    fi
    echo >&100     # Put the pipeline content back in 
}&
done
wait
#echo "test compelet"
exec 100<&-
exec 100>&-

版权声明
本文为[osc_ 2ak6wwpl]所创,转载请带上原文链接,感谢
https://chowdera.com/2020/12/202012070800441544.html