shmop_open

(PHP 4 >= 4.0.4, PHP 5)

shmop_open -- Create or open shared memory block

Description

int shmop_open ( int key, string flags, int mode, int size )

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

shmop_open() takes 4 parameters: key, which is the system's id for the shared memory block, this parameter can be passed as a decimal or hex. The second parameter are 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.

The third parameter is the mode, which are 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 ex. 0644. The last parameter is size of the shared memory block you wish to create in bytes.

注: Note: the 3rd and 4th should be entered as 0 if you are opening an existing memory segment. On success shmop_open() will return an id that you can use to access the shared memory segment you've created.

例子 1. 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().


add a note add a note User Contributed Notes
Craig Manley
06-Jan-2005 09:19
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
me at here dot com
08-Dec-2004 09:04
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
daniele_dll at yahoo dot it
02-Feb-2004 05:28
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 :)
Chris Petersen
29-Aug-2003 02:18
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 at prohost dot org
20-Jan-2002 09:04
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 :)
Mitchell_Shnier at ieee dot orgZ
08-Nov-2001 09:51
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)...<BR>
$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
}<BR>
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.
macmaster at pobox dot com
31-Mar-2001 04:15
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)