Article Listing 1 Listing 2 Listing 3 Listing 4
Listing 5 Listing 6 Listing 7 Listing 8 Listing 9
Listing 10 Listing 11 Listing 12 Listing 13 Sidebar aug2004.tar

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 $?
}