PHP共用記憶體用法執行個體分析,php共用執行個體分析
本文執行個體講述了PHP共用記憶體用法。分享給大家供大家參考,具體如下:
共用記憶體主要用於處理序間通訊
php中的共用記憶體有兩套擴充可以實現
1、shmop 編譯時間需要開啟 --enable-shmop 參數
執行個體:
$shm_key = ftok(__FILE__, 't');/** 開闢一塊共用記憶體int $key , string $flags , int $mode , int $size $flags: a:訪問唯讀記憶體段 c:建立一個新記憶體段,或者如果該記憶體段已存在,嘗試開啟它進行讀寫 w:可讀寫的記憶體段 n:建立一個新記憶體段,如果該記憶體段已存在,則會失敗$mode: 八進位格式 0655$size: 開闢的資料大小 位元組 */$shm_id = shmop_open($shm_key, "c", 0644, 1024);/** * 寫入資料 資料必須是字串格式 , 最後一個指位移量 * 注意:位移量必須在指定的範圍之內,否則寫入不了 * */$size = shmop_write($shm_id, 'songjiankang', 0);echo "write into {$size}";#讀取的範圍也必須在申請的記憶體範圍之內,否則失敗$data = shmop_read($shm_id, 0, 100);var_dump($data);#刪除 只是做一個刪除標誌位,同時不在允許新的進程進程讀取,當在沒有任何進程讀取時系統會自動刪除shmop_delete($shm_id);#關閉該記憶體段shmop_close($shm_id);
2、用 Semaphore 擴充中的 sem 類函數 (用起來更方便,類似 key-value 格式)
// Get the file token key$key = ftok(__DIR__, 'a');// 建立一個共用記憶體$shm_id = shm_attach($key, 1024, 777); // resource typeif ($shm_id === false) { die('Unable to create the shared memory segment');}#設定一個值shm_put_var($shm_id, 111, 'value');#刪除一個key//shm_remove_var($shm_id, 111);#擷取一個值$value = shm_get_var($shm_id, 111);var_dump($value);#檢測一個key是否存在// var_dump(shm_has_var($shm_id, 111));#從系統中移除shm_remove($shm_id);#關閉和共用記憶體的串連shm_detach($shm_id);
注意:這兩種方式不通用的
一個用共用記憶體和訊號量實現的訊息佇列
/*** 使用共用記憶體和訊號量實現* * 支援多進程, 支援各種資料類型的儲存* 注: 完成入隊或出隊操作,儘快使用unset(), 以釋放臨界區**/class ShmQueue{ private $maxQSize = 0; // 隊列最大長度 private $front = 0; // 隊頭指標 private $rear = 0; // 隊尾指標 private $blockSize = 256; // 塊的大小(byte) private $memSize = 25600; // 最大共用記憶體(byte) private $shmId = 0; private $filePtr = './shmq.ptr'; private $semId = 0; public function __construct () { $shmkey = ftok(__FILE__, 't'); $this->shmId = shmop_open($shmkey, "c", 0644, $this->memSize); $this->maxQSize = $this->memSize / $this->blockSize; // 申請一個訊號量 $this->semId = sem_get($shmkey, 1); sem_acquire($this->semId); // 申請進入臨界區 $this->init(); } private function init () { if (file_exists($this->filePtr)) { $contents = file_get_contents($this->filePtr); $data = explode('|', $contents); if (isset($data[0]) && isset($data[1])) { $this->front = (int) $data[0]; $this->rear = (int) $data[1]; } } } public function getLength () { return (($this->rear - $this->front + $this->memSize) % ($this->memSize)) / $this->blockSize; } public function enQueue ($value) { if ($this->ptrInc($this->rear) == $this->front) { // 隊滿 return false; } $data = $this->encode($value); shmop_write($this->shmId, $data, $this->rear); $this->rear = $this->ptrInc($this->rear); return true; } public function deQueue () { if ($this->front == $this->rear) { // 隊空 return false; } $value = shmop_read($this->shmId, $this->front, $this->blockSize - 1); $this->front = $this->ptrInc($this->front); return $this->decode($value); } private function ptrInc ($ptr) { return ($ptr + $this->blockSize) % ($this->memSize); } private function encode ($value) { $data = serialize($value) . "__eof"; echo ''; echo strlen($data); echo ''; echo $this->blockSize - 1; echo ''; if (strlen($data) > $this->blockSize - 1) { throw new Exception(strlen($data) . " is overload block size!"); } return $data; } private function decode ($value) { $data = explode("__eof", $value); return unserialize($data[0]); } public function __destruct () { $data = $this->front . '|' . $this->rear; file_put_contents($this->filePtr, $data); sem_release($this->semId); // 出臨界區, 釋放訊號量 }}/* * // 進隊操作 $shmq = new ShmQueue(); $data = 'test data'; $shmq->enQueue($data); * unset($shmq); // 出隊操作 $shmq = new ShmQueue(); $data = $shmq->deQueue(); * unset($shmq); */
linux下 用 ipc命令查看 ,用 ipcrm 命令可以刪除
更多關於PHP相關內容感興趣的讀者可查看本站專題:《PHP基本文法入門教程》、《PHP錯誤與異常處理方法總結》、《php程式設計演算法總結》及《php物件導向程式設計入門教程》
希望本文所述對大家PHP程式設計有所協助。
您可能感興趣的文章:
- 單台伺服器的PHP進程之間實現共用記憶體的方法
- php共用記憶體段樣本分享
- php跨域cookie共用使用方法
- PHP通過session id 實現session共用和登入驗證的代碼
- PHP 實現多伺服器共用 SESSION 資料
- PHP實現多伺服器session共用之NFS共用的方法
- 用PHP實現多伺服器共用SESSION資料的方法
- PHP下操作Linux訊息佇列完成處理序間通訊的方法
http://www.bkjia.com/PHPjc/1099077.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/1099077.htmlTechArticlePHP共用記憶體用法執行個體分析,php共用執行個體分析 本文執行個體講述了PHP共用記憶體用法。分享給大家供大家參考,具體如下: 共用記憶體主要用於進程...