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
https://forums.phpfreaks.com/topic/156397-solved-testing-semaphore-via-ajax/
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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.