Last active
March 10, 2020 18:14
-
-
Save ormaaj/3911059 to your computer and use it in GitHub Desktop.
multiproc test
This file contains 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
#!/usr/bin/env bash | |
if [[ ${!KSH_VERSION} == .sh.version ]]; then | |
if builtin getconf; builtin pids; then | |
function BASHPID.get { .sh.value=$(pids -f '%(pid)d'); } | |
elif [[ -r /proc/self/stat ]]; then | |
function BASHPID.get { read -r .sh.value _ </proc/self/stat; } | |
else | |
function BASHPID.get { .sh.value=$(exec sh -c 'echo $PPID'); } | |
fi 2>/dev/null | |
elif [[ ${BASH_VERSION+_} ]]; then | |
shopt -s lastpipe extglob | |
elif [[ ! ${BASHPID+_} ]]; then | |
echo 'BASHPID requires Bash, ksh93, or mksh >= R41' >&2 | |
exit 1 | |
fi | |
function f { | |
printf '%d: sleeping %d sec\n' "${@:1:2}" | |
sleep "$2" | |
printf '%d: returning %d\n' "$1" "$3" | |
return "$3" | |
} >&2 | |
function main { | |
typeset -i n j maxj=$(getconf _NPROCESSORS_ONLN) | |
trap '((j--))' CHLD | |
set -m | |
while ((n++ < 30)); do | |
f "$BASHPID" $(((RANDOM % 5) + 1)) $((RANDOM % 2)) & | |
((++j >= maxj)) && POSIXLY_CORRECT= wait | |
done | |
echo 'finished, waiting for remaining jobs...' >&2 | |
wait | |
} | |
main "$@" | |
echo | |
# vim: set fenc=utf-8 ff=unix ft=sh : |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment