Home >Backend Development >PHP Tutorial >一段在linux下测试共享内存的程序_PHP

一段在linux下测试共享内存的程序_PHP

WBOY
WBOYOriginal
2016-06-01 12:28:38959browse

1 share.php 分配/删除共享内存






if($stop!=1)

{

// 判断是否已经创建

$shm_id = @shmop_open(0xff3, "a", 0644, 100);

if($shm_id) {

echo "内存块已经创建\n";

exit();

}

// 创建 100 字节的系统标识为 0xff3 的共享内存块

$shm_id = @shmop_open(0xff3, "c", 0644, 100);

if(!$shm_id) {

echo "共享内存块创建失败\n";

}



// 写一些内容

for($i=0;$i
{

$shm_bytes_written = shmop_write($shm_id, "共享块$i", ($i*10));

}

echo "创建成功";

shmop_close($shm_id);

}

else

{

// 是否存在

$shm_id = @shmop_open(0xff3, "a", 0644, 100);

if(!$shm_id) {

echo "没有找到共享内存块\n";

exit();

}

// 删除

if(!shmop_delete($shm_id)) {

echo "删除失败.";

}

shmop_close($shm_id);

echo "内存块删除完毕!";

}

?>



2 share2
.php 用于显示共享内存的内容






// 打开共享内存块 0xff3

$shm_id = @shmop_open(0xff3, "a", 0644, 100);

if(!$shm_id) {

echo "不能打开共享内存块\n";

exit();

}



// 读出里面的内容

$my_string = shmop_read($shm_id, 0, $shm_size);

if(!$my_string) {

echo "没有内容或者不能读\n";

}

echo $my_string."\n";

shmop_close($shm_id);

?&g

Statement:
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