一段在linux下测试共享内存的程序
时间:2007-02-17 来源:PHP爱好者
1 share.php 分配/删除共享内存
chi na it p owe r .comn8kcrr87WPyOiBt0sqothKdo0
chi na it p owe r .comn8kcrr87WPyOiBt0sqothKdo0
<?php
chi na it p owe r .comn8kcrr87WPyOiBt0sqothKdo0
chi na it p owe r .comn8kcrr87WPyOiBt0sqothKdo0
if($stop!=1)
chi na it p owe r .comn8kcrr87WPyOiBt0sqothKdo0
{
chi na it p owe r .comn8kcrr87WPyOiBt0sqothKdo0
// 判断是否已经创建
chi na it p owe r .comn8kcrr87WPyOiBt0sqothKdo0
$shm_id = @shmop_open(0xff3, "a", 0644, 100);
chi na it p owe r .comn8kcrr87WPyOiBt0sqothKdo0
if($shm_id) {
chi na it p owe r .comn8kcrr87WPyOiBt0sqothKdo0
echo "内存块已经创建n";
chi na it p owe r .comn8kcrr87WPyOiBt0sqothKdo0
exit();
chi na it p owe r .comn8kcrr87WPyOiBt0sqothKdo0
}
chi na it p owe r .comn8kcrr87WPyOiBt0sqothKdo0
// 创建 100 字节的系统标识为 0xff3 的共享内存块
chi na it p owe r .comn8kcrr87WPyOiBt0sqothKdo0
$shm_id = @shmop_open(0xff3, "c", 0644, 100);
chi na it p owe r .comn8kcrr87WPyOiBt0sqothKdo0
if(!$shm_id) {
chi na it p owe r .comn8kcrr87WPyOiBt0sqothKdo0
echo "共享内存块创建失败n";
chi na it p owe r .comn8kcrr87WPyOiBt0sqothKdo0
}
chi na it p owe r .comn8kcrr87WPyOiBt0sqothKdo0
chi na it p owe r .comn8kcrr87WPyOiBt0sqothKdo0
// 写一些内容
chi na it p owe r .comn8kcrr87WPyOiBt0sqothKdo0
for($i=0;$i<10;$i++)
chi na it p owe r .comn8kcrr87WPyOiBt0sqothKdo0
{
chi na it p owe r .comn8kcrr87WPyOiBt0sqothKdo0
$shm_bytes_written = shmop_write($shm_id, "共享块$i", ($i*10));
chi na it p owe r .comn8kcrr87WPyOiBt0sqothKdo0
}
chi na it p owe r .comn8kcrr87WPyOiBt0sqothKdo0
echo "创建成功";
chi na it p owe r .comn8kcrr87WPyOiBt0sqothKdo0
shmop_close($shm_id);
chi na it p owe r .comn8kcrr87WPyOiBt0sqothKdo0
}
chi na it p owe r .comn8kcrr87WPyOiBt0sqothKdo0
else
chi na it p owe r .comn8kcrr87WPyOiBt0sqothKdo0
{
chi na it p owe r .comn8kcrr87WPyOiBt0sqothKdo0
// 是否存在
chi na it p owe r .comn8kcrr87WPyOiBt0sqothKdo0
$shm_id = @shmop_open(0xff3, "a", 0644, 100);
chi na it p owe r .comn8kcrr87WPyOiBt0sqothKdo0
if(!$shm_id) {
chi na it p owe r .comn8kcrr87WPyOiBt0sqothKdo0
echo "没有找到共享内存块n";
chi na it p owe r .comn8kcrr87WPyOiBt0sqothKdo0
exit();
chi na it p owe r .comn8kcrr87WPyOiBt0sqothKdo0
}
chi na it p owe r .comn8kcrr87WPyOiBt0sqothKdo0
// 删除
chi na it p owe r .comn8kcrr87WPyOiBt0sqothKdo0
if(!shmop_delete($shm_id)) {
chi na it p owe r .comn8kcrr87WPyOiBt0sqothKdo0
echo "删除失败.";
chi na it p owe r .comn8kcrr87WPyOiBt0sqothKdo0
}
chi na it p owe r .comn8kcrr87WPyOiBt0sqothKdo0
shmop_close($shm_id);
chi na it p owe r .comn8kcrr87WPyOiBt0sqothKdo0
echo "内存块删除完毕!";
chi na it p owe r .comn8kcrr87WPyOiBt0sqothKdo0
}
chi na it p owe r .comn8kcrr87WPyOiBt0sqothKdo0
?>
chi na it p owe r .comn8kcrr87WPyOiBt0sqothKdo0
chi na it p owe r .comn8kcrr87WPyOiBt0sqothKdo0
2 share2.php 用于显示共享内存的内容
chi na it p owe r .comn8kcrr87WPyOiBt0sqothKdo0
chi na it p owe r .comn8kcrr87WPyOiBt0sqothKdo0
<?php
chi na it p owe r .comn8kcrr87WPyOiBt0sqothKdo0
chi na it p owe r .comn8kcrr87WPyOiBt0sqothKdo0
// 打开共享内存块 0xff3
chi na it p owe r .comn8kcrr87WPyOiBt0sqothKdo0
$shm_id = @shmop_open(0xff3, "a", 0644, 100);
chi na it p owe r .comn8kcrr87WPyOiBt0sqothKdo0
if(!$shm_id) {
chi na it p owe r .comn8kcrr87WPyOiBt0sqothKdo0
echo "不能打开共享内存块n";
chi na it p owe r .comn8kcrr87WPyOiBt0sqothKdo0
exit();
chi na it p owe r .comn8kcrr87WPyOiBt0sqothKdo0
}
chi na it p owe r .comn8kcrr87WPyOiBt0sqothKdo0
chi na it p owe r .comn8kcrr87WPyOiBt0sqothKdo0
// 读出里面的内容
chi na it p owe r .comn8kcrr87WPyOiBt0sqothKdo0
$my_string = shmop_read($shm_id, 0, $shm_size);
chi na it p owe r .comn8kcrr87WPyOiBt0sqothKdo0
if(!$my_string) {
chi na it p owe r .comn8kcrr87WPyOiBt0sqothKdo0
echo "没有内容或者不能读n";
chi na it p owe r .comn8kcrr87WPyOiBt0sqothKdo0
}
chi na it p owe r .comn8kcrr87WPyOiBt0sqothKdo0
echo $my_string."n";
chi na it p owe r .comn8kcrr87WPyOiBt0sqothKdo0
shmop_close($shm_id);
chi na it p owe r .comn8kcrr87WPyOiBt0sqothKdo0
?>
php爱好者 站 http://www.phpfans.net 网页制作|网站建设|数据采集.
chi na it p owe r .comn8kcrr87WPyOiBt0sqothKdo0
chi na it p owe r .comn8kcrr87WPyOiBt0sqothKdo0
<?php
chi na it p owe r .comn8kcrr87WPyOiBt0sqothKdo0
chi na it p owe r .comn8kcrr87WPyOiBt0sqothKdo0
if($stop!=1)
chi na it p owe r .comn8kcrr87WPyOiBt0sqothKdo0
{
chi na it p owe r .comn8kcrr87WPyOiBt0sqothKdo0
// 判断是否已经创建
chi na it p owe r .comn8kcrr87WPyOiBt0sqothKdo0
$shm_id = @shmop_open(0xff3, "a", 0644, 100);
chi na it p owe r .comn8kcrr87WPyOiBt0sqothKdo0
if($shm_id) {
chi na it p owe r .comn8kcrr87WPyOiBt0sqothKdo0
echo "内存块已经创建n";
chi na it p owe r .comn8kcrr87WPyOiBt0sqothKdo0
exit();
chi na it p owe r .comn8kcrr87WPyOiBt0sqothKdo0
}
chi na it p owe r .comn8kcrr87WPyOiBt0sqothKdo0
// 创建 100 字节的系统标识为 0xff3 的共享内存块
chi na it p owe r .comn8kcrr87WPyOiBt0sqothKdo0
$shm_id = @shmop_open(0xff3, "c", 0644, 100);
chi na it p owe r .comn8kcrr87WPyOiBt0sqothKdo0
if(!$shm_id) {
chi na it p owe r .comn8kcrr87WPyOiBt0sqothKdo0
echo "共享内存块创建失败n";
chi na it p owe r .comn8kcrr87WPyOiBt0sqothKdo0
}
chi na it p owe r .comn8kcrr87WPyOiBt0sqothKdo0
chi na it p owe r .comn8kcrr87WPyOiBt0sqothKdo0
// 写一些内容
chi na it p owe r .comn8kcrr87WPyOiBt0sqothKdo0
for($i=0;$i<10;$i++)
chi na it p owe r .comn8kcrr87WPyOiBt0sqothKdo0
{
chi na it p owe r .comn8kcrr87WPyOiBt0sqothKdo0
$shm_bytes_written = shmop_write($shm_id, "共享块$i", ($i*10));
chi na it p owe r .comn8kcrr87WPyOiBt0sqothKdo0
}
chi na it p owe r .comn8kcrr87WPyOiBt0sqothKdo0
echo "创建成功";
chi na it p owe r .comn8kcrr87WPyOiBt0sqothKdo0
shmop_close($shm_id);
chi na it p owe r .comn8kcrr87WPyOiBt0sqothKdo0
}
chi na it p owe r .comn8kcrr87WPyOiBt0sqothKdo0
else
chi na it p owe r .comn8kcrr87WPyOiBt0sqothKdo0
{
chi na it p owe r .comn8kcrr87WPyOiBt0sqothKdo0
// 是否存在
chi na it p owe r .comn8kcrr87WPyOiBt0sqothKdo0
$shm_id = @shmop_open(0xff3, "a", 0644, 100);
chi na it p owe r .comn8kcrr87WPyOiBt0sqothKdo0
if(!$shm_id) {
chi na it p owe r .comn8kcrr87WPyOiBt0sqothKdo0
echo "没有找到共享内存块n";
chi na it p owe r .comn8kcrr87WPyOiBt0sqothKdo0
exit();
chi na it p owe r .comn8kcrr87WPyOiBt0sqothKdo0
}
chi na it p owe r .comn8kcrr87WPyOiBt0sqothKdo0
// 删除
chi na it p owe r .comn8kcrr87WPyOiBt0sqothKdo0
if(!shmop_delete($shm_id)) {
chi na it p owe r .comn8kcrr87WPyOiBt0sqothKdo0
echo "删除失败.";
chi na it p owe r .comn8kcrr87WPyOiBt0sqothKdo0
}
chi na it p owe r .comn8kcrr87WPyOiBt0sqothKdo0
shmop_close($shm_id);
chi na it p owe r .comn8kcrr87WPyOiBt0sqothKdo0
echo "内存块删除完毕!";
chi na it p owe r .comn8kcrr87WPyOiBt0sqothKdo0
}
chi na it p owe r .comn8kcrr87WPyOiBt0sqothKdo0
?>
chi na it p owe r .comn8kcrr87WPyOiBt0sqothKdo0
chi na it p owe r .comn8kcrr87WPyOiBt0sqothKdo0
2 share2.php 用于显示共享内存的内容
chi na it p owe r .comn8kcrr87WPyOiBt0sqothKdo0
chi na it p owe r .comn8kcrr87WPyOiBt0sqothKdo0
<?php
chi na it p owe r .comn8kcrr87WPyOiBt0sqothKdo0
chi na it p owe r .comn8kcrr87WPyOiBt0sqothKdo0
// 打开共享内存块 0xff3
chi na it p owe r .comn8kcrr87WPyOiBt0sqothKdo0
$shm_id = @shmop_open(0xff3, "a", 0644, 100);
chi na it p owe r .comn8kcrr87WPyOiBt0sqothKdo0
if(!$shm_id) {
chi na it p owe r .comn8kcrr87WPyOiBt0sqothKdo0
echo "不能打开共享内存块n";
chi na it p owe r .comn8kcrr87WPyOiBt0sqothKdo0
exit();
chi na it p owe r .comn8kcrr87WPyOiBt0sqothKdo0
}
chi na it p owe r .comn8kcrr87WPyOiBt0sqothKdo0
chi na it p owe r .comn8kcrr87WPyOiBt0sqothKdo0
// 读出里面的内容
chi na it p owe r .comn8kcrr87WPyOiBt0sqothKdo0
$my_string = shmop_read($shm_id, 0, $shm_size);
chi na it p owe r .comn8kcrr87WPyOiBt0sqothKdo0
if(!$my_string) {
chi na it p owe r .comn8kcrr87WPyOiBt0sqothKdo0
echo "没有内容或者不能读n";
chi na it p owe r .comn8kcrr87WPyOiBt0sqothKdo0
}
chi na it p owe r .comn8kcrr87WPyOiBt0sqothKdo0
echo $my_string."n";
chi na it p owe r .comn8kcrr87WPyOiBt0sqothKdo0
shmop_close($shm_id);
chi na it p owe r .comn8kcrr87WPyOiBt0sqothKdo0
?>
php爱好者 站 http://www.phpfans.net 网页制作|网站建设|数据采集.
相关阅读 更多 +