Multi-processing using shell script

I love concurrency. Writing a concurrent program is always complex. There are different technologies which are most suitable to do this job. In an earlier post I briefed how to achieve parallelization using PHP. Parallel processing can be done using shell script also; by spawning multiple child processes. It’s fairly simple to handle basic concurrent tasks in that manner.

A basic example to achieve the same-


declare -a NUMBERS=(1 2 3 4 5 6 7 8);

functionName() {
    echo "pid: $BASHPID => number: $1";

export -f functionName

for NUMBER in ${NUMBERS[*]}; do echo $NUMBER; done | xargs -P4 -n1 -I{} bash -c "functionName '{}'";


pid: 9970 => number: 3
pid: 9971 => number: 4
pid: 9968 => number: 1
pid: 9969 => number: 2
pid: 9973 => number: 6
pid: 9972 => number: 5
pid: 9975 => number: 8
pid: 9974 => number: 7

Here I’ve explained a practical example.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s