Jump to content

[SOLVED] Testing semaphore via Ajax


stockton

Recommended Posts

I am attempting to set and clear semaphores from php code called via Ajax and the setting seems to be working but the clear fails with the following message.

Warning: sem_release() [function.sem-release]: SysV semaphore 2 (key 0x6b) is not currently acquired

The php I am using is:-

<?php
//This function takes a GUUID like one generated from the /usr/bin/uuidgen program
//(e.g. ab684e1c-15fa-4241-aa53-65dd3bc55bd6) and strips out all the alpha- characters,
//It will then add together all the numbers and return that for use as a VERY semi-unique key.
//This is done this way for the fact that sem_get()  (which is what this was written for) only takes integers for
//the key value.
function GUUIDtoInt($p_GUUID)
    {
    $retVal = 0;
    $stack = array();
    for ($index = 0; $index < strlen($p_GUUID); $index++)
        {
        $char = $p_GUUID{$index};
        if (is_numeric($char))
            {
            array_push($stack, $char);
            }
        }

    $topChar = array_pop($stack);
    $decimalPower = 1;
    while ($topChar != NULL)
        {
        $temp = (int)$topChar;
        $retVal += $temp;
        $topChar = array_pop($stack);
        }

    return $retVal;
    }

// Program starts here
        $Command = $_REQUEST["Command"];
        $intKey = GUUIDtoInt('b6379c52-d96d-418b-83c3-8aedb5e6cf95');
        echo "Testing semaphore code ".$Command." ".$intKey."<br/>";
        if (!$sem_id = sem_get($intKey))
            {
            $Message="Could not get ID for the semaphore.";
            echo $Message;
            exit;
            }
        if ($Command=="set")
            {
            $Message="Got semaphore ID; acquiring...";
            if (!sem_acquire($sem_id))
                {
                $Message.=" failed.";
                echo $Message;
                exit;
                }
            else
                {
                $Message.=" OK Got it.".$sem_id;
                echo $Message;
                exit;
                }
            }
        else
            {
            if (!sem_release($sem_id))
                {
                $Message.=" Release for ".$sem_id." failed. ".$intKey;
                echo $Message;
                exit;
                }

            $Message="Semaphore released. Other scripts should be able to get it now.";
            echo $Message;
            exit;
            }
?>

What am I not understanding and therefore have done wrong?

Link to comment
Share on other sites

If I had applied my mind to the problem before asking the question I would have realised that a semaphore cannot survive the program that created it.

Therefore calling the program via Ajax was a bit thick.

Semaphores are used to stop more than one user from running the same program at the same time.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.