PHP共享內(nèi)存用法實例分析
本文實例講述了PHP共享內(nèi)存用法。分享給大家供大家參考,具體如下:
共享內(nèi)存主要用于進程間通信
php中的共享內(nèi)存有兩套擴展可以實現(xiàn)
1、shmop 編譯時需要開啟 --enable-shmop 參數(shù)
實例:
$shm_key = ftok(__FILE__, 't');
/**
開辟一塊共享內(nèi)存
int $key , string $flags , int $mode , int $size
$flags: a:訪問只讀內(nèi)存段
c:創(chuàng)建一個新內(nèi)存段,或者如果該內(nèi)存段已存在,嘗試打開它進行讀寫
w:可讀寫的內(nèi)存段
n:創(chuàng)建一個新內(nèi)存段,如果該內(nèi)存段已存在,則會失敗
$mode: 八進制格式 0655
$size: 開辟的數(shù)據(jù)大小 字節(jié)
*/
$shm_id = shmop_open($shm_key, "c", 0644, 1024);
/**
* 寫入數(shù)據(jù) 數(shù)據(jù)必須是字符串格式 , 最后一個指偏移量
* 注意:偏移量必須在指定的范圍之內(nèi),否則寫入不了
*
*/
$size = shmop_write($shm_id, 'songjiankang', 0);
echo "write into {$size}";
#讀取的范圍也必須在申請的內(nèi)存范圍之內(nèi),否則失敗
$data = shmop_read($shm_id, 0, 100);
var_dump($data);
#刪除 只是做一個刪除標志位,同時不在允許新的進程進程讀取,當(dāng)在沒有任何進程讀取時系統(tǒng)會自動刪除
shmop_delete($shm_id);
#關(guān)閉該內(nèi)存段
shmop_close($shm_id);
2、用 Semaphore 擴展中的 sem 類函數(shù) (用起來更方便,類似 key-value 格式)
// Get the file token key
$key = ftok(__DIR__, 'a');
// 創(chuàng)建一個共享內(nèi)存
$shm_id = shm_attach($key, 1024, 777); // resource type
if ($shm_id === false) {
die('Unable to create the shared memory segment');
}
#設(shè)置一個值
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));
#從系統(tǒng)中移除
shm_remove($shm_id);
#關(guān)閉和共享內(nèi)存的連接
shm_detach($shm_id);
注意:這兩種方式不通用的
一個用共享內(nèi)存和信號量實現(xiàn)的消息隊列
/**
* 使用共享內(nèi)存和信號量實現(xiàn)
*
* 支持多進程, 支持各種數(shù)據(jù)類型的存儲
* 注: 完成入隊或出隊操作,盡快使用unset(), 以釋放臨界區(qū)
*
*/
class ShmQueue
{
private $maxQSize = 0; // 隊列最大長度
private $front = 0; // 隊頭指針
private $rear = 0; // 隊尾指針
private $blockSize = 256; // 塊的大小(byte)
private $memSize = 25600; // 最大共享內(nèi)存(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); // 申請進入臨界區(qū)
$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); // 出臨界區(qū), 釋放信號量
}
}
/*
* // 進隊操作 $shmq = new ShmQueue(); $data = 'test data'; $shmq->enQueue($data);
* unset($shmq); // 出隊操作 $shmq = new ShmQueue(); $data = $shmq->deQueue();
* unset($shmq);
*/
linux下 用 ipc命令查看 ,用 ipcrm 命令可以刪除
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《PHP基本語法入門教程》、《PHP錯誤與異常處理方法總結(jié)》、《php程序設(shè)計算法總結(jié)》及《php面向?qū)ο蟪绦蛟O(shè)計入門教程》
希望本文所述對大家PHP程序設(shè)計有所幫助。
相關(guān)文章
PHP的命令行擴展Readline相關(guān)函數(shù)的使用
PHP 作為一個 Web 開發(fā)語言,相對來說,命令行程序并不是它的主戰(zhàn)場。所以很多年輕的 PHP 開發(fā)者可能連命令行腳本都沒有寫過,更別提交互式的命令操作了。而今天,我們帶來的這個擴展就是針對 PHP 的交互式命令行操作的2021-05-05
PHP數(shù)組操作——獲取數(shù)組最后一個值的方法
這篇文章主要介紹了PHP數(shù)組操作——獲取數(shù)組最后一個值的方法,需要的朋友可以參考下2015-04-04
PHP7擴展開發(fā)教程之Hello World實現(xiàn)方法示例
這篇文章主要介紹了PHP7擴展開發(fā)教程之Hello World實現(xiàn)方法,結(jié)合實例形式較為詳細的分析了針對php7擴展的源碼下載、設(shè)置、代碼編輯及測試運行相關(guān)操作技巧,需要的朋友可以參考下2017-08-08

