Multi-process concurrent execution using Linux Shell

Source: Internet
Author: User

In bash, background tasks are used to implement multi-process tasks ". In the uncontrolled mode, all tasks are executed in the background no matter how many tasks exist. That is to say, in this case, the "process" of each task is executed simultaneously. First, we will implement the first situation:
Example 1: Normal script
----------------------------
#! /Bin/bash

For (I = 0; I <5; I ++); do
{
Sleep 1; echo 1> aa & echo "done !"
}
Done
Cat aa | wc-l
Rm aa
----------------------------
In this case, the program is executed in sequence, and each cycle takes about 3 s, which takes about 15 s.
$ Time bash test. sh
Done!
Done!
Done!
Done!
Done!
5

Real 0m15. 030 s
User 0m0. 002 s
Sys 0m0. 003 s
Example 2: multi-process implementation
----------------------------
#! /Bin/bash

For (I = 0; I <5; I ++); do
{
Sleep 3; echo 1> aa & echo "done !"
}&
Done
Wait
Cat aa | wc-l
Rm aa
----------------------------
This instance actually adds a background execution & symbol on the basis of the above. At this time, it should be five cyclic tasks for concurrent execution, and it will take about 3 seconds.
$ Time bash test. sh
Done!
Done!
Done!
Done!
Done!
5

Real 0m3. 011 s
User 0m0. 002 s
Sys 0m0. 004 s
The effect is very obvious.
Here we need to describe about wait. Wait is executed only after all the previous background tasks are completed. Otherwise, the program itself will not wait, which may cause errors for the commands that depend on the results of the previous tasks. For example, the wc-l command above reports an error: the file aa does not exist.
The instances mentioned above are all situations where the number of processes is uncontrollable. The following describes how to accurately control the number of concurrent processes.
--------------------------------------
#! /Bin/bash
#2006-7-12, by wwy
#----------------------------
# This example illustrates a technique for simulating multithreading with wait and read commands.
# This technique is often used for multi-host checks, such as ssh logon and ping, where a single process is slow and does not consume cpu.
# Multi-thread control
#----------------------------


Function a_sub {# define a function as a thread (sub-process)
Sleep 3 # The Role Of The thread is sleep 3 s
}


Tmp_domainofile = "/tmp/$. fifo"
Mkfifo $ tmp_domainofile # create a file of the fifo type
Exec 6 <> $ tmp_domainofile # point fd6 to the fifo type
Rm $ tmp_kerberofile


Thread = 15 # Number of threads defined here
For (I = 0; I <$ thread; I ++); do
Echo
Done> & 6 # In fact, $ thread carriage returns are placed in fd6.


For (I = 0; I <50; I ++); do #50 cycles, can be understood as 50 hosts, or other

Read-u6
# If a read-u6 command is executed once, a carriage return is subtracted from fd6 and then executed downward,
# When there is no carriage return in fd6, it stops here to implement thread quantity control.

{# The sub-process starts execution and is put in the background
A_sub & {# The sub-process logic can be determined here.
Echo "a_sub is finished"
} | {
Echo "sub error"
}
Echo> & 6 # When the process ends, add a carriage return to fd6, that is, the one that is subtracted from read-u6.
}&

Done

Wait # wait for all the backend sub-processes to end
Exec 6> &-# disable df6


Exit 0
--------------------------------------
Sleep 3 s, thread number is 15, a total of 50 cycles, so the total execution time of this script is about 12 seconds
That is:
15 × 3 = 45, SO 3x3 s = 9 s
(50-45 = 5) <15, so 1x3 s = 3 s
So 9 s + 3 s = 12 s
$ Time./multithread. sh>/dev/null
Real 0m12. 025 s
User 0m0. 020 s
Sys 0m0. 064 s
When multithreading is not used, the execution time is: 50x3 s = 150 s.
The command mkfifo tmpfile in this program has the same effect as the command mknod tmpfile p in linux.
The difference is that mkfifo is 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 6. Pipeline files are a way of communication between processes. Note that this sentence is very important.
Exec 6 <> $ tmp_domainofile # point fd6 to the fifo type
If this statement is not provided, the program will be blocked when writing data to the file $ tmp_kerberofile or & 6 until the data in the MPs queue file is read by read. After the above statement is executed, data can be constantly written to the first-in-first-out files during the program running without blocking, and the data will be saved for the read program to read.

Author: "dikar cloud mozhu"
 

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.