Descrierea

Parametri

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

Notă:

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

Valorile întoarse

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.

These shared memory functions are kind of silly on Windows where sem_get() and friends nor any sort of synchronization object is available (as of PHP 5.5.5) to perform proper locking prior to access. A core PHP dev needs to write some wrappers for sem_get() for Windows as they did for shmop to really round out this feature.

The implementation of shmop for Windows is pretty slick - the author basically ported variations of POSIX functions to Windows equivalent prototypes.

If you faced with any problem you're going to solve with shared memmory, but your server doesn't support it, you can use files instead. I've wrote simple wrapper for this and its suites for me. Hope it will be usefull for you too.

=== Checking if a shared memory exists ===The solution provided by Mitchell_Shnier at ieee dot orgZ doesn't work on my computer - I get a warning "Invalid flag ac".

In order to check if a shared-memory exists, you just have to open it with the "a" or "w" flag, while hiding the warnings using the "@" operator:<?php@$shid = shmop_open($systemId, "a", 0666, 0);if (!empty($shid)) { ... shared memory exists} else { ... shared memory doesn't exist}?>

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.

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.