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 10 get_resource_holder

{
    # Echos the process id (pid) of the process that currently has a
    # particular resource.

    local FUNC_NAME=get_resource_holder
    ${TRACE:-trace $FUNC_NAME $@}

    local semaphore=$1
    integer resource_num=$2
    local pid
    local resource=$HOME_DIR/$semaphore/$RESOURCES_DIRNAME/$resource_num

    assert_semaphore $semaphore

    if [[ -L $resource ]]
    then
        pid=$(get_link_target $resource)
        if [[ -n $pid ]]
        then
            if is_non_negative_integer $pid
            then
                echo $pid
                return 0
            fi
        fi
    elif [[ -f $resource ]]
    then
        rm -f $resource # regular files shouldn't exist
    fi
    return 1
}