共享内存 Shared memory examples, there are few examples to be found just about anywhere on this subject, I wrote these up a few months ago and am posting them here in case anyone might need these. <? //shared memory example.. hope this helps // return a memory hex segment for string.. // limit four chars in Joel = 0x4a6f656c (joel is my name) function memsegspot($str){ $str = str_pad($str, 4, "Z"); // Z = 5a for ($a=0; $a<4; $a++){ $out1 = substr($str, $a , 1); // walk through $out .= dechex(ord($out1)); // ord returns dec, we need hex for shared memory segments }// return ("0x".$out); // prepend the }//end function // memory location and size of location to alloc function attachtomem($memloc, $alloc){ $sem_id=sem_get($memloc, 1); sem_acquire($sem_id); $shm = shm_attach($memloc, $alloc); return $shm; }// end function $test = serialize("If you can read this, it is coming out of shared memory.."); $ms = ((strlen($test)+44)*2); $sem_id = sem_get(memsegspot("test"), 1); sem_acquire($sem_id); $smh = shm_attach(memsegspot("test"), $ms); echo "Attached to shm # $smhn"; shm_put_var($smh, $joi{md5($test)}, $test); // $shm_variable is the variable name, $test = the data shm_put_var($smh, $another, serialize("some data")); echo "placed data in shm.n";
手册上这么写的操作系统的锁怎么调用呢
难道写一个extension?
不过只能在unix下用
内存管理已经有方案了
就是线程同步不好办
就是不知道怎么在php当中实现。。
如设置一块内存共享。在php怎搞
Shared memory examples, there are few examples to be found just about anywhere on this subject, I wrote these up a few months ago and am posting them here in case anyone might need these.
<?
//shared memory example.. hope this helps // return a memory hex segment for string..
// limit four chars in Joel = 0x4a6f656c (joel is my name)
function memsegspot($str){
$str = str_pad($str, 4, "Z"); // Z = 5a
for ($a=0; $a<4; $a++){
$out1 = substr($str, $a , 1); // walk through
$out .= dechex(ord($out1)); // ord returns dec, we need hex for shared memory segments
}//
return ("0x".$out); // prepend the
}//end function // memory location and size of location to alloc
function attachtomem($memloc, $alloc){
$sem_id=sem_get($memloc, 1);
sem_acquire($sem_id);
$shm = shm_attach($memloc, $alloc);
return $shm;
}// end function $test = serialize("If you can read this, it is coming out of shared memory..");
$ms = ((strlen($test)+44)*2);
$sem_id = sem_get(memsegspot("test"), 1);
sem_acquire($sem_id);
$smh = shm_attach(memsegspot("test"), $ms);
echo "Attached to shm # $smhn";
shm_put_var($smh, $joi{md5($test)}, $test); // $shm_variable is the variable name, $test = the data
shm_put_var($smh, $another, serialize("some data"));
echo "placed data in shm.n";
$out = @shm_get_var($smh, ${md5($test)}); echo unserialize($out) . " n stored as: " . md5($out)."n";
@shm_detach($sem_id);
echo "detached from shared memory..n";
echo $ms . " bytes memory used.n";
?>