Skip to content

Instantly share code, notes, and snippets.

@orbekk
Created August 8, 2013 00:00
Show Gist options
  • Save orbekk/6180139 to your computer and use it in GitHub Desktop.
Save orbekk/6180139 to your computer and use it in GitHub Desktop.
Bash Challenge #1 Solution
#!/bin/bash
if [[ -z $1 ]]; then
echo "Usage: $(basename $0) NUM_PROCESSES COMMAND..."
fi
NUM_PROCESSES=$1
shift
SEMAPHORE=/tmp/semaphore.$RANDOM
exec 3<>${SEMAPHORE}
function up() {
echo ready >> ${SEMAPHORE}
}
function down() {
local token=
while [[ $token != "ready" ]]; do
read token <&3 || sleep 1
done
}
i=$NUM_PROCESSES
while (( i-- )); do
up
done
cat ${SEMAPHORE}
for command in "$@"; do
(down && eval "$command"; up) &
done
wait
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment