BASH Shell for concurrent multi-process operations

Source: Internet
Author: User
Preface The basic languages I have mastered: PhP (most familiar with, the code in the project is implemented using it), 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 the bash shell script to implement concurrent operations in the skill point shell script, you need to use
  1. For Loop
  2. & Background Operator
  3. Wait waits for all sub-processes to end
Ideas
  1. We can write a for loop to control the number of batch operations each time,
  2. Encapsulate a method, Perform Batch operations in the method, and run the method in the background with the & Symbol
  3. Use the wait function before each loop ends to ensure that all current batch processing operations are completed.
In this example, we prepare to create directories in batches. The directory name is a number from 1 to 100. Each time we create 20 non-concurrent times codes in batches, the following code is displayed:
#! /Bin/bash # Start Time begin = $ (date + % s) # test the root directory root_dir = "/home/wzy/wzy_scripts/file_scripts/test" If [! -D $ root_dir]; thenmkdir-p $ root_dirficd $ root_dir # create 10000 directories in a loop (I = 0; I <10000 ;)) domkdir $ II = $ (expr $ I + 1) Done # End Time end = $ (date + % s) Spend = $ (expr $ end-$ begin) echo "takes $ spend seconds"

Running time:

For concurrent operations, we create 200 directories concurrently at a time. Therefore, the code for creating 10000 directories is as follows:
#! /Bin/bash # Start Time begin = $ (date + % s) # test the root directory root_dir = "/home/wzy/wzy_scripts/file_scripts/test" If [! -D $ root_dir]; thenmkdir-p $ root_dirficd $ root_dir # create directory function create_dir () in batches () {mkdir $1} # create 10000 directories in a loop COUNT = 10000 rsnum = 200 cishu = $ (expr $ count/$ rsnum) for (I = 0; I <$ cishu;) dostart_num = $ (expr $ I \ * $ rsnum + $ I) end_num = $ (expr $ start_num + $ rsnum) for J in 'seq $ start_num $ end_num 'docreate _ DIR $ J & donewaiti =$ (expr $ I + 1) Done # End Time end = $ (date + % s) spend = $ (expr $ end-$ begin) echo "time spent: $ spend seconds"

It mainly involves paging processing and the running time of wait:

In 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!

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.