问题
I am trying to create a shared memory segment in a C++ app. Then read it back on a web page.
The C++ app will be the server. And my test code works with the shm_client.c example on this page.
C++ code:
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 );
I changed the key in shm_client.c to 6565. And it works.
The problem is in my PHP file.
<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>
This outputs:
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() tells me that I have PHP Version 5.3.3 installed. The server is running centos 6.
来源:https://stackoverflow.com/questions/21997082/shared-memory-c-php