Multithreading in Bash [duplicate]
I would like to introduce multithreading feature in my shell script.
I have a script which calls the function read_cfg()
with different arguments.
Each of these function calls are independent.
Would it be possible to instantiate these function calls (not scripts) parallelly. Please let me how can we achieve that.. ?
Sure, just add &
after the command:
read_cfg cfgA &
read_cfg cfgB &
read_cfg cfgC &
wait
all those jobs will then run in the background simultaneously. The optional wait
command will then wait for all the jobs to finish.
Each command will run in a separate process, so it's technically not "multithreading", but I believe it solves your problem.
You can run several copies of your script in parallel, each copy for different input data, e.g. to process all *.cfg files on 4 cores:
ls *.cfg | xargs -P 4 -n 1 read_cfg.sh
The read_cfg.sh script takes just one parameters (as enforced by -n)
Bash job control involves multiple processes, not multiple threads.
You can execute a command in background with the &
suffix.
You can wait for completion of a background command with the wait
command.
You can execute multiple commands in parallel by separating them with |
. This provides also a synchronization mechanism, since stdout of a command at left of |
is connected to stdin of command at right.
精彩评论