1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
|
#!/bin/bash
# number of functions to run in background
NUM_FUNCTIONS=10
# array of backgroud function pid's
declare -a FUNCTION_PID
function myFunction()
{
echo "Function $1 sleeps."
sleep 2
}
i=1
echo "main pid: $$"
THREADS=1
if [ $THREADS -eq 1 ];then
echo "THREADS"
else
echo "NO THREADS"
fi
while [ $i -le $NUM_FUNCTIONS ]
do
myFunction $i $i &
FUNCTION_PID[$i]=$!
let "i+=1"
done
# we wait here for all the child processes to terminate
for i in ${FUNCTION_PID[@]} ; do
#echo "Waiting for $i ..."
wait $i
echo "$i ret code is $?. "
done
echo "Main end"
exit 0; |
Partager