用户评论:
[#1]
kakkau at grr dot la [2015-10-20 21:35:48]
For those that encounter strange behavior in using sem_acquire() on resources generated by sem_get(). Have a look at sem_get()'s 4th parameter auto_release. It allows multiple acquisitions through reassignments to resource variables.
./multi.acquire.php
private$key=null;
private$res=null;
public function__construct() {$this->key=ftok(".",".");$this->set_res();$this->acquire();
}
public functionset_res() {// 4th parameter auto_released is 1 by default$this->res=sem_get($this->key,1,0600,1);
}
public functionacquire() {
echo"acquired='".sem_acquire($this->res,true)."'\n";
}
}$s= newSem();$s->set_res();$s->acquire();?>
$ php multi.acquire.php
acquired='1'
acquired='1'
To avoid reacquiring by default set sem_get()'s parameter auto_release to 0 or check if your resource variable is already set, e.g. by using is_null().
[#2]
Michael Z. [2011-08-25 06:37:47]
Watch out when you use fileinode() to get a unique semaphore key (as suggested in some comment on this or a related function) in conjunction with version control software: It seems, for example, SVN will change the inode. Using such a file will leave you with your mutex not working reliably and your system's semaphore pool being filled until further attempts to get a semaphore will fail. Use ipcs and ipcrm commands from linux-util-ng (on most distros probably) to examine/fix related problems.
[#3]
soger [2011-03-28 08:20:56]
Actually it looks like the semaphore is automatically released not on request shutdown but when the variable you store it's resource ID is freed. That is a very big difference.
[#4]
pail dot luo at gmail dot com [2009-02-26 18:40:05]
A very simple to introduce semaphore...
$SEMKey="123456";## Get Semaphore id$seg=sem_get($SEMKey,2,0666, -1) ;
if ($argv[1]=="remove") {sem_remove($seg);
}
echo"Try to acquire ..."sem_acquire($seg);
echo"Acquired...\n";
echo"Press Any Key to continue...\n";$fh=fopen("php://stdin","r");$a=fgets($fh);fclose($fh);sem_release($seg);?>
[#5]
cyrus dot drive at gmail dot com [2008-05-01 17:51:02]
Implementation of a read-write semaphore in PHP:
constREAD_ACCESS=0;
constWRITE_ACCESS=1;private$mutex;private$resource;private$writers=0;private$readers=0;public function__construct() {$mutex_key=ftok('/home/cyrus/development/php/sysvipc/rw_semaphore.php','m');$resource_key=ftok('/home/cyrus/development/php/sysvipc/rw_semaphore.php','r');$this->mutex=sem_get($mutex_key,1);$this->resource=sem_get($resource_key,1);
}public function__destruct() {sem_remove($this->mutex);sem_remove($this->resource);
}private functionrequest_access($access_type=self::READ_ACCESS) {
if ($access_type==self::WRITE_ACCESS) {sem_acquire($this->mutex);$this->writers++;sem_release($this->mutex);sem_acquire($this->resource);
} else {sem_acquire($this->mutex);
if ($this->writers>0||$this->readers==0) {sem_release($this->mutex);sem_acquire($this->resource);sem_acquire($this->mutex);
}$this->readers++;sem_release($this->mutex);
}
}
private functionrequest_release($access_type=self::READ_ACCESS) {
if ($access_type==self::WRITE_ACCESS) {sem_acquire($this->mutex);$this->writers--;sem_release($this->mutex);sem_release($this->resource);
} else {sem_acquire($this->mutex);$this->readers--;
if ($this->readers==0)sem_release($this->resource);sem_release($this->mutex);
}
}public functionread_access() {$this->request_access(self::READ_ACCESS); }public functionread_release() {$this->request_release(self::READ_ACCESS); }public functionwrite_access() {$this->request_access(self::WRITE_ACCESS); }public functionwrite_release() {$this->request_release(self::WRITE_ACCESS); }
}?>
[#6]
ein at anti-logic dot com [2007-06-26 04:21:47]
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.
[#7]
neofutur [2006-08-28 12:49:06]
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/
[#8]
joeldg AT listbid.com [2003-05-02 11:39:57]
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);
[#9]
joeldg at listbid.com [2003-05-02 00:36:12]
// 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);?>