Bash shell for concurrent multi-process operations

Source: Internet
Author: User

Bash shell for concurrent multi-process operations

Preface
Currently, I have mastered the basic language, php (most familiar with the code in the project), bash shell (O & M tool), and c (acm dedicated ), it seems that only c can implement multithreading, but c is only used to learn and implement algorithms and data structures. Therefore, I want to simulate concurrent multi-process operations in my work, you can only rely on bash shell scripts.

Skill points
To implement concurrent operations in shell scripts, you need to use the for loop.
& Background Operator
Wait waits for the end of all sub-processes. We can write a for loop to control the number of batch operations each time,
Encapsulate a method, Perform Batch operations in the method, and run the method in the background with the & Symbol
Use the wait function before each loop ends to ensure that all the current batch processing operations are completed.
We are going to execute the create directory operation in batches. The directory name is a number, starting from 1 to 100. Each time we create 20

No concurrency
The Code is as follows: [html] #! /Bin/bash
 
# Start Time
Begin = $ (date + % s)
 
# Test the root directory
Root_dir = "/home/wzy/wzy_scripts/file_scripts/test"
 
If [! -D $ root_dir]; then
Mkdir-p $ root_dir
Fi
Cd $ root_dir
 
 
# Create 10000 directories cyclically
For (I = 0; I <10000 ;))
Do
Mkdir $ I
I = $ (expr $ I + 1)
Done
 
# End Time
End = $ (date + % s)
Spend = $ (expr $ end-$ begin)
Echo "takes $ spend seconds" Run Time:
Concurrent operations
We create 200 directories concurrently at a time, so we can create 10000 directories by executing them 50 times in a loop.

The Code is as follows: [html] #! /Bin/bash
 
# Start Time
Begin = $ (date + % s)
 
# Test the root directory
Root_dir = "/home/wzy/wzy_scripts/file_scripts/test"
 
If [! -D $ root_dir]; then
Mkdir-p $ root_dir
Fi
Cd $ root_dir
 
# Batch create directory Functions
Function create_dir ()
{
 
Mkdir $1
}
 
# Create 10000 directories cyclically
Count = 10000
Rsnum= 200
Cishu = $ (expr $ count/$ rsnum)
 
For (I = 0; I <$ cishu ;))
Do
Start_num = $ (expr $ I \ * $ rsnum + $ I)
End_num = $ (expr $ start_num + $ rsnum)
For j in 'seq $ start_num $ end_num'
Do
Create_dir $ j &
Done
Wait
I = $ (expr $ I + 1)
Done
 
# End Time
End = $ (date + % s)
Spend = $ (expr $ end-$ begin)
Echo "takes $ spend seconds"
Mainly for paging processing and & wait usage

Running time:
Summary
Concurrent operations through &, wait, and for loops can significantly improve the efficiency. You can refer to this method for your daily work, I master this method when creating multiple mysql databases!

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.