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 )

shmop_open() can create or open a shared memory block.

Parameters

key

System's id for the shared memory block. Can be passed as a decimal or hex.

flags

The flags that you can use:

  • "a" for access (sets SHM_RDONLY for shmat) use this flag when you need to open an existing shared memory segment for read only
  • "c" for create (sets IPC_CREATE) use this flag when you need to create a new shared memory segment or if a segment with the same key exists, try to open it for read and write
  • "w" for read & write access use this flag when you need to read and write to a shared memory segment, use this flag in most cases.
  • "n" create a new memory segment (sets IPC_CREATE|IPC_EXCL) use this flag when you want to create a new shared memory segment but if one already exists with the same flag, fail. This is useful for security purposes, using this you can prevent race condition exploits.
mode

The permissions that you wish to assign to your memory segment, those are the same as permission for a file. Permissions need to be passed in octal form, like for example 0644

size

The size of the shared memory block you wish to create in bytes

Note:

Note: the 3rd and 4th should be entered as 0 if you are opening an existing memory segment.

Return Values

On success shmop_open() will return an id that you can use to access the shared memory segment you've created. FALSE is returned on failure.

Examples

Example 2233. Create a new shared memory block

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


This example opened a shared memory block with a system id returned by ftok().

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