php shmop_open的有关问题

php中文网
发布: 2016-06-13 10:27:10
原创
1213人浏览过

php shmop_open的问题

PHP code
<!--Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--><?php$arr=array();function application($key,$value=""){    global $arr;    if($value=="")    {        $shm_id = @shmop_open(12345, "a", 0644,100);    }else{        @$shm_id = shmop_open(12345, "c",0,0);        if(!$shm_id)        {            $shm_id=shmop_open(12345,"c",0644,100);        }    }    @$byte=shmop_read($shm_id,0,1024*1024);    if(!$byte){        return "";    }else{        if($byte!=""){            $arr=unserialize($byte);        }else{            return "";        }    }    if($value=="")//取值    {        shmop_close($shm_id);        if(array_key_exists($key,$arr))        {            return $arr[$key];        }        return "";    }else{        $arr[$key]=$value;        shmop_write($shm_id,serialize($arr),0);        shmop_close($shm_id);    }}?>
登录后复制


我在php中实现asp中的application对象的功能
目前的问题是在同一个http请求之中,执行了application("a","1"),那么application("a")会返回1,上面这个函数是正常的
但如果开始第二个请求了,application("a")就取不到值了
问题出在哪儿啊?


------解决方案--------------------
shmop_open(12345, "a", 0644,100);
表示最大可能的空间(字节数)
而你 shmop_read($shm_id,0,1024*1024);
读取时就越界了,这就是所谓的内存溢出
------解决方案--------------------
一般可以这样写
PHP code
$p = new shared;//$p-&gt;a = 'abcd';//$p-&gt;b = 1234;print_r($p-&gt;_all);echo $p-&gt;b;class shared {  private $shm_id;  private $shm_key = 0xff3;  private $shm_size = 1024;  function __construct() {    $this-&gt;shm_id = shmop_open($this-&gt;shm_key, "c", 0644, $this-&gt;shm_size) or die('申请失败');  }  function __get($name) {    $buf = shmop_read($this-&gt;shm_id, 0, $this-&gt;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-&gt;shm_id, 0, $this-&gt;shm_size);    $buf = unserialize(trim($buf));    $buf[$name] = $value;    $buf = serialize($buf);    if(strlen($buf) &gt;= $this-&gt;shm_size) die('空间不足');    shmop_write($this-&gt;shm_id, $buf, 0) or die('写入失败');  }}<br><font color="#e78608">------解决方案--------------------</font><br>和你上一个帖子一样,讨论是在基于 php_shmop.dll 扩展的<br><br>linux 中有另外的方法,但原理是一样的<br>php_shmop 也可以在 linux 中编译,但他却是为 window 设计的<br><br><font color="#e78608">------解决方案--------------------</font><br>我就明着告诉你,你这个问题大了.<br><br>共享内存区需要加锁操作, php没法设置进程共享的mutex, 你可以同样适用一个system v 的sem二值信号量来做锁.<div class="clear"></div>
登录后复制
PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号