Hi

I have a query regarding shared memory created under Linux and then
accessing it by PHP script.

For my application, I have created a shared memory block in Debian Linux
using the shm_open(..), ftruncate(...) and mmap(...) function calls.

The shared memory block is of size 6304 bytes long. It is store 197 strings
of 32 characters long. I am confident that the shared memory block is
correctly created because when I go navigate to the /shm directory I
find the shared memory block called "/program.SCShared" present and it is of
size 6304 bytes. In addition to that, I have also written some strings to to
the shared memory, where each string starts at every 32 bytes. I have
verified that I can read these strings back successfully.

What I would like to do now is have some PHP script to read the strings out
of the shared memory so the strings can be displayed on a web page.

>From what I can gather from the PHP web site, it appears that I need to use
the shared memory functions as given in
http://au.php.net/manual/en/ref.shmop.php.

Specifically, since my strings start at 32-byte boundaries, the function:


string *shmop_read* ( int $shmid , int $start , int $count);

seems to be the ideal read routine to use. For example, if I want to read
the third string, I believe i write:


$shm_data = shmop_read($shmid, 64, 32);


Below is my code:

==============================

<?php

$shm_key = ftok("/dev/shm/program.SCShared", 't');

if ($shm_key < 0)
{
echo "ftok failed\n";
}

$shm_id = shmop_open ($shm_key,"w",0,0);

if (!empty($shm_id))
{
echo "\n Shared memory exists";
}
else
{
echo "\n Shared memory doesnt exists";
}

$shm_size = shmop_size ($shm_id);

echo "\n the size of shared memory is $shm_size";

$shm_data = shmop_read($shm_id, 0, 32);

$shm_data = unserialize($shm_data);

echo "\n read is $shm_data";

$i = strpos($shm_data, "\0");

if ($i === false)
{
echo "\n String is NULL";
}
else
{
$result = substr($shm_data, 3904, $i);

print_r($result);
}

shmop_close($shm_id);

echo "\nDetached from shared memory";

?>


==============================

Running this script shows, it appears that I'm connecting to my shared
memory "/dev/shm/program.SCShared" because "Shared memory exists" is
displayed.

However, I get the following which I would like some help with:

1) The size of the memory block is returning 10000 and not 6304 bytes as
expected. I believe 10000 is the default size if the memory block is created
from scratch using *shm_attach. Does the same apply to shmop_open. If so,
why is it being created?*

2) The call $shm_data = shmop_read($shm_id, 0, 32), which reads the first
string, returns no result. Does any know why this is the case when I know
that there is a string there because I have added it there using some C
code.

3) Is there another way I can access these strings without using the
shmop_<blah> routines? I have tried using shm_attach(...), shm_get_var(...),
but this seems to be too hard because it requires variable keys. Should I be
using MySQL?

Any help will be greatly appreciated, especially answering 2) as to why I
can't read the data.

Regards,
Richard.

Reply via email to