我们有一个由dhcp服务器在某些事件上执行的php脚本。 如果有很多用户,并且因为脚本具有某些依赖性,它们只能逐个被激活,这些事件可能会经常发生。为实现这一点,我们使用信号量,这就是它的实现方式。
public function getGlobalLock() {
$this->semaphore = sem_get($this->sem_key, 1, $this->sem_perm, 1);
if(!$this->semaphore) {
// throw exception because we always should be able to do this!
// this is a serious error
throw new Exception("Error. Locking was not possible, unable to retrieve semaphore key. PID " . $this->pid . ", purpose for lock is: " . $this->purpose);
}
// blocks while waiting for semaphore
if(!sem_acquire($this->semaphore)) {
// throw exception because we always should be able to do this!
// serious error
throw new Exception("Error. Locking was not possible, unable to acquire semaphore. PID " . $this->pid . ", purpose for lock is: " . $this->purpose);
}
}
现在的问题是,我们遇到了一些问题,脚本表现得很奇怪,而且有人认为信号量不像我们期望的那样工作。 任何人都可以验证这应该保证顺序订单吗?