Home Backend Development PHP Tutorial php shmop_open的有关问题

php shmop_open的有关问题

Jun 13, 2016 am 10:27 AM
quot shmop this

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);    }}?>
Copy after login


我在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->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('写入失败');  }}<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>
Copy after login
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Detailed example of how PHP creates shared memory to reduce load Detailed example of how PHP creates shared memory to reduce load Nov 03, 2022 pm 05:03 PM

This article will introduce to you how to create shared memory in PHP to reduce load. Here you need to understand a function shmop. Let’s follow the article to learn~

php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决 php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决 Jun 13, 2016 am 10:23 AM

php提交表单通过后,弹出的对话框怎样在当前页弹出php提交表单通过后,弹出的对话框怎样在当前页弹出而不是在空白页弹出?想实现这样的效果:而不是空白页弹出:------解决方案--------------------如果你的验证用PHP在后端,那么就用Ajax;仅供参考:HTML code

An article that understands this point and catches up with 70% of front-end people An article that understands this point and catches up with 70% of front-end people Sep 06, 2022 pm 05:03 PM

A colleague got stuck due to a bug pointed by this. Vue2’s this pointing problem caused an arrow function to be used, resulting in the inability to get the corresponding props. He didn't know it when I introduced it to him, and then I deliberately looked at the front-end communication group. So far, at least 70% of front-end programmers still don't understand it. Today I will share with you this link. If everything is wrong If you haven’t learned it yet, please give me a big mouth.

Let's talk about why Vue2 can access properties in various options through this Let's talk about why Vue2 can access properties in various options through this Dec 08, 2022 pm 08:22 PM

This article will help you interpret the vue source code and introduce why you can use this to access properties in various options in Vue2. I hope it will be helpful to everyone!

What is this? An in-depth analysis of this in JavaScript What is this? An in-depth analysis of this in JavaScript Aug 04, 2022 pm 05:02 PM

What is this? The following article will introduce you to this in JavaScript, and talk about the differences between this in different calling methods of functions. I hope it will be helpful to you!

How to use this method in Java How to use this method in Java Apr 18, 2023 pm 01:58 PM

1. this keyword 1. Type of this: Which object is called is the reference type of that object 2. Usage summary 1. this.data;//Access attribute 2. this.func();//Access method 3.this( );//Call other constructors in this class 3. Explanation of usage 1.this.data is used in member methods. Let us see what will happen if this is not added classMyDate{publicintyear;publicintmonth;publicintday; publicvoidsetDate(intyear,intmonth,intday){ye

Clever way to use this keyword in jQuery Clever way to use this keyword in jQuery Feb 25, 2024 pm 04:09 PM

Flexible use of this keyword in jQuery In jQuery, the this keyword is a very important and flexible concept. It is used to refer to the DOM element currently being manipulated. By rationally using this keyword, we can easily operate elements on the page and achieve various interactive effects and functions. This article will combine specific code examples to introduce the flexible use of this keyword in jQuery. Simple this example First, let's look at a simple this example. Suppose we have a

Detailed explanation of this in JavaScript arrow function Detailed explanation of this in JavaScript arrow function Jan 25, 2024 pm 01:41 PM

The arrow function in JavaScript is a relatively new syntax. It does not have its own this keyword. On the contrary, the this of the arrow function points to the scope object containing it. The impacts are: 1. This in the arrow function is static; 2. Arrow Functions cannot be used as constructors; 3. Arrow functions cannot be used as methods.

See all articles