Listing 12 grab_resource()
{
# Caveat Emptor! The implementation of semaphore hinges on the
# behavior of the ln command. To implement a semaphore
# correctly, the test of a semaphore's value and the decre-
# menting of this value must be an atomic operation. In the
# function grab_resource, the act of creating a symbolic link
# does both things at once. If the ln command is successful,
# then the semaphore's count is effectively tested and decre-
# mented in one atomic step.
local FUNC_NAME=grab_resource
${TRACE:-trace $FUNC_NAME $@}
local semaphore=$1
integer resource_num=$2
local symbolic_link=$HOME_DIR/$semaphore/$RESOURCES_DIRNAME/$resource_num
integer pid=$3
assert_semaphore $semaphore
ln -s $pid $symbolic_link >&- 2>&-
return $?
} |