pastebin - collaborative debugging tool
eckelmann.kpaste.net RSS

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

    
11
#!/usr/bin/ksh93
22
33
# should work for ksh88/ksh93/bash4
44
55
########################################################################
66
#                                                                      #
77
#               This software is part of the ast package               #
88
#                    Copyright (c) 2019 Roland Mainz                   #
99
#                      and is licensed under the                       #
1010
#                 Eclipse Public License, Version 1.0                  #
1111
#                    by AT&T Intellectual Property                     #
1212
#                                                                      #
1313
#                A copy of the License is available at                 #
1414
#          http://www.eclipse.org/org/documents/epl-v10.html           #
1515
#         (with md5 checksum b35adb5213ca9657e911e9befb180842)         #
1616
#                                                                      #
1717
#                                                                      #
1818
#                 Roland Mainz <roland.mainz@nrubsig.org>              #
1919
#                                                                      #
2020
########################################################################
2121
2222
#
2323
# Copyright (c) 2019, Roland Mainz. All rights reserved.
2424
#
2525
2626
#
2727
# shsemaphore.sh - wait until <n> customers are waiting
2828
# at the semaphore
2929
#
3030
# Usage:
3131
# 1. init semaphore ("mysemaname" is the 'semaphore address')
3232
# $ shsemaphore.sh init mysemaname
3333
#
3434
# 2. let two semaphore customers (names are 'Bear' and 'Fox')
3535
# wait for each other:
3636
# $ shsemaphore.sh wait mysemaname Bear 2 &
3737
# $ shsemaphore.sh wait mysemaname Fox 2
3838
#
3939
# 3. Cleanup all the mess if you are really done:
4040
# $ shsemaphore.sh cleanup mysemaname
4141
#
4242
4343
# POSIX path for mkdir, ls, rm, rmdir ...
4444
export PATH='/bin:/usr/bin'
4545
4646
# constants
4747
typeset -r SEMA_BASE_DIR='/tmp/'
4848
4949
# arguments
5050
typeset sema_cmd="$1"
5151
typeset sema_address="${SEMA_BASE_DIR}/shsemaphore_$2"
5252
typeset sema_name="${sema_address}/$3"
5353
typeset -i sema_count="$4"
5454
5555
# Debug:
5656
#printf "cmd=%s, address=%s, name=%s, count=%d\n" \
5757
#	"${sema_cmd}" "${sema_address}" "${sema_name}" "${sema_count}"
5858
5959
function sema_cleanup
6060
{
6161
	rm -Rf "${sema_address}"
6262
}
6363
6464
function sema_init
6565
{
6666
	if [[ -z "$SEMA_BASE_DIR" || -z "$sema_address" ]] ; then
6767
		printf "%s: Illegal arguments.\n" "$0" 1>&2
6868
		return 1
6969
	fi
7070
7171
	mkdir -p "${SEMA_BASE_DIR}"
7272
7373
	sema_cleanup
7474
	mkdir "${sema_address}"
7575
}
7676
7777
function sema_wait
7878
{
7979
	typeset IFS
8080
8181
	if [[ -z "$SEMA_BASE_DIR" || -z "$sema_address" || -z "$sema_name" || -z "$sema_count" ]] ; then
8282
		printf "%s: Illegal arguments.\n" "$0" 1>&2
8383
		return 1
8484
	fi
8585
8686
	if ! mkdir "${sema_name}" ; then
8787
		printf "%s: Count not set name.\n" "$0" 1>&2
8888
		return 1
8989
	fi
9090
91+
	# is semaphore still valid ?
9194
	IFS=$'\n'
9295
	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
95102
103+
		# Debug:
96106
		# semaphore condition fullfilled ?
97107
		(( ${#sfiles[@]} >= (sema_count+2) )) && return 0
98108
99109
		# 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 '.*/'
101113
102114
		sleep 2
103115
	done
104116
	# notreached
105117
}
106118
107119
# main
108120
case "${sema_cmd}" in
109121
	'init')
110122
		sema_init
111123
		exit $?
112124
		;;
113125
	'wait')
114126
		sema_wait
115127
		exit $?
116128
		;;
117129
	'cleanup')
118130
		sema_cleanup
119131
		exit $?
120132
		;;
121133
	*)
122134
		printf "%s: Unknown command.\n" "$0" 1>&1
123135
		exit 1
124136
		;;
125137
esac
126138
127139

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