当前位置:首页 > PHP教程 > php高级应用 > 列表

PHP共享内存用法实例分析

发布:smiling 来源: PHP粉丝网  添加日期:2021-07-09 13:55:31 浏览: 评论:0 

这篇文章主要介绍了PHP共享内存用法,结合实例形式较为详细的分析了基于共享内存实现进程间通信的技巧,需要的朋友可以参考下。

本文实例讲述了PHP共享内存用法,分享给大家供大家参考,具体如下:

共享内存主要用于进程间通信

php中的共享内存有两套扩展可以实现

1、shmop  编译时需要开启 --enable-shmop 参数

实例:

  1. $shm_key = ftok(__FILE__, 't'); 
  2. /** 
  3.  开辟一块共享内存 
  4. int $key , string $flags , int $mode , int $size  
  5. $flags: a:访问只读内存段 
  6.     c:创建一个新内存段,或者如果该内存段已存在,尝试打开它进行读写 
  7.     w:可读写的内存段 
  8.     n:创建一个新内存段,如果该内存段已存在,则会失败 
  9. $mode: 八进制格式 0655 
  10. $size: 开辟的数据大小 字节 
  11.  */ 
  12. $shm_id = shmop_open($shm_key, "c", 0644, 1024); 
  13. /** 
  14.  * 写入数据 数据必须是字符串格式 , 最后一个指偏移量 
  15.  * 注意:偏移量必须在指定的范围之内,否则写入不了 
  16.  *  
  17.  */ 
  18. $size = shmop_write($shm_id, 'songjiankang', 0); 
  19. echo "write into {$size}"; 
  20. #读取的范围也必须在申请的内存范围之内,否则失败 
  21. $data = shmop_read($shm_id, 0, 100); 
  22. var_dump($data); 
  23. #删除 只是做一个删除标志位,同时不在允许新的进程进程读取,当在没有任何进程读取时系统会自动删除 
  24. shmop_delete($shm_id); 
  25. #关闭该内存段 
  26. shmop_close($shm_id); 

2、用 Semaphore 扩展中的 sem 类函数 (用起来更方便,类似 key-value 格式)

  1. // Get the file token key 
  2. $key = ftok(__DIR__, 'a'); 
  3. // 创建一个共享内存 
  4. $shm_id = shm_attach($key, 1024, 777); // resource type 
  5. if ($shm_id === false) { 
  6.   die('Unable to create the shared memory segment'); 
  7. } 
  8. #设置一个值 
  9. shm_put_var($shm_id, 111, 'value'); 
  10. #删除一个key 
  11. //shm_remove_var($shm_id, 111); 
  12. #获取一个值 
  13. $value = shm_get_var($shm_id, 111); 
  14. var_dump($value); 
  15. #检测一个key是否存在 
  16. // var_dump(shm_has_var($shm_id, 111)); 
  17. #从系统中移除 
  18. shm_remove($shm_id); 
  19. #关闭和共享内存的连接 
  20. shm_detach($shm_id); 

注意:这两种方式不通用的

一个用共享内存和信号量实现的消息队列

  1. /** 
  2. * 使用共享内存和信号量实现 
  3. *  
  4. * 支持多进程, 支持各种数据类型的存储 
  5. * 注: 完成入队或出队操作,尽快使用unset(), 以释放临界区 
  6. * 
  7. */ 
  8. class ShmQueue 
  9. { 
  10.   private $maxQSize = 0; // 队列最大长度 
  11.   private $front = 0; // 队头指针 
  12.   private $rear = 0; // 队尾指针 
  13.   private $blockSize = 256; // 块的大小(byte) 
  14.   private $memSize = 25600; // 最大共享内存(byte) 
  15.   private $shmId = 0; 
  16.   private $filePtr = './shmq.ptr'; 
  17.   private $semId = 0; 
  18.   public function __construct () 
  19.   { 
  20.     $shmkey = ftok(__FILE__, 't'); 
  21.     $this->shmId = shmop_open($shmkey, "c", 0644, $this->memSize); 
  22.     $this->maxQSize = $this->memSize / $this->blockSize; 
  23.     // 申請一个信号量 
  24.     $this->semId = sem_get($shmkey, 1); 
  25.     sem_acquire($this->semId); // 申请进入临界区 
  26.     $this->init(); 
  27.   } 
  28.   private function init () 
  29.   { 
  30.     if (file_exists($this->filePtr)) { 
  31.       $contents = file_get_contents($this->filePtr); 
  32.       $data = explode('|', $contents); 
  33.       if (isset($data[0]) && isset($data[1])) { 
  34.         $this->front = (int) $data[0]; 
  35.         $this->rear = (int) $data[1]; 
  36.       } 
  37.     } 
  38.   } 
  39.   public function getLength () 
  40.   { 
  41.     return (($this->rear - $this->front + $this->memSize) % ($this->memSize)) / 
  42.          $this->blockSize; 
  43.   } 
  44.   public function enQueue ($value) 
  45.   { 
  46.     if ($this->ptrInc($this->rear) == $this->front) { // 队满 
  47.       return false; 
  48.     } 
  49.     $data = $this->encode($value); 
  50.     shmop_write($this->shmId, $data, $this->rear); 
  51.     $this->rear = $this->ptrInc($this->rear); 
  52.     return true; 
  53.   } 
  54.   public function deQueue () 
  55.   { 
  56.     if ($this->front == $this->rear) { // 队空 
  57.       return false; 
  58.     } 
  59.     $value = shmop_read($this->shmId, $this->front, $this->blockSize - 1); 
  60.     $this->front = $this->ptrInc($this->front); 
  61.     return $this->decode($value); 
  62.   } 
  63.   private function ptrInc ($ptr) 
  64.   { 
  65.     return ($ptr + $this->blockSize) % ($this->memSize); 
  66.   } 
  67.   private function encode ($value) 
  68.   { 
  69.     $data = serialize($value) . "__eof"; 
  70.     echo ''; 
  71.     echo strlen($data); 
  72.     echo ''; 
  73.     echo $this->blockSize - 1; 
  74.     echo ''; 
  75.     if (strlen($data) > $this->blockSize - 1) { 
  76.       throw new Exception(strlen($data) . " is overload block size!"); 
  77.     } 
  78.     return $data; 
  79.   } 
  80.   private function decode ($value) 
  81.   { 
  82.     $data = explode("__eof", $value); 
  83.     return unserialize($data[0]); 
  84.   } 
  85.   public function __destruct () 
  86.   { 
  87.     $data = $this->front . '|' . $this->rear; 
  88.     file_put_contents($this->filePtr, $data); 
  89.     sem_release($this->semId); // 出临界区, 释放信号量 
  90.   } 
  91. } 
  92. /* 
  93.  * // 进队操作 $shmq = new ShmQueue(); $data = 'test data'; $shmq->enQueue($data); 
  94.  * unset($shmq); // 出队操作 $shmq = new ShmQueue(); $data = $shmq->deQueue(); 
  95.  * unset($shmq); 
  96.  */ 

linux下 用 ipc命令查看 ,用 ipcrm 命令可以删除

Tags: PHP共享内存

分享到: