PHP SHARED MEMORY - PASSING DATA BETWEEN PHP SCRIPTS

Php Shared Memory on Linux

In order to demonstrate passing data between two php scripts I have created two scripts below called shmopReadArgs.php and shmopReadContinuous.php. Also I have a user on my Linux system called fred.

shmopReadArgs.php

<?php
$Message=$argv; //this needs to be <100 bytes for this example!!!
// Create 100 byte shared memory block with system id of 0xff3
$shm_id = shmop_open(0xff4, "c", 0644, 100);
if (!$shm_id) {
echo "Couldn't create shared memory segment\n";
}
// Get shared memory block's size
$shm_size = shmop_size($shm_id);
echo "SHM Block Size: " . $shm_size . " has been created.\n";
// Lets write a test string into shared memory
$shm_bytes_written = shmop_write($shm_id, $Message[1], 0);
if ($shm_bytes_written != strlen($Message[1])) {
echo "Couldn't write the entire length of data\n";
}
// Now lets read the string back
$my_string = shmop_read($shm_id, 0, $shm_size);
if (!$my_string) {
echo "Couldn't read from shared memory block\n";
}
echo "The data inside shared memory was: " . $my_string . "\n";
//Now lets delete the block and close the shared memory segment
shmop_close($shm_id);
?>

shmopReadContinuous.php

<?php
// Create 100 byte shared memory block with system id of 0xff3
$shm_id = shmop_open(0xff4, "a", 0644, 100);
if (!$shm_id) {
echo "Couldn't create shared memory segment\n";
}
// Get shared memory block's size
$shm_size = shmop_size($shm_id);
echo "SHM Block Size: " . $shm_size . "\n";
// Now lets read the string back
while(1)
{
$my_string = shmop_read($shm_id, 0, $shm_size);
if (!$my_string) {
echo "Couldn't read from shared memory block\n";
}
echo "The data inside shared memory was: " . $my_string . "\n";
sleep(1);
}
shmop_close($shm_id);
?>

Open two terminals.

In the First Terminal, run the following:

sudo –u fred php shmopReadArgs.php “Message from Fred ”

In the Second Terminal, run the following infinitely looping script:

sudo –u fred php shmopReadContinuous.php

You will observe the following output:

The data inside shared memory was: Message from Fred
The data inside shared memory was: Message from Fred
The data inside shared memory was: Message from Fred
The data inside shared memory was: Message from Fred
.
.
.

After about 10 seconds, try running the following in Terminal One:

sudo –u fred php shmopReadArgs.php “This Shared Memory stuff is quite useful”

You will observe the following output:

The data inside shared memory was: Message from Fred
The data inside shared memory was: Message from Fred
The data inside shared memory was: Message from Fred
The data inside shared memory was: Message from Fred
.
.
.
The data inside shared memory was: This Shared Memory stuff is quite useful
The data inside shared memory was: This Shared Memory stuff is quite useful
The data inside shared memory was: This Shared Memory stuff is quite useful
.
.
.

NOTE:You will have to press CTRL-C to kill this continuous loop in Terminal Two.

So we are successfully sending data (text messages) from one php script (shmopReadArgs.php) to another php script (shmopReadContinuous.php)



Also, you can observe the memory being used here simply via the Linux ipcs command:

PHP-Shared-Memory

Note that this shared memory segment can be deleted via the Linux terminal command:

sudo ipcrm –m 425987





Linux Examples - Comments