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
- For Loop
- & Background Operator
- Wait waits for all sub-processes to end
Ideas
- 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 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!