Skip to content

Instantly share code, notes, and snippets.

@muink
Last active February 15, 2024 16:10
Show Gist options
  • Save muink/5fab45f29e0670b83b4efa2a48e9d7d7 to your computer and use it in GitHub Desktop.
Save muink/5fab45f29e0670b83b4efa2a48e9d7d7 to your computer and use it in GitHub Desktop.
bash parallel execution
#!/bin/bash
pause() { read -p "Press any key to continue..." -n1 -r; }
# func <min> <max>
randnum() {
local min=$1
local max=$[ $2 - $min + 1 ]
local num=$(cat /dev/urandom 2>/dev/null | head -n 8 | cksum | cut -f1 -d' ')
echo $[ $num % $max + $min ]
}
# func <fdnum>
tmpfd() {
local tmpfifo=/tmp/pid$$fd$1.fifo
trap "exec $1>&-;exec $1<&-;exit 0" 2
mkfifo $tmpfifo
eval "exec $1<>$tmpfifo"
rm -rf $tmpfifo
}
NPROC=16; total=200; arr=($(seq 1 $total))
time=$($DATE -u +%s%3N)
tmpfd 6; for i in $(seq 1 $NPROC); do echo >&6; done # Generate $NPROC tokens
for i in $(seq 0 $[$total -1 ]); do
read -u6 # Take token, will wait when no token is available
{
sleep 2
#sleep $(randnum 1 3)
if [ $i -ne 30 -a $i -ne 69 -a $i -ne 99 -a $i -ne 199 ]; then
echo "$i ${arr[$i]}"
fi
echo >&6 # Release token
} &
done
wait
time=$[ $($DATE -u +%s%3N) - $time ]
echo "Total time: $[ $time / 60000 ]m$[ $time / 1000 % 60 ].$[ $time % 1000 ]s."
pause
time=$($DATE -u +%s%3N)
for i in $(seq 0 $[$total -1 ]); do
while [ $(jobs -p|wc -l) -ge $NPROC ]; do wait -n; done
{
sleep 2
#sleep $(randnum 1 3)
echo "$i ${arr[$i]}"
# Variables from subshell cannot be passed to parent shell
} &
done
wait
time=$[ $($DATE -u +%s%3N) - $time ]
echo "Total time: $[ $time / 60000 ]m$[ $time / 1000 % 60 ].$[ $time % 1000 ]s."
ref: https://superuser.com/questions/153630/running-commands-in-parallel-with-a-limit-of-simultaneous-number-of-commands
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment