php shmop_open的有關問題

來源:互聯網
上載者:User
php shmop_open的問題
PHP code



我在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('寫入失敗');  }}
------解決方案--------------------
和你上一個文章一樣,討論是在基於 php_shmop.dll 擴充的

linux 中有另外的方法,但原理是一樣的
php_shmop 也可以在 linux 中編譯,但他卻是為 window 設計的

------解決方案--------------------
我就明著告訴你,你這個問題大了.

共用記憶體區需要加鎖操作, php沒法設定進程共用的mutex, 你可以同樣適用一個system v 的sem二值訊號量來做鎖.
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.