pastebin - collaborative debugging tool
eckelmann.kpaste.net RSS


semaphore implemented as shell script
Posted by Anonymous on Mon 18th Nov 2019 16:16
raw | new post
view followups (newest first): semaphore implemented as shell script (version without |fork()|/|exec()| per test cycle) by Anonymous

  1. #!/usr/bin/ksh93
  2.  
  3. # should work for ksh88/ksh93/bash4
  4.  
  5. ########################################################################
  6. #                                                                      #
  7. #               This software is part of the ast package               #
  8. #                    Copyright (c) 2019 Roland Mainz                   #
  9. #                      and is licensed under the                       #
  10. #                 Eclipse Public License, Version 1.0                  #
  11. #                    by AT&T Intellectual Property                     #
  12. #                                                                      #
  13. #                A copy of the License is available at                 #
  14. #          http://www.eclipse.org/org/documents/epl-v10.html           #
  15. #         (with md5 checksum b35adb5213ca9657e911e9befb180842)         #
  16. #                                                                      #
  17. #                                                                      #
  18. #                 Roland Mainz <roland.mainz@nrubsig.org>              #
  19. #                                                                      #
  20. ########################################################################
  21.  
  22. #
  23. # Copyright (c) 2019, Roland Mainz. All rights reserved.
  24. #
  25.  
  26. #
  27. # shsemaphore.sh - wait until <n> customers are waiting
  28. # at the semaphore
  29. #
  30. # Usage:
  31. # 1. init semaphore ("mysemaname" is the 'semaphore address')
  32. # $ shsemaphore.sh init mysemaname
  33. #
  34. # 2. let two semaphore customers (names are 'Bear' and 'Fox')
  35. # wait for each other:
  36. # $ shsemaphore.sh wait mysemaname Bear 2 &
  37. # $ shsemaphore.sh wait mysemaname Fox 2
  38. #
  39. # 3. Cleanup all the mess if you are really done:
  40. # $ shsemaphore.sh cleanup mysemaname
  41. #
  42.  
  43. # POSIX path for mkdir, ls, rm, rmdir ...
  44. export PATH='/bin:/usr/bin'
  45.  
  46. # constants
  47. typeset -r SEMA_BASE_DIR='/tmp/'
  48.  
  49. # arguments
  50. typeset sema_cmd="$1"
  51. typeset sema_address="${SEMA_BASE_DIR}/shsemaphore_$2"
  52. typeset sema_name="${sema_address}/$3"
  53. typeset -i sema_count="$4"
  54.  
  55. # Debug:
  56. #printf "cmd=%s, address=%s, name=%s, count=%d\n" \
  57. #       "${sema_cmd}" "${sema_address}" "${sema_name}" "${sema_count}"
  58.  
  59. function sema_cleanup
  60. {
  61.         rm -Rf "${sema_address}"
  62. }
  63.  
  64. function sema_init
  65. {
  66.         if [[ -z "$SEMA_BASE_DIR" || -z "$sema_address" ]] ; then
  67.                 printf "%s: Illegal arguments.\n" "$0" 1>&2
  68.                 return 1
  69.         fi
  70.  
  71.         mkdir -p "${SEMA_BASE_DIR}"
  72.        
  73.         sema_cleanup
  74.         mkdir "${sema_address}"
  75. }
  76.  
  77. function sema_wait
  78. {
  79.         typeset IFS
  80.  
  81.         if [[ -z "$SEMA_BASE_DIR" || -z "$sema_address" || -z "$sema_name" || -z "$sema_count" ]] ; then
  82.                 printf "%s: Illegal arguments.\n" "$0" 1>&2
  83.                 return 1
  84.         fi
  85.  
  86.         if ! mkdir "${sema_name}" ; then
  87.                 printf "%s: Count not set name.\n" "$0" 1>&2
  88.                 return 1
  89.         fi
  90.  
  91.         IFS=$'\n'
  92.         while (( 1 )) ; do
  93.                 # we count '.' and '..', too
  94.                 sfiles=( $(ls -1a "${sema_address}" 2>'/dev/null') )
  95.  
  96.                 # semaphore condition fullfilled ?
  97.                 (( ${#sfiles[@]} >= (sema_count+2) )) && return 0
  98.  
  99.                 # semaphore gone ?
  100.                 (( ${#sfiles[@]} < 2 )) && return 4
  101.  
  102.                 sleep 2
  103.         done
  104.         # notreached
  105. }
  106.  
  107. # main
  108. case "${sema_cmd}" in
  109.         'init')
  110.                 sema_init
  111.                 exit $?
  112.                 ;;
  113.         'wait')
  114.                 sema_wait
  115.                 exit $?
  116.                 ;;
  117.         'cleanup')
  118.                 sema_cleanup
  119.                 exit $?
  120.                 ;;
  121.         *)
  122.                 printf "%s: Unknown command.\n" "$0" 1>&1
  123.                 exit 1
  124.                 ;;
  125. esac

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.

Syntax highlighting:

To highlight particular lines, prefix each line with {%HIGHLIGHT}




All content is user-submitted.
The administrators of this site (kpaste.net) are not responsible for their content.
Abuse reports should be emailed to us at