PHP
downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

shmop_read> <shmop_delete
Last updated: Sat, 24 Mar 2007

view this page in

shmop_open

(PHP 4 >= 4.0.4, PHP 5)

shmop_open — Vytvořit nebo otevřít blok sdílené paměti

Popis

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

shmop_open() vytvoří nebo otevře blok sdílené paměti.

shmop_open() přijímá 4 argumenty: klíč, což je system id bloku sdílené paměti; tento argument může být předán jako desítkové nebo hexadecimální číslo. Druhý argument jsou parametry:

  • "a" pro přístup (nastavuje IPC_EXCL) tento parametr použijte, pokud chcete otevřít existující segment sdílené paměti
  • "c" pro vytvoření (nastavuje IPC_CREATE) tento parametr použijte, pokud chcete vytvořit nový segment sdílené paměti
Třetí argument je mód, což jsou přístupová práva, která chcete tomuto segmentu přiřadit; jsou stejná jako práva pro soubory. Přístupová práva musí být předána jako oktalové číslo, např. 0644. Poslední argument je velikost bloku sdílené paměti, který chcete vytvořit, v bytech.

Poznámka: Pozn.: Pokud otvíráte existující segment paměti, 3. 4. argument by měly být předány jako 0. Při úspěchu shmop_open() vrací id, které můžete použít k přístupu na tento segment sdílené paměti.

Příklad 1919. Vytvoření bloku sdílené paměti

<?php
$shm_id
= shmop_open(0x0fff, "c", 0644, 100);
?>

Tato ukázka otevřela blok sdílené paměti se system id 0x0fff.



shmop_read> <shmop_delete
Last updated: Sat, 24 Mar 2007
 
add a note add a note User Contributed Notes
shmop_open
Craig Manley
05-Jan-2005 05: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
07-Dec-2004 05: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
01-Feb-2004 01: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
28-Aug-2003 11: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 05: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
07-Nov-2001 05: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
30-Mar-2001 12: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)

shmop_read> <shmop_delete
Last updated: Sat, 24 Mar 2007
 
 
show source | credits | stats | sitemap | contact | advertising | mirror sites