PHP訊號量基本用法執行個體詳解

來源:互聯網
上載者:User
本文執行個體講述了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 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: %s\n", shm_get_var($data, $inmem));shm_detach($data); 

以上列子來源php手冊 sem_get 函數comment

更多關於PHP相關內容感興趣的讀者可查看本站專題:《PHP基本文法入門教程》、《PHP錯誤與異常處理方法總結》、《php程式設計演算法總結》及《php物件導向程式設計入門教程》

希望本文所述對大家PHP程式設計有所協助。

以上就介紹了PHP訊號量基本用法執行個體詳解,包括了方面的內容,希望對PHP教程有興趣的朋友有所協助。

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.