日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > php >内容正文

php

php 信号量 关闭,PHP信号量基本用法实例详解

發布時間:2025/4/16 php 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 php 信号量 关闭,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信号量基本用法实例详解的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。