Skip to content

Instantly share code, notes, and snippets.

@jhgorse
Last active September 7, 2016 04:10
Show Gist options
  • Save jhgorse/f316261e63fc397ba54fbcad5fb36c3c to your computer and use it in GitHub Desktop.
Save jhgorse/f316261e63fc397ba54fbcad5fb36c3c to your computer and use it in GitHub Desktop.
bash batch sequential producer consumer
#! /usr/bin/env bash
# TODO: Add a queue element for generated pids so that the
# producer may run in parallel with consumer loop
# Some function that takes a long time to process
longprocess() {
# Sleep up to 6 seconds
sleep $((3 + RANDOM % 3))
# Randomly exit with 0 or 1
exit $((RANDOM % 2))
}
maxjobs=$(nproc)
totaljobs=16
# Producer: Run nproc concurrent processes
for n in $(seq $totaljobs); do
( longprocess ) &
# echo "pid $! $(jobs | wc -l) >= ${maxjobs}"
while (( $(jobs | wc -l) >= ${maxjobs} )); do
sleep 0.1
jobs > /dev/null
done
done
# Consumer loop
consumed=1
while (( ${consumed} <= ${totaljobs} )); do
if wait $(jobs -p | head -1); then
echo "Process ${consumed} success"
else
echo "Process ${consumed} fail"
fi
((consumed+=1))
# echo "consumed ${consumed}"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment