欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

PHP共享內(nèi)存用法實例分析

 更新時間:2016年02月12日 13:18:14   作者:思齊_  
這篇文章主要介紹了PHP共享內(nèi)存用法,結(jié)合實例形式較為詳細的分析了基于共享內(nèi)存實現(xiàn)進程間通信的技巧,需要的朋友可以參考下

本文實例講述了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);
#刪除 只是做一個刪除標志位,同時不在允許新的進程進程讀取,當在沒有任何進程讀取時系統(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)文章

  • PHP5.5基于mysqli連接MySQL數(shù)據(jù)庫和讀取數(shù)據(jù)操作實例詳解

    PHP5.5基于mysqli連接MySQL數(shù)據(jù)庫和讀取數(shù)據(jù)操作實例詳解

    這篇文章主要介紹了PHP5.5基于mysqli連接MySQL數(shù)據(jù)庫和讀取數(shù)據(jù)操作,結(jié)合實例形式詳細分析了php5.5使用mysqli連接、讀取mysql數(shù)據(jù)庫,以及PDO預(yù)處理相關(guān)操作技巧,需要的朋友可以參考下
    2019-02-02
  • php chr() ord()中文截取亂碼問題解決方法

    php chr() ord()中文截取亂碼問題解決方法

    今天看到chr() ord()中文截取亂碼問題這個例子,覺得相當?shù)牟诲e,拿出來和大家分享下,有興趣的朋友可以去試下,看看怎么樣.
    2008-09-09
  • PHP實現(xiàn)的DES加密解密封裝類完整實例

    PHP實現(xiàn)的DES加密解密封裝類完整實例

    這篇文章主要介紹了PHP實現(xiàn)的DES加密解密封裝類,結(jié)合完整實例形式分析了php DES加密解密封裝類的定義與使用技巧,需要的朋友可以參考下
    2017-04-04
  • Ajax+PHP邊學邊練 之五 圖片處理

    Ajax+PHP邊學邊練 之五 圖片處理

    在上一篇中講解了如何通過Ajax提交表單并由PHP處理底層數(shù)據(jù),本篇將主要介紹圖片的上傳與處理。對于文件的上傳很簡單,只需一個Form便可實現(xiàn),再通過PHP將源文件上傳到目標目錄。
    2009-12-12
  • Laravel網(wǎng)站打開速度優(yōu)化的方法匯總

    Laravel網(wǎng)站打開速度優(yōu)化的方法匯總

    最近在學習 Laravel 框架,自己做了個小站,總結(jié)了一些 Laravel 網(wǎng)站性能提升的方法。所以下面這篇文章主要給大家匯總介紹了關(guān)于Laravel網(wǎng)站打開速度優(yōu)化的一些方法,文中介紹的非常詳細,需要的朋友可以參考借鑒,下面來一起看看吧。
    2017-07-07
  • 簡單概括PHP的字符串中單引號與雙引號的區(qū)別

    簡單概括PHP的字符串中單引號與雙引號的區(qū)別

    這篇文章主要介紹了PHP的字符串中單引號與雙引號的區(qū)別,是PHP入門學習中的基礎(chǔ)知識,需要的朋友可以參考下
    2016-05-05
  • Warning:?require():?open_basedir?restriction?in?effect,目錄配置open_basedir報錯問題分析

    Warning:?require():?open_basedir?restriction?in?effect,

    在linux服務(wù)器部署thinkphp5的時候PHP報了Warning:?require():?open_basedir?restriction?in?effect這個錯誤,是因為網(wǎng)站目錄配置錯誤,PHP不能引入其授權(quán)目錄上級及其以上的文件。下面詳細講解如何處理這個問題,需要的朋友可以參考下
    2022-11-11
  • PHP服務(wù)端環(huán)境搭建的圖文教程(分享)

    PHP服務(wù)端環(huán)境搭建的圖文教程(分享)

    下面小編就為大家分享一篇PHP服務(wù)端環(huán)境搭建的圖文教程,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2017-12-12
  • PHP實現(xiàn)ASCII碼與字符串相互轉(zhuǎn)換的方法

    PHP實現(xiàn)ASCII碼與字符串相互轉(zhuǎn)換的方法

    這篇文章主要介紹了PHP實現(xiàn)ASCII碼與字符串相互轉(zhuǎn)換的方法,涉及php字符串的遍歷、替換、編碼轉(zhuǎn)換等相關(guān)操作技巧,需要的朋友可以參考下
    2017-04-04
  • PHP設(shè)計模式之模板方法模式定義與用法詳解

    PHP設(shè)計模式之模板方法模式定義與用法詳解

    這篇文章主要介紹了PHP設(shè)計模式之模板方法模式,結(jié)合實例形式詳細分析了php設(shè)計模式中模板方法模式的概念、原理、定義、用法及相關(guān)注意事項,需要的朋友可以參考下
    2018-04-04

最新評論