views:

268

answers:

2

I would 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.. ?

+5  A: 

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.

Martin
You should read up the difference between process and thread. What you propose is not multithreading - it involves separate processes for every command.
TomTom
@TomTom: I certainly know the difference between processes and threads. If you see through the OP's choice of words, I believe he is simply asking whether it's possible to run the commands in parallel (which is possible). I added a note about this to clarify.
Martin
Kiran
+3  A: 

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.

mouviciel