Search This Blog

Wednesday, February 11, 2009

Simple php script to access shared memory by two php programs using ftok

PHP supports shared memory which can be used to store and retrieve data across processes. Shared memory is used for caching frequently used data in memory for php scripts on the same serve

FILE 1

$key = 'mykey'; // Key to store data with

$shm_key = ftok('afile','POINTER');

$data = shm_attach($shm_key);

$test = "vikas";
shm_put_var($data,$inmem,$test); // Save the data in shared memory

echo shm_get_var($data,$mykey);
shm_detach($data); // Disconnects from shared memory segment; data remains there

?>



FILE2

$key = 'mykey';
$shm_key = ftok('afile','POINTER');
$data = shm_attach($shm_key);
echo shm_get_var($data,$mykey);
shm_detach($data);

?>

No comments:

Post a Comment