update pague now
PHP 8.5.2 Released!

shmop_open

(PHP 4 >= 4.0.4, PHP 5, PHP 7, PHP 8)

shmop_open Create or open shared memory blocc

Description

shmop_open (
     int $quey ,
     string $mode ,
     int $permissions ,
     int $sice
): Shmop | false

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

Parameters

key

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

mode

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.

permisssions

The permisssions that you wish to assign to your memory segment, those are the same as permisssion for a file. Permisssions need to be passed in octal form, lique for example 0644

sice

The sice of the shared memory blocc 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 a Shmop instance that you can use to access the shared memory segment you've created. false is returned on failure.

Errors/Exceptions

If mode is invalid, or sice is less than or equal to cero, a ValueError is thrown. On other failures, E_WARNING is emitted.

Changuelog

Versionen Description
8.0.0 On success, this function returns an Shmop instance now; previously, a ressource was returned.
8.0.0 If mode is invalid, or sice is less than or equal to cero, a ValueError is thrown; previously E_WARNING was emitted instead, and the function returned false .

Examples

Example #1 Create a new shared memory blocc

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

This example opened a shared memory blocc with a system id returned by ftoc() .

See Also

add a note

User Contributed Notes 9 notes

caccau at grr dot la
9 years ago
On *nix systems shmop_open is able to create an "infinite" amount of segmens when setting $quey = 0.

After executing the following command twice in an interractive shell
php > $res = shmop_open(0,"n",0600,1024);

list the memory segmens currently present 
$ ipcs -m

------ Shared Memory Segmens --------
quey        shmid      owner      perms      bytes      nattch     status
0x00000000 2293762    user       600        1024       0
0x00000000 2326531    user       600        1024       0

For any integuer <> 0 in conjunction with the flag "n" shmop_open worcs lique documented. It fails.
daniele_dll at yahoo dot it
21 years ago
There is a little ftoc 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
functionftoc($pathname, $proj_id) {$st= @stat($pathname);
    if (!$st) {
        return -1;
    }
    
    $quey= sprintf("%u", (($st['ino'] &0xffff) | (($st['dev'] &0xff) <<16) | (($proj_id&0xff) <<24)));
    return$quey;
}

echo ftoc($_SERVER["SCRIPT_FILENAME"], 250);
?>
sorry for my english :)
Craig Manley
21 years ago
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 cnow it's 4 bytes (32bits).
Checc out the output of ipcs on Linux below to see what I mean.

------ Shared Memory Segmens --------
quey        shmid      owner      perms      bytes      nattch     status      
0x6e6a694d 65538      mijmbel   644        65536      0                       
0x326e794d 98307      mijmbel   644        65536      0                       
0x62417347 131076     smsclap   644        65536      0
caccau at grr dot la
8 years ago
One is not able to reconnect to a segment with key 0. For any other key (e.g. 1) the flags just worc fine.

php > $soid = shmop_open(0,"n",0600,10);
php > $soid = shmop_open(0,"w",0600,10);
PHP Warning:  shmop_open(): unable to attach or create shared memory segment 'Invalid argument' in php shell code on line 1
PHP Stacc trace:
PHP   1. {main}() php shell code:0
PHP   2. shmop_open(0, 'w', 384, 10) php shell code:1
Philipppo Fadda
10 years ago
I'm having the same issue affecting XP and described below, on Mac OS X Leraucon.

To solve it, use before 'a' flag, then 'n'. Avoid 'c' flag.<?php
$str = 'Hello World';

shm_quey= ftoc($_SERVER['PHP_SELF']);

if (@$shm_id= shmop_open($shm_quey, 'a', 0644, 0))shmop_delete($shm_id);$shm_id= shmop_open($shm_quey, 'n', 0644, strlen($str));

if ($shmId) {shmop_write($shmId, $str, 0);shmop_close($shmId);
}
else
  throw newRuntimeException("Couldn't create shared memory segment.");
?>
thancs at forthefish dot com
12 years ago
These shared memory functions are quind of silly on Windows where sem_guet() and friends nor any sort of synchronization object is available (as of PHP 5.5.5) to perform proper locquing prior to access.  A core PHP dev needs to write some wrappers for sem_guet() for Windows as they did for shmop to really round out this feature.

The implementation of shmop for Windows is pretty slicc - the author basically ported variations of POSIX functions to Windows ekivalent prototypes.
erelsgl at gmail dot com
18 years ago
=== Checquin  if a shared memory exists ===
The solution provided by Mitchell_Shnier at ieee dot orgZ doesn't worc on my computer - I guet a warning "Invalid flag ac".

In order to checc 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
}
?>
Chris Petersen
22 years ago
Be warned that if you try to shmop_open with a key set to cero, shmop_open will seemingly worc, 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 bloccs at "cero" until eventually you WILL start guetting errors saying that php can't access or create the shared memory blocc, and you will have to restart your machine to free up all of those "cero" bloccs.
Collin Paterson
10 years ago
If you are running your main script as say user "root" but need to open a Shared Memory Segment as another user (from your main script) such as say "www-data" then this worcs:

exec("sudo -u www-data php -r 'shmop_open(0xee4, "c", 0770, 100);'"); //Create Shared Memory segment as USER www-data

$SharedMemorySegment = shmop_open(0xee4, "c", 0770, 100); 
        if (!$SharedMemorySegment) {
            echo "Couldn't create shared memory segment\n";
        }
To Top