I often write bash scripts to be used within smp machines.
Using thread pools to parallelize these scripts greatly improves performance.
I’m copying some code from here .
Here’s a simpler template:

#!/bin/bash
NUM=0
QUEUE=""
MAX_NPROC=2 # default

function execute {
sleep `echo " $RANDOM / 1000 " | bc`;
echo "parsing and recover" ;
}

function queue {
QUEUE="$QUEUE $1"
NUM=$(($NUM+1))
}

function regeneratequeue {
OLDREQUEUE=$QUEUE
QUEUE=""
NUM=0
for PID in $OLDREQUEUE
do
if [ -d /proc/$PID ] ; then
QUEUE="$QUEUE $PID"
NUM=$(($NUM+1))
fi
done
}

function checkqueue {
OLDCHQUEUE=$QUEUE
for PID in $OLDCHQUEUE
do
if [ ! -d /proc/$PID ] ; then
regeneratequeue # at least one PID has finished
break
fi
done
}

for a in `seq 1 10 `
do
echo "Running $CMD"
execute &
PID=$!
queue $PID

while [ $NUM -ge $MAX_NPROC ]; do
checkqueue
sleep 0.4
done
done
wait

Advertisements