Created
August 8, 2013 00:00
-
-
Save orbekk/6180139 to your computer and use it in GitHub Desktop.
Bash Challenge #1 Solution
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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