PHP信號(hào)量基本用法實(shí)例詳解
本文實(shí)例講述了PHP信號(hào)量基本用法。分享給大家供大家參考,具體如下:
一些理論基礎(chǔ):
信號(hào)量:又稱為信號(hào)燈、旗語 用來解決進(jìn)程(線程同步的問題),類似于一把鎖,訪問前獲取鎖(獲取不到則等待),訪問后釋放鎖。
臨界資源:每次僅允許一個(gè)進(jìn)程訪問的資源。
臨界區(qū):每個(gè)進(jìn)程中訪問臨界資源的那段代碼叫臨界區(qū)
進(jìn)程互斥:兩個(gè)或以上的進(jìn)程不能同時(shí)進(jìn)入關(guān)于同一組共享變量的臨界區(qū)域,即一個(gè)進(jìn)程正在訪問臨界資源,另一個(gè)進(jìn)程要想訪問必須等待。
進(jìn)程同步主要研究如何確定數(shù)個(gè)進(jìn)程之間的執(zhí)行順序和避免數(shù)據(jù)競(jìng)爭(zhēng)的問題 即,如何讓多個(gè)進(jìn)程能一塊很好的協(xié)作運(yùn)行
舉例子:(來自百度百科)
以一個(gè)停車場(chǎng)的運(yùn)作為例。簡(jiǎn)單起見,假設(shè)停車場(chǎng)只有三個(gè)車位,一開始三個(gè)車位都是空的。這時(shí)如果同時(shí)來了五輛車,看門人允許其中三輛直接進(jìn)入,然后放下車攔,剩下的車則必須在入口等待,此后來的車也都不得不在入口處等待。這時(shí),有一輛車離開停車場(chǎng),看門人得知后,打開車攔,放入外面的一輛進(jìn)去,如果又離開兩輛,則又可以放入兩輛,如此往復(fù)。
在這個(gè)停車場(chǎng)系統(tǒng)中,車位是公共資源,每輛車好比一個(gè)線程,看門人起的就是信號(hào)量的作用。
$key=ftok(__FILE__,'t'); /** * 獲取一個(gè)信號(hào)量資源 int $key [, int $max_acquire = 1 [, int $perm = 0666 [, int $auto_release = 1 ]]] $max_acquire:最多可以多少個(gè)進(jìn)程同時(shí)獲取信號(hào) $perm:權(quán)限 默認(rèn) 0666 $auto_release:是否自動(dòng)釋放信號(hào)量 */ $sem_id=sem_get($key); #獲取信號(hào) sem_acquire($seg_id); //do something 這里是一個(gè)原子性操作 //釋放信號(hào)量 sem_release($seg_id); //把次信號(hào)從系統(tǒng)中移除 sem_remove($sem_id); //可能出現(xiàn)的問題 $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); } }
共享內(nèi)存+信號(hào) 實(shí)現(xiàn)原子性操作
$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手冊(cè) sem_get 函數(shù)comment
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《PHP基本語法入門教程》、《PHP錯(cuò)誤與異常處理方法總結(jié)》、《php程序設(shè)計(jì)算法總結(jié)》及《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》
希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。
相關(guān)文章
PHP實(shí)現(xiàn)對(duì)數(shù)字分隔加千分號(hào)的方法
今天小編就為大家分享一篇關(guān)于PHP實(shí)現(xiàn)對(duì)數(shù)字分隔加千分號(hào)的方法,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2019-03-03docker?中搭建php環(huán)境經(jīng)驗(yàn)分享
這篇文章主要介紹了docker?中搭建php環(huán)境經(jīng)驗(yàn)分享的相關(guān)資料,需要的朋友可以參考下2023-09-09PHP var關(guān)鍵字相關(guān)原理及使用實(shí)例解析
這篇文章主要介紹了PHP var關(guān)鍵字相關(guān)原理及使用實(shí)例解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-07-07php cookie中點(diǎn)號(hào)(句號(hào))自動(dòng)轉(zhuǎn)為下劃線問題
這篇文章主要介紹了php cookie中點(diǎn)號(hào)(句號(hào))自動(dòng)轉(zhuǎn)為下劃線問題,需要的朋友可以參考下2014-10-10php實(shí)現(xiàn)通用的信用卡驗(yàn)證類
這篇文章主要介紹了php實(shí)現(xiàn)通用的信用卡驗(yàn)證類,涉及信用卡的規(guī)則與php字符串操作的相關(guān)技巧,具有一定參考借鑒價(jià)值,文中有英文原文注釋說明,有助于更直觀的了解源碼相關(guān)信息,需要的朋友可以參考下2015-03-03PHP stripos()函數(shù)及注意事項(xiàng)的分析
本篇文章是對(duì)PHP中的stripos()函數(shù)及注意事項(xiàng)進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06jQuery+php簡(jiǎn)單實(shí)現(xiàn)全選刪除的方法
這篇文章主要介紹了jQuery+php簡(jiǎn)單實(shí)現(xiàn)全選刪除的方法,涉及php結(jié)合jQuery操作表單的全選及ajax交互實(shí)現(xiàn)刪除的相關(guān)技巧,需要的朋友可以參考下2016-11-11