|
|
sem_get (PHP 3 >= 3.0.6, PHP 4, PHP 5) sem_get -- Get a semaphore id Descriptionresource sem_get ( int key [, int max_acquire [, int perm [, int auto_release]]] )
sem_get() returns an id that can be used to
access the System V semaphore with the given key.
The semaphore is created if necessary using the permission bits specified in
perm (defaults to 0666). The number of processes that can
acquire the semaphore simultaneously is set to max_acquire
(defaults to 1). Actually this value is set only if the process
finds it is the only process currently attached to the semaphore.
Optional parameter auto_release specifies if the
semaphore should be automatically released on request shutdown.
It is available since PHP 4.3.0.
Returns a positive semaphore identifier on success, or FALSE on
error.
A second call to sem_get() for the same key
will return a different semaphore identifier, but both
identifiers access the same underlying semaphore.
See also sem_acquire(),
sem_release(), and ftok().
quickshiftin at gmail dot com
30-Jul-2007 08:55
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.
ein at anti-logic dot com
26-Jun-2007 04:21
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 AT listbid.com
02-May-2003 11:39
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);
|