PHP信号量基本用法实例详解_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; $this->resource = sem_get; } /** * Destructor * * Remove the read/write semaphore */ public function __destruct() { sem_remove; sem_remove; } /** * 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; /* update the writers counter */ $this->writers++; sem_release; sem_acquire; } else { sem_acquire; if ($this->writers > 0 || $this->readers == 0) { sem_release; sem_acquire; sem_acquire; } /* update the readers counter */ $this->readers++; sem_release; } } private function request_release($access_type = self::READ_ACCESS) { if ($access_type == self::WRITE_ACCESS) { sem_acquire; /* update the writers counter */ $this->writers--; sem_release; sem_release; } else { sem_acquire; /* update the readers counter */ $this->readers--; if  sem_release; sem_release; } } /** * Request read access to the resource * * @return void */ public function read_access() { $this->request_access; } /** * Release read access to the resource * * @return void */ public function read_release() { $this->request_release; } /** * Request write access to the resource * * @return void */ public function write_access() { $this->request_access; } /** * Release write access to the resource * * @return void */ public function write_release() { $this->request_release; }}

共享内存+信号 实现原子性操作

信号量:又称为信号灯、旗语
用来解决进程,类似于一把锁,访问前获取锁,访问后释放锁。临界资源:每次仅允许一个进程访问的资源。临界区:每个进程中访问临界资源的那段代码叫临界区进程互斥:两个或以上的进程不能同时进入关于同一组共享变量的临界区域,即一个进程正在访问临界资源,另一个进程要想访问必须等待。进程同步PHP信号量基本用法实例详解_php技巧_脚本之家。主要研究如何确定数个进程之间的执行顺序和避免数据竞争的问题
即,如何让多个进程能一块很好的协作运行

本文实例讲述了PHP信号量基本用法。分享给大家供大家参考,具体如下:

$SHM_KEY = ftok("/home/joeldg/homeymail/shmtest.php", 'R');$shmid = sem_get($SHM_KEY, 1024, 0644 | IPC_CREAT);$data = shm_attach;// we now have our shm segment// lets place a variable in thereshm_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: %sn", shm_get_var;shm_detach; 

更多关于PHP相关内容感兴趣的读者可查看本站专题:《PHP基本语法入门教程》、《PHP错误与异常处理方法总结》、《php程序设计算法总结》及《php面向对象程序设计入门教程》

以一个停车场的运作为例。简单起见,假设停车场只有三个车位,一开始三个车位都是空的。这时如果同时来了五辆车,看门人允许其中三辆直接进入,然后放下车拦,剩下的车则必须在入口等待,此后来的车也都不得不在入口处等待。这时,有一辆车离开停车场,看门人得知后,打开车拦,放入外面的一辆进去,如果又离开两辆,则又可以放入两辆,如此往复。

Implementation of a read-write semaphore in PHP:

一些理论基础:

$key=ftok;/** * 获取一个信号量资源 int $key [, int $max_acquire = 1 [, int $perm = 0666 [, int $auto_release = 1 ]]] $max_acquire:最多可以多少个进程同时获取信号 $perm:权限 默认 0666 $auto_release:是否自动释放信号量 */$sem_id=sem_get;#获取信号sem_acquire;//do something 这里是一个原子性操作//释放信号量sem_release;//把次信号从系统中移除sem_remove;//可能出现的问题$fp = sem_get, 100);sem_acquire;$fp2 = sem_get, 1));sem_acquire;

希望本文所述对大家PHP程序设计有所帮助。

在这个停车场系统中,车位是公共资源,每辆车好比一个线程,看门人起的就是信号量的作用。

以上列子来源php手册 sem_get 函数comment

相关文章

发表评论

电子邮件地址不会被公开。 必填项已用*标注

网站地图xml地图