本文主要介绍了PHP信号量基本用法,较为详细的分析了PHP信号量的基本概念、使用技巧与相关注意事项,需要的朋友可以参考下。希望对大家有所帮助。
具体如下:
一些理论基础:
信号量:又称为信号灯、旗语 用来解决进程(线程同步的问题),类似于一把锁,访问前获取锁(获取不到则等待),访问后释放锁。
临界资源:每次仅允许一个进程访问的资源。
临界区:每个进程中访问临界资源的那段代码叫临界区
进程互斥:两个或以上的进程不能同时进入关于同一组共享变量的临界区域,即一个进程正在访问临界资源,另一个进程要想访问必须等待。
进程同步主要研究如何确定数个进程之间的执行顺序和避免数据竞争的问题 即,如何让多个进程能一块很好的协作运行
举例子:(来自百度百科)
以一个停车场的运作为例。简单起见,假设停车场只有三个车位,一开始三个车位都是空的。这时如果同时来了五辆车,看门人允许其中三辆直接进入,然后放下车拦,剩下的车则必须在入口等待,此后来的车也都不得不在入口处等待。这时,有一辆车离开停车场,看门人得知后,打开车拦,放入外面的一辆进去,如果又离开两辆,则又可以放入两辆,如此往复。
在这个停车场系统中,车位是公共资源,每辆车好比一个线程,看门人起的就是信号量的作用。
$key=ftok(__FILE__,'t'); /** * 获取一个信号量资源 int $key [, int $max_acquire = 1 [, int $perm = 0666 [, int $auto_release = 1 ]]] $max_acquire:最多可以多少个进程同时获取信号 $perm:权限 默认 0666 $auto_release:是否自动释放信号量 */ $sem_id=sem_get($key); #获取信号 sem_acquire($seg_id); //do something 这里是一个原子性操作 //释放信号量 sem_release($seg_id); //把次信号从系统中移除 sem_remove($sem_id); //可能出现的问题 $fp = sem_get(fileinode(__DIR__), 100); sem_acquire($fp); $fp2 = sem_get(fileinode(__DIR__), 1)); sem_acquire($fp2);
Implementation of a read-write semaphore in PHP:
class rw_semaphore { const READ_ACCESS = 0; const WRITE_ACCESS = 1; /** * @access private * @var resource - mutex semaphore */ private $mutex; /** * @access private * @var resource - read/write semaphore */ private $resource; /** * @access private * @var int */ private $writers = 0; /** * @access private * @var int */ private $readers = 0; /** * Default constructor * * Initialize the read/write semaphore */ 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); } /** * Destructor * * Remove the read/write semaphore */ public function __destruct() { sem_remove($this->mutex); sem_remove($this->resource); } /** * Request acess to the resource * * @param int $mode * @return void */ private function request_access($access_type = self::READ_ACCESS) { if ($access_type == self::WRITE_ACCESS) { sem_acquire($this->mutex); /* update the writers counter */ $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); } /* update the readers counter */ $this->readers++; sem_release($this->mutex); } } private function request_release($access_type = self::READ_ACCESS) { if ($access_type == self::WRITE_ACCESS) { sem_acquire($this->mutex); /* update the writers counter */ $this->writers--; sem_release($this->mutex); sem_release($this->resource); } else { sem_acquire($this->mutex); /* update the readers counter */ $this->readers--; if ($this->readers == 0) sem_release($this->resource); sem_release($this->mutex); } } /** * Request read access to the resource * * @return void */ public function read_access() { $this->request_access(self::READ_ACCESS); } /** * Release read access to the resource * * @return void */ public function read_release() { $this->request_release(self::READ_ACCESS); } /** * Request write access to the resource * * @return void */ public function write_access() { $this->request_access(self::WRITE_ACCESS); } /** * Release write access to the resource * * @return void */ public function write_release() { $this->request_release(self::WRITE_ACCESS); } }
共享内存+信号 实现原子性操作
$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);
相关推荐:
php之Constructor Prototype Pattern 原型模式详解
Atas ialah kandungan terperinci PHP信号量详解. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!