script for multi-threaded bash processes

hey everyone,

I'm having some trouble breaking down some code. It's simple a control script that takes machines meant to be backed up from a list. Then according to that will run multi-threaded processes up until the specified thread limit.

for example if there are 4 machines to be backed up, and you specify the --threads=3 if will do the first 3, wait for one to finish before running the 4th.

Here's my problem. I know what this code does, but not how it works. Any help in breaking this down would be greatly appreciated!:slight_smile:

concurrent=`echo $1 | sed -e 's/--threads=//'`
maxprocs=`cat $serverset | wc -l`
tick=2

for ((i=0; i<$concurrent; i+=1 )); do running[$i]=123456789; done
ran=0
until
        while [ $ran -lt $maxprocs ]; do
                for ((p=0; p<$concurrent; p+=1 )); do
                        proc=${running[$p]}
                        ps -p $proc | fgrep $proc >/dev/null
                        if [ $? -ne '0' ] ; then
                                runproc $ran &
                                running[$p]=$!
                                ((ran+=1))
                                if [ $ran -ge $maxprocs ]; then break 1; fi
                        fi
                done
                sleep $tick
        done
        sleep $tick
do [ `jobs -r|wc -l` -eq 0 ]
done
wait
# get maximum number of thread slots
concurrent=`echo $1 | sed -e 's/--threads=//'`
# get number of tasks to be done
maxprocs=`cat $serverset | wc -l`
# wait time between two task checks
tick=2

# initialize running info to a value which cannot be a valid PID
for ((i=0; i<$concurrent; i+=1 )); do running[$i]=123456789; done
# Zero tasks are started at the beginning
ran=0
# LOOP until no more running background jobs are present (see condition below)
until
    # while not all tasks are started
    while [ $ran -lt $maxprocs ]; do
        # loop over all thread slots
        for ((p=0; p<$concurrent; p+=1 )); do
            # get running PID info of the thread slot
            proc=${running[$p]}
            # check if a process with that PID is (still) running
            # in the init case this will result in "not running"
            ps -p $proc | fgrep $proc >/dev/null
            # if the thread slot is in state "not running"
            if [ $? -ne '0' ] ; then
                # occupy the thread slot with a background job ...
                runproc $ran &
                # ... and remember its PID
                running[$p]=$!
                # increase started tasks count
                ((ran+=1))
                # if all tasks to be done are started, leave thread slot loop
                if [ $ran -ge $maxprocs ]; then break 1; fi
            fi
        done
        sleep $tick
    done
    sleep $tick
# LOOP until no more running background jobs are present
do [ `jobs -r|wc -l` -eq 0 ]
done
# to be sure: final wait for complete finish of all child processes
wait

awesome! thanks for the help!:slight_smile: