我正在尝试在C ++应用程序中创建共享内存段。然后在网页上阅读。
C ++应用程序将是服务器。我的测试代码适用于this页面上的shm_client.c示例。
C ++代码:
key = 6565;
if( (shmid = shmget( key, 27, IPC_CREAT | 0666 )) < 0 )
throw std::string( "shmget failed." );
if( (data = shmat( shmid, NULL, 0 )) < 0 )
throw std::string( "shmat failed" );
memset( data, 0, 27 );
strncpy( (char *)data, "Hello world", 27 );
我将shm_client.c中的密钥更改为6565.它有效。
问题出在我的PHP文件中。
<html>
<body>
<p>Hej</p>
<?php
ini_set('display_errors',1);
ini_set('display_startup_errors',1);
error_reporting(-1);
$shm_id = shmop_open( 6565, "a", 0, 0 );
echo "Id: " . $shm_id . "<br />";
echo "Size: " . shmop_size( $shm_id ) . "<br />";
$data = shmop_read( $shm_id, 0, 27 );
echo "Data: " . $data . "<br />";
?>
</body>
</html>
输出:
Hej
Warning: shmop_open(): unable to attach or create shared memory segment in /var/www/html/secure/ctrl.php on line 8 Id:
Warning: shmop_size(): no shared memory segment with an id of [0] in /var/www/html/secure/ctrl.php on line 10 Size:
Warning: shmop_read(): no shared memory segment with an id of [0] in /var/www/html/secure/ctrl.php on line 12 Data:
phpinfo()告诉我安装了PHP Version 5.3.3。 服务器正在运行centos 6。