Re: [PHP] Shared memory - linx and PHP newbie

2009-05-22 Thread Richard W
Thanks Daniel for your suggestions.

What I have found are:
1) I'm assuming the key is good. A value of 1947143245 is returned.

2) I have set the permission of the shared memory (program.SCShared) to
777 octal (full read/write/execute access). The group and owner of the file
is my login ('eclipse'). According to the PHP manual for shmop_open(..), I
do not need to set the 'mode' parameter of this function (eg, to 0777)
because I'm trying to connect to an existing shared memory block. Despite
this, I've tried  setting mode=0777, but errors were generated when I ran
the code. I event tried changing the user and group to 'www-data' with no
luck.

3) I added the debug_backtrace and the display result was array(0) {}
inserted where debug_backtrace() was added as shown, but I'm not sure what
the result really means:

CODE

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

var_dump(debug_backtrace());

if ($shm_key  0)
{
echo \nftok failed. Error message is $php_errormsg br;
}

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

if ($shm_id == FALSE)
{
echo \n Shared memory doesnt exists br;
}
else
{
echo \n Shared memory exists br;
}

$shm_size = shmop_size ($shm_id);

var_dump(debug_backtrace());

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

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

if ($shm_data == FALSE)
{
echo \nCould not read data. : $php_errormsg br;
}
else
{
echo \nRead successful br;
}

echo \n read1 is $shm_data br;

$shm_data = unserialize($shm_data);

echo \n read2 is $shm_data br;

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

if ($i === FALSE)
{
echo \n String is NULL br;
}
else
{
$result = substr($shm_data, 0, $i);

print_r($result);

echo br;
}

shmop_close($shm_id);

echo \nDetached from shared memory;

?

=

===RESULT OF CODE
array(0) { } Shared memory exists
array(0) { } the size of shared memory is 1
Read successful
read1 is PHP_SM�'
read2 is
String is NULL
Detached from shared memory
=

4) The data I expecting to read is 9876.54321 because this is the first
string in memory. A 'cat' of the shared memory is shown:


r...@ts7800:shm# cat program.SCShared
9876.5432101.230034.50678.9010002345.678900

5)  Is there anything else that  can try?

Regards,
Richard.


On Thu, May 21, 2009 at 11:17 PM, Daniel Brown danbr...@php.net wrote:

 On Thu, May 21, 2009 at 10:15, Richard W rw180...@gmail.com wrote:
 
  Any help will be greatly appreciated, especially answering 2) as to why I
  can't read the data.

Are you certain that the problem lies within the shmop reading?
 Check to see if the file is actually being accessed properly, the key
 is good from your ftok(), etc.  You may also want to make sure that
 things as basic as permissions and 'who created' vs. 'who can read'
 (since you're trying to run it from the web server) match up
 appropriately.

With just a cursory glance, the shmop_read() piece itself looks
 fine, which suggests to me that there may be other problems.  See if
 the logs spit anything out, or try to debug the things with an `or
 die(Here's the error.\n);` tacked onto the end of the suspected
 lines.  If it's crashing out, consider a cachegrind or
 debug_backtrace() run.

As for the memory being read, I'd agree that it does seem that it
 is, since shm_open() is returning something other than FALSE, but that
 doesn't mean that it's `=== TRUE` either.  It may instead be returning
 a message or another unexpected result that, in empty(), may evaluate
 to TRUE and allow it to echo out the message in your test condition.

 --
 /Daniel P. Brown
 daniel.br...@parasane.net || danbr...@php.net
 http://www.parasane.net/ || http://www.pilotpig.net/
 50% Off All Shared Hosting Plans at PilotPig: Use Coupon DOW1



[PHP] Shared memory - linx and PHP newbie

2009-05-21 Thread Richard W
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 1 and not 6304 bytes as
expected. I believe 1 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.


Re: [PHP] Shared memory - linx and PHP newbie

2009-05-21 Thread Daniel Brown
On Thu, May 21, 2009 at 10:15, Richard W rw180...@gmail.com wrote:

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

Are you certain that the problem lies within the shmop reading?
Check to see if the file is actually being accessed properly, the key
is good from your ftok(), etc.  You may also want to make sure that
things as basic as permissions and 'who created' vs. 'who can read'
(since you're trying to run it from the web server) match up
appropriately.

With just a cursory glance, the shmop_read() piece itself looks
fine, which suggests to me that there may be other problems.  See if
the logs spit anything out, or try to debug the things with an `or
die(Here's the error.\n);` tacked onto the end of the suspected
lines.  If it's crashing out, consider a cachegrind or
debug_backtrace() run.

As for the memory being read, I'd agree that it does seem that it
is, since shm_open() is returning something other than FALSE, but that
doesn't mean that it's `=== TRUE` either.  It may instead be returning
a message or another unexpected result that, in empty(), may evaluate
to TRUE and allow it to echo out the message in your test condition.

-- 
/Daniel P. Brown
daniel.br...@parasane.net || danbr...@php.net
http://www.parasane.net/ || http://www.pilotpig.net/
50% Off All Shared Hosting Plans at PilotPig: Use Coupon DOW1

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php