Delicious Bookmark this on Delicious Share on Facebook SlashdotSlashdot It! Digg! Digg



PHP : Function Reference : Semaphore, Shared Memory and IPC Functions : shm_attach

shm_attach

Creates or open a shared memory segment (PHP 4, PHP 5)
int shm_attach ( int key [, int memsize [, int perm]] )


Code Examples / Notes » shm_attach

webmaster

With Sun Solaris 2.x, the MAXIMUM shared memory value allowed is 1,048,576.  The maximum allowed value can be determined using the command /usr/sbin/sysdef.  On Linux, there does not seem to be any system enforced maximum size.  To change the maximum allowed size on Solaris 2.x, use set shmsys:shminfo_shmmax=[new value].

php

The memsize needed for shared memory (tested on linux system) can be calculated with:
For each varialbe you want to store: 24 Bytes
+ serialized var-size (see below) alligned by 4 Bytes
+ 16 Bytes
For a update of a var with the same key, the memory for the old var will be needed extra.


katzenmeier

The limit for one SHM block seems to be 32 MB, but you can split your data in several SHM blocks if you need to. The total SHM limit seems to be about 8 GB.
I'm not sure whether this is true for all configurations.


uther pendragon

Since there aren't seperate functions for CREATING and ATTACHING shared memory (a mistake in my opinion), you might want to do some testing to check whether you've created it or not, as you may not want the slave of a master/slave pair to ever create the shared memory.
One way you can test this is by having your slave set the size to something small, then testing the size by putting a variable too large to fit, e.g.:
function get_shmem() {
    return shm_attach(ftok('somefile.txt', 'T'), 100, 0644);
}
$shm = get_shmem();
while (!@shm_put_var($shm, 1, str_repeat('.....', 20))) {
    shm_remove($shm);
    sleep(1);
    //we created it, so we'll remove it and sleep to wait for the master to create it, then try again.
    $shm = get_shmem();
}
shm_remove_var($shm, 1);
//here we know the shared memory was already created, because we could put a variable in bigger than the size requested.
Another way you can handle it is to have all programs initialize the shared memory with the same parameters... I had a problem with this when my clients starting too fast and created the shmem without passing a memsize value, so it was defaulting to 10k which was too small.


eric

Objects are stored serialized in shm_put_var, so to find a value for memsize, you can use strlen(serialize($object_to_store_in_shm)).

h dot raaf

Notice that 'int key' for shared-memory is shared with the keys used for semaphores. So you get in trouble when you use the same key value for semaphores and shared memory!

muytoloco

If one process make a shm_attach to one inexistent memory area, this area will be created under the same priviliegies of the script running user. If another process will try to create or acces the same area, runnig by other user with different privileges of the first user, an error will occur.

jpeter1978

I tried all the suggestions above for getting the object size (in bytes) for $memsize, but they didn't work universally for the two types of objects I tried (string and array of strings).
After doing some googling and experimenting, I've found the following magic formula:
$memsize = ( strlen( serialize( $object ) ) + 44 ) * 2;
I found this in someone else's code, so I can't explain it.


cecil

Here is an example of how to use one shared memory block to store multiple variables or arrays.. unfortunetly in order to store more than ONE variable, you have to use sem_get() multiple times.. same goes for shm_attach(), shm_put_var() and shm_get_var()
#!/usr/local/bin/php -q
<?PHP
// test.php
$SHM_KEY = ftok(__FILE__,'A');
$shmid  = sem_get($SHM_KEY, 1024, 0644 | IPC_CREAT);
$shmid2 = sem_get($SHM_KEY, 1024, 0644 | IPC_CREAT);
$shmid3 = sem_get($SHM_KEY, 1024, 0644 | IPC_CREAT);
$data =  shm_attach($shmid, 1024);
$data2 = shm_attach($shmid2, 1024);
$data3 = shm_attach($shmid3, 1024);
$test = array("hello","world","1","2","3");
$test2 = array("hello","world","4","5","6");
$test3 = array("hello","world","7","8","9");
shm_put_var($data,$inmem,$test);
shm_put_var($data2,$inmem2,$test2);
shm_put_var($data3,$inmem3,$test3);
print_r(shm_get_var($data,$inmem));
print_r(shm_get_var($data2,$inmem2));
print_r(shm_get_var($data3,$inmem3));
shm_detach($data);
shm_detach($data2);
shm_detach($data2);
?>
to REALLY test it.. create a second script like so and run it..
#!/usr/local/bin/php -q
<?PHP
// test2.php
$SHM_KEY = ftok(__FILE__,'A');
$shmid  = sem_get($SHM_KEY, 1024, 0644 | IPC_CREAT);
$shmid2 = sem_get($SHM_KEY, 1024, 0644 | IPC_CREAT);
$shmid3 = sem_get($SHM_KEY, 1024, 0644 | IPC_CREAT);
$data =  shm_attach($shmid, 1024);
$data2 = shm_attach($shmid2, 1024);
$data3 = shm_attach($shmid3, 1024);
print_r(shm_get_var($data,$inmem));
print_r(shm_get_var($data2,$inmem2));
print_r(shm_get_var($data3,$inmem3));
shm_detach($data);
shm_detach($data2);
shm_detach($data2);
?>
As you can see, test2.php doesn't insert anything into shared memory.. yet it pulls out 3 totally different arrays already stored..
Hope that helps.. took me a bit to get it right.. everyone seems to have their own idea of how shm should be used. lol.
BTW, not sure how the ftok works to be honest, cause I didn't change the __FILE__ to match the file path of test.php or anything.. I would think that the file path out have to be the exact same to work correctly.. oh well, it worked as-is! haha..
- Cecil


lew

Finding out shared memory kernel settings in FreeBSD:
sys1# sysctl -a | grep -i SHM
kern.ipc.shmmax: 4194304
kern.ipc.shmmin: 1
kern.ipc.shmmni: 96
kern.ipc.shmseg: 64
kern.ipc.shmall: 1024
kern.ipc.shm_use_phys: 0
Shows us that we can allocate a total of 4GB (wow) of shared memory, etc...


rch

Cecil, the key of a var is an integer (not the name ). You can put multiples vars in the same share.
#!/usr/local/bin/php -q
<?PHP
$SHM_KEY = ftok(__FILE__, chr( 4 ) );
$data =  shm_attach($SHM_KEY, 1024, 0666);
$test1 = array("hello","world","1","2","3");
$test2 = array("hello","world","4","5","6");
$test3 = array("hello","world","7","8","9");
shm_put_var($data, 1, $test1);
shm_put_var($data, 2,$test2);
shm_put_var($data, 3,$test3);
print_r(shm_get_var($data, 1));
print_r(shm_get_var($data, 2));
print_r(shm_get_var($data, 3));
shm_detach($data);
?>


andreykeinspam

As far as I see from the sources of ext/sysvshm, it's not needed to  do  arithmetic (bit) OR (|) of "perm" with  IPC_CREAT (and IPC_EXCL). The extension will do it for you. It tries to attach to the memory segment and if the try did not succeed it tries to attach but with flags set to user_flag | IPC_CREAT | IPC_EXCL.
The exact code (shm_flag is the third param to the function) :
if ((shm_id = shmget(shm_key, 0, 0)) < 0) {
     if (shm_size < sizeof(sysvshm_chunk_head)) {
           php_error_docref(NULL TSRMLS_CC, E_WARNING, "failed for key 0x%x: memorysize too small", shm_key);
           efree(shm_list_ptr);
           RETURN_FALSE;
     }
     if ((shm_id = shmget(shm_key, shm_size, shm_flag | IPC_CREAT | IPC_EXCL)) < 0) {
           php_error_docref(NULL TSRMLS_CC, E_WARNING, "failed for key 0x%x: %s", shm_key, strerror(errno));
            efree(shm_list_ptr);
            RETURN_FALSE;
    }
}


uther pendragon

As a follow-up to my last post regarding shm_attach and its limit capability for knowing how it was created....
for more control, use the shmop_* series of functions, as they have finer grained control than these.
and by the way:  the SHMOP functions SHOULD BE listed under "see also" for all the SHM* wrapper functions (I assume they are wrappers to the SHMOP* functions).


koester

4194304 means 4 MB and NOT 4 GB for shared memory on FreeBSD. You can increase the shared memory at runtime if you like.

Change Language


Follow Navioo On Twitter
ftok
msg_get_queue
msg_receive
msg_remove_queue
msg_send
msg_set_queue
msg_stat_queue
sem_acquire
sem_get
sem_release
sem_remove
shm_attach
shm_detach
shm_get_var
shm_put_var
shm_remove_var
shm_remove
eXTReMe Tracker