stockton Posted May 1, 2009 Share Posted May 1, 2009 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? Quote Link to comment Share on other sites More sharing options...
stockton Posted May 2, 2009 Author Share Posted May 2, 2009 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.