Difference between
modified post a5 by Anonymous on Mon 18th Nov 2019 17:45
original post 239810184c by Anonymous on Mon 18th Nov 2019 16:16
Download diff
Show
old version |
new version |
both versions
| 1 | 1 | #!/usr/bin/ksh93 | |
| 2 | 2 | ||
| 3 | 3 | # should work for ksh88/ksh93/bash4 | |
| 4 | 4 | ||
| 5 | 5 | ######################################################################## | |
| 6 | 6 | # # | |
| 7 | 7 | # This software is part of the ast package # | |
| 8 | 8 | # Copyright (c) 2019 Roland Mainz # | |
| 9 | 9 | # and is licensed under the # | |
| 10 | 10 | # Eclipse Public License, Version 1.0 # | |
| 11 | 11 | # by AT&T Intellectual Property # | |
| 12 | 12 | # # | |
| 13 | 13 | # A copy of the License is available at # | |
| 14 | 14 | # http://www.eclipse.org/org/documents/epl-v10.html # | |
| 15 | 15 | # (with md5 checksum b35adb5213ca9657e911e9befb180842) # | |
| 16 | 16 | # # | |
| 17 | 17 | # # | |
| 18 | 18 | # Roland Mainz <roland.mainz@nrubsig.org> # | |
| 19 | 19 | # # | |
| 20 | 20 | ######################################################################## | |
| 21 | 21 | ||
| 22 | 22 | # | |
| 23 | 23 | # Copyright (c) 2019, Roland Mainz. All rights reserved. | |
| 24 | 24 | # | |
| 25 | 25 | ||
| 26 | 26 | # | |
| 27 | 27 | # shsemaphore.sh - wait until <n> customers are waiting | |
| 28 | 28 | # at the semaphore | |
| 29 | 29 | # | |
| 30 | 30 | # Usage: | |
| 31 | 31 | # 1. init semaphore ("mysemaname" is the 'semaphore address') | |
| 32 | 32 | # $ shsemaphore.sh init mysemaname | |
| 33 | 33 | # | |
| 34 | 34 | # 2. let two semaphore customers (names are 'Bear' and 'Fox') | |
| 35 | 35 | # wait for each other: | |
| 36 | 36 | # $ shsemaphore.sh wait mysemaname Bear 2 & | |
| 37 | 37 | # $ shsemaphore.sh wait mysemaname Fox 2 | |
| 38 | 38 | # | |
| 39 | 39 | # 3. Cleanup all the mess if you are really done: | |
| 40 | 40 | # $ shsemaphore.sh cleanup mysemaname | |
| 41 | 41 | # | |
| 42 | 42 | ||
| 43 | 43 | # POSIX path for mkdir, ls, rm, rmdir ... | |
| 44 | 44 | export PATH='/bin:/usr/bin' | |
| 45 | 45 | ||
| 46 | 46 | # constants | |
| 47 | 47 | typeset -r SEMA_BASE_DIR='/tmp/' | |
| 48 | 48 | ||
| 49 | 49 | # arguments | |
| 50 | 50 | typeset sema_cmd="$1" | |
| 51 | 51 | typeset sema_address="${SEMA_BASE_DIR}/shsemaphore_$2" | |
| 52 | 52 | typeset sema_name="${sema_address}/$3" | |
| 53 | 53 | typeset -i sema_count="$4" | |
| 54 | 54 | ||
| 55 | 55 | # Debug: | |
| 56 | 56 | #printf "cmd=%s, address=%s, name=%s, count=%d\n" \ | |
| 57 | 57 | # "${sema_cmd}" "${sema_address}" "${sema_name}" "${sema_count}" | |
| 58 | 58 | ||
| 59 | 59 | function sema_cleanup | |
| 60 | 60 | { | |
| 61 | 61 | rm -Rf "${sema_address}" | |
| 62 | 62 | } | |
| 63 | 63 | ||
| 64 | 64 | function sema_init | |
| 65 | 65 | { | |
| 66 | 66 | if [[ -z "$SEMA_BASE_DIR" || -z "$sema_address" ]] ; then | |
| 67 | 67 | printf "%s: Illegal arguments.\n" "$0" 1>&2 | |
| 68 | 68 | return 1 | |
| 69 | 69 | fi | |
| 70 | 70 | ||
| 71 | 71 | mkdir -p "${SEMA_BASE_DIR}" | |
| 72 | 72 | ||
| 73 | 73 | sema_cleanup | |
| 74 | 74 | mkdir "${sema_address}" | |
| 75 | 75 | } | |
| 76 | 76 | ||
| 77 | 77 | function sema_wait | |
| 78 | 78 | { | |
| 79 | 79 | typeset IFS | |
| 80 | 80 | ||
| 81 | 81 | if [[ -z "$SEMA_BASE_DIR" || -z "$sema_address" || -z "$sema_name" || -z "$sema_count" ]] ; then | |
| 82 | 82 | printf "%s: Illegal arguments.\n" "$0" 1>&2 | |
| 83 | 83 | return 1 | |
| 84 | 84 | fi | |
| 85 | 85 | ||
| 86 | 86 | if ! mkdir "${sema_name}" ; then | |
| 87 | 87 | printf "%s: Count not set name.\n" "$0" 1>&2 | |
| 88 | 88 | return 1 | |
| 89 | 89 | fi | |
| 90 | 90 | ||
| 91 | + | # is semaphore still valid ? | |
| 91 | 94 | IFS=$'\n' | |
| 92 | 95 | while (( 1 )) ; do | |
| 93 | - | # we count '.' and '..', too | |
| 96 | + | # 1. we rely on shell globing to avoid |fork()|/|exec()| | |
| 94 | - | sfiles=( $(ls -1a "${sema_address}" 2>'/dev/null') ) | |
| 97 | + | # for ls(1) | |
| 98 | + | # 2. we count '.' and '..', too. Both special dirs | |
| 95 | 102 | ||
| 103 | + | # Debug: | |
| 96 | 106 | # semaphore condition fullfilled ? | |
| 97 | 107 | (( ${#sfiles[@]} >= (sema_count+2) )) && return 0 | |
| 98 | 108 | ||
| 99 | 109 | # semaphore gone ? | |
| 100 | - | (( ${#sfiles[@]} < 2 )) && return 4 | |
| 110 | + | # In that case we get only two entries from globbing | |
| 111 | + | # above, one for '*/' and one for '.*/' | |
| 101 | 113 | ||
| 102 | 114 | sleep 2 | |
| 103 | 115 | done | |
| 104 | 116 | # notreached | |
| 105 | 117 | } | |
| 106 | 118 | ||
| 107 | 119 | # main | |
| 108 | 120 | case "${sema_cmd}" in | |
| 109 | 121 | 'init') | |
| 110 | 122 | sema_init | |
| 111 | 123 | exit $? | |
| 112 | 124 | ;; | |
| 113 | 125 | 'wait') | |
| 114 | 126 | sema_wait | |
| 115 | 127 | exit $? | |
| 116 | 128 | ;; | |
| 117 | 129 | 'cleanup') | |
| 118 | 130 | sema_cleanup | |
| 119 | 131 | exit $? | |
| 120 | 132 | ;; | |
| 121 | 133 | *) | |
| 122 | 134 | printf "%s: Unknown command.\n" "$0" 1>&1 | |
| 123 | 135 | exit 1 | |
| 124 | 136 | ;; | |
| 125 | 137 | esac | |
| 126 | 138 | ||
| 127 | 139 |
Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.
eckelmann.kpaste.net RSS