PHP and Shared Memory

July 17, 2004

So what good are the shared memory functions? well you can use them to share data between different PHP scripts or even with other processes running in your server. Very usefull if you know how to make use it. The trouble is I don't. Shared memory functions are an area that I never looked at before.

After yesterday's fiasco with PHP 5 and Java 5 I decided to investigate turck MMCache a bit further and found references to Shared Memory and semaphores, that set me wondering If I might use it in mega upload to improve it's performance.

In any event I had to recompile my php installation to try to accomodate java 5 with php 4 and in the process I decided to add --enable-sysvshm and --enable-sysvsem to the configure script. Some of the user contributed comments in the PHP manual were really usefull in getting started. Just a short while ago I managed to write hello world using shared memory. Here is what it looks like

shm1.php - saves a variable in shared memory.

$sem_id = sem_get(64); if($sem_id) { if(sem_acquire($sem_id)) { $shm_id = shm_attach(1234); $hello = "Hello, World"; echo "have an id $shm_id"; shm_put_var($shm_id,1234,$hello); } sem_release($sem_id); }

shm2.php - retrieves a variable in shared memory.

$sem_id = sem_get(64); if($sem_id) { if(sem_acquire($sem_id)) { $shm_id = shm_attach(1234); echo "have an id $shm_id"; $hello = shm_get_var($shm_id,1234); echo "$hello"; } sem_release($sem_id); }

Obviously not the most defensive piece of code I have written but it works.

Posted by raditha at July 17, 2004 10:58 AM
Your Ad Here

 

Jabber  |  Linux  |  mySQL  |  PHP  |  Java  |  Site Map  |  Wiki

Downloads  |  About  |  Links  |  Contact  |  Home

 

Copyright © Raditha Dissanayake 2003 - 2007

Terms of Use  |  Privacy

 

 

July 2004
Sun Mon Tue Wed Thu Fri Sat
        1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31