欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

PHP信号量基本用法实例详解

程序员文章站 2024-04-01 17:33:04
本文实例讲述了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手册  sem_get 函数comment

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

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