Delicious Bookmark this on Delicious Share on Facebook SlashdotSlashdot It! Digg! Digg



PHP : Function Reference : Semaphore, Shared Memory and IPC Functions : sem_get

sem_get

Get a semaphore id (PHP 4, PHP 5)
resource sem_get ( int key [, int max_acquire [, int perm [, int auto_release]]] )


Code Examples / Notes » sem_get

neofutur

with gentoo php5 you will need to add the USE flag :
sysvipc
see :
http://forums.gentoo.org/viewtopic-t-464175-highlight-semget+php.html
and also :
http://overlays.gentoo.org/proj/php/


quickshiftin

in regards to the last post, it looks like there is a mistake.
well, understandably, the documentation isnt quite clear on a point the post uses to rationalize its claim, allow me to explain.
the purpose of a semaphore is to manage access to a shared resource, so natrually it follows that if multiple attempts to access the same semaphore arent blocking (when expected to), then the api simply isnt being used properly.
the problem is that the documentation does not stipulate what will happen if the max_acquire paramter is varied upon successive invocations of the sem_get method.  so setting it to 100, then to 1 on 2 successive calls will have an undefined behavior.  if the value is kept constant however (and set to 1 for the example), you will find, as i did that the second attempt to acquire the semaphore will block.  NOTE: this does not work when using
php -a
here is the revised sample code:
<?php
$fp1 = sem_get(fileinode('commonResource'), 1);
sem_acquire($fp1);
echo 'got mutex' . PHP_EOL;
$fp2 = sem_get(fileinode('commonResource'), 1);
sem_acquire($fp2);
echo 'got mutex again' . PHP_EOL;
?>
note there are 2 references; $fp1 and $fp2 and you will not see the second message because the script will block forever.


joeldg

Heh, actually the above comment I added is not technically correct, it was more of an idea to display the function.
$SHM_KEY = ftok("/home/joeldg/homeymail/shmtest.php", 'R');
$shmid = sem_get($SHM_KEY, 1024, 0644 | IPC_CREAT);
$data = shm_attach($shmid, 1024);
// we now have our shm segment
// lets place a variable in there
shm_put_var ($data, $inmem, "test");
// now lets get it back. we could be in a forked process and still have
// access to this variable.
printf("shared contents: %s\n", shm_get_var($data, $inmem));
shm_detach($data);


ein

Be aware that there is no way to ensure that you have exclusive access to a lock, despite setting max_acquire=1.
In example,
<?
$fp = sem_get(fileinode('lock_file', 100);
sem_acquire($fp);
$fp2 = sem_get(fileinode('lock_file', 1);
sem_acquire($fp2);
?>
This will not block on the second sem_aquire.  Therefore, if you have functions or processes that utilize shared locks (>1 max_acquire) you will still need to provide a seperate lock mechanism (ie flock) for write access, making the sem_ functions useless.
Some more info, in flock, each reference to the lock file has it's own options (can be shared exclusive blocking non blocking etc), but apparently php's sem functions only support these options per semaphore, not per semaphore-reference.


joeldg

<?
// thanks to
// http://www.ecst.csuchico.edu/~beej/guide/ipc/shmem.html
$SHM_KEY = ftok("/home/joeldg/homeymail/shmtest.php", 'R');
$shmid = sem_get($SHM_KEY, 1024, 0644 | IPC_CREAT);
$data = shm_attach($shmid, 1024);
$data = "test";
printf("shared contents: %s\n", $data);
shm_detach($data);
?>


Change Language


Follow Navioo On Twitter
ftok
msg_get_queue
msg_receive
msg_remove_queue
msg_send
msg_set_queue
msg_stat_queue
sem_acquire
sem_get
sem_release
sem_remove
shm_attach
shm_detach
shm_get_var
shm_put_var
shm_remove_var
shm_remove
eXTReMe Tracker