启用php的共享内存:
1).windows下,php.ini中取消 ;extension=php_shmop.dll 这行的行首分号,加载shmop扩展。
2) .linux/unix下,重新编译php,加入--enable--shmop 选项。
php.net中函数解释如下: shmop_close -- Close shared memory block shmop_delete -- Delete shared memory block shmop_open -- Create or open shared memory block shmop_read -- Read data from shared memory block shmop_size -- Get size of shared memory block shmop_write -- Write data into shared memory block
测试代码如下:
<?php
/*
* @文件: create.php
* @功能:将全局变量写入共享内存中
*/
//定义全局变量
$super = "hello world";
//申请100字节共享内存空间
$shm_id = shmop_open(0xff3, "c", 0644, 100);
if (!$shm_id)
{
echo "申请空间失败<br>";
}
//内容写入共享内存空间
if (shmop_write($shm_id, $super, 0))
{
echo "全局变量已经写入共享内存<br>";
}
else
{
echo "写入共享内存失败<br>";
}
//关闭共享内存空间
shmop_close($shm_id);
?>
<?php
/*
* @文件: read.php
* @功能:读取共享内存中的内容
*/
//读100字节共享内存空间
$shm_id = shmop_open(0xff3, "a", 0644, 100);
//获取共享内存空间中的前11个字节的内容
//create.php中 $super 变量长度为11
$share = shmop_read($shm_id, 0, 11);
echo $share;
//关闭
shmop_close($shm_id);
?>