Home > bashism, software > Bash: parallel processes

Bash: parallel processes

Sometimes there is a need for spawning multiple sub processes in a shell script and do something after ALL of them have finished. The Bash has nice little features to do this very easily.

Here is an example:

#!/bin/bash
./process_1 &     # fork first sub process
PID_1=$!          # get PID of first sub process
./process_2 &     # fork second sub process
PID_2=$!          # get PID of second sub process

#...do some other work...

wait  ${PID_1}     # stop execution until first process has finished
wait  ${PID_2}     # stop execution until second process has finished

#...all work is done...

exit 0

With the ampersand (&) one can fork sub processes running independent from the parent process.

To avoid busy waiting we want to stop execution of the parent script until the child has finished. So we don’t use an infinite loop with a check and a sleep or something like that, but a function called wait together with the childs’ Process ID.

The PID of a created process can be received by reading $! right after the fork. As far as I know this is mostly bash functionality and maybe is not POSIX! (correct me if I’m wrong)

Categories: bashism, software Tags: , ,
  1. Hermann
    May 9th, 2011 at 23:07 | #1

    No need for the PID variables with bash’s job control: just do a wait %1, wait %2 etc.

    See the last line of this section for reference: http://web.mit.edu/gnu/doc/html/features_5.html#SEC34

  2. stefan
    May 11th, 2011 at 09:01 | #2

    nice! :)

  1. No trackbacks yet.