Delicious Bookmark this on Delicious Share on Facebook SlashdotSlashdot It! Digg! Digg



PHP : Function Reference : Shared Memory Functions : shmop_open

shmop_open

Create or open shared memory block (PHP 4 >= 4.0.4, PHP 5)
int shmop_open ( int key, string flags, int mode, int size )

Example 2233. Create a new shared memory block

<?php
$shm_key
= ftok(__FILE__, 't');
$shm_id = shmop_open($shm_key, "c", 0644, 100);
?>

Code Examples / Notes » shmop_open

craig manley

To: macmaster at pobox dot com:
To clear up some new confusion: you said the shm key is 8 bytes long. As far as I know it's 4 bytes (32bits).
Check out the output of ipcs on Linux below to see what I mean.
------ Shared Memory Segments --------
key        shmid      owner      perms      bytes      nattch     status      
0x6e6a694d 65538      mijnbel   644        65536      0                      
0x326e794d 98307      mijnbel   644        65536      0                      
0x62417347 131076     smsklap   644        65536      0


mitchell_shnier

To check whether a particular shared memory segment is already created, you need to concatenate the "a" and "c" flags. For example (where $SystemKey is the Unix key used by the other process(es) with which you want to share this memory segment)...
$shm_id = shmop_open($SystemKey, "ac", 0, 0);
if ($shm_id) {
  #it is already created
} else {
  #you need to create it with shmop_open using "c" only
}
Using only "a" does not work (just as using only IPC_EXCL in the Unix shmget() call is meaningless). Also, use the ipcs shell command to see your shared memory segments.


daniele_dll

There is a little ftok function. This function isn't included into php for windows so i've grabbed it directly from linux glibc 2.3.2 source code. I hope that this can be useful.
There is the code:
<?php
function ftok($pathname, $proj_id) {
$st = @stat($pathname);
if (!$st) {
return -1;
}

$key = sprintf("%u", (($st['ino'] & 0xffff) | (($st['dev'] & 0xff) << 16) | (($proj_id & 0xff) << 24)));
return $key;
}
echo ftok($_SERVER["SCRIPT_FILENAME"], 250);
?>
sorry for my english :)


macmaster

the key is a LONG variable type, meaning that the key can only be eight (8) bytes long, which can be too short if you're using any form of automagic key generation (like a parsed filename)

me

Just an alternative idea if 'shared memory' is what you need for your websites, you can use tmpfs (on Linux):
Get root to do this:
mkdir /home/myname/tmpfs
chown myname:mygroup /home/myname/tmpfs
..and this in a script executed at boot time:
mount -t tmpfs /mnt/tmpfs /home/myuser/tmpfs
Now you can use regular file functions (including locking) to access shared memory between all your processes.
More info: http://docsun.cites.uiuc.edu/sun_docs/C/
...and this because the note editor doesn't accept long lines...
solaris_9/SUNWaadm/SYSADV1/p150.html


chris petersen

Be warned that if you try to shmop_open with a key set to zero, shmop_open will seemingly work, and you can write to it, but you will not be able to read from it or delete it.  If you're not careful, you can continue doing this - creating more and more shared memory blocks at "zero" until eventually you WILL start getting errors saying that php can't access or create the shared memory block, and you will have to restart your machine to free up all of those "zero" blocks.

hackie

All of the problems have been addressed in the CVS, in addition the a mode now indeed DOES attach to the segment in readonly mode (i.e. SHM_RDONLY), so using shm_write on it would fail with a warning. It has 2 new flags w (read/write) and n (new segment IPC_CREAT|IPC_EXCL).
And a number of segfaults fixed :)


Change Language


Follow Navioo On Twitter
shmop_close
shmop_delete
shmop_open
shmop_read
shmop_size
shmop_write
eXTReMe Tracker