时间:2021-07-01 10:21:17 帮助过:4人阅读
$p = new shared; //$p->a = 'abcd'; //$p->b = 1234; print_r($p->_all); echo $p->b; class shared { private $shm_id; private $shm_key = 0xff3; private $shm_size = 1024; function __construct() { $this->shm_id = shmop_open($this->shm_key, "c", 0644, $this->shm_size) or die('申请失败'); } function __get($name) { $buf = shmop_read($this->shm_id, 0, $this->shm_size); $buf = unserialize(trim($buf)); if($name == '_all') return $buf; return isset($buf[$name]) ? $buf[$name] : false; } function __set($name, $value) { $buf = shmop_read($this->shm_id, 0, $this->shm_size); $buf = unserialize(trim($buf)); $buf[$name] = $value; $buf = serialize($buf); if(strlen($buf) >= $this->shm_size) die('空间不足'); shmop_write($this->shm_id, $buf, 0) or die('写入失败'); } }
------解决方案--------------------
和你上一个帖子一样,讨论是在基于 php_shmop.dll 扩展的
linux 中有另外的方法,但原理是一样的
php_shmop 也可以在 linux 中编译,但他却是为 window 设计的
------解决方案--------------------
我就明着告诉你,你这个问题大了.
共享内存区需要加锁操作, php没法设置进程共享的mutex, 你可以同样适用一个system v 的sem二值信号量来做锁.