Shared Memory PHP

PHP offers an extension for using System V shared memory. It follows the same restrictions as the System V semaphore functions, above. That is, your operating system must support this functionality. Solaris, Linux, and AIX are known to work with shared memory.

Shared memory is virtual memory shared by separate processes. It helps solve the problem of communication between processes running on the same machine. An obvious method might be to write information to a file, but access to permanent storage is relatively slow. Shared memory allows the creation of system memory that may be accessed by multiple processes, which is much faster. Since exclusive use of this memory is essential, you must use some sort of locking. This is usually done with semaphores. If you use the shared memory functions, make sure you include support for System V semaphores as well.

The shared memory extension was added to PHP by Christian Cartus.

integer shm_attach(integer key, integer size, integer permissions)
The shm_attach function returns an identifier to shared memory. The key argument is an integer that specifies the shared memory. The shared memory will be created if necessary, in which case the optional size and permissions arguments will be used if present.

The size of the memory segment defaults to a value defined when PHP is compiled. Minimum and maximum values for the size are dependent on the operating system, but reasonable values to expect are a 1-byte minimum and a 128K maximum. There are also limits on the number of shared memory segments. Normal limits are 100 total segments and 6 segments per process.

The permissions for a memory segment default to 0x666, which is read and write permission to all users. This value operates like those used to set file permissions.

As with semaphores, calling shm_attach for the same key twice will return two different identifiers, yet they will both point to the same shared memory segment internally.

Keep in mind that shared memory does not expire automatically. You must free it using shm_remove.

<?
/*
** Shared Memory example
**
** This example builds on the semaphore example
** by using shared memory to communicate between
** multiple processes. This example creates shared
** memory but does not release it. Make sure you
** run the shm_remove example when you're done
** experimenting with this example.
*/
//Define integer for semaphore key
define("SEM_COREPHP", 1970);
//Define integer for shared memory key
define("SHM_COREPHP", 1970);
//Define integer for variable key
define("SHMVAR_MESSAGE", 1970);
//Get or create the semaphore
//This semaphore can only be acquired once
$sem = sem_get(SEM_COREPHP, 1);
//acquire semaphore
if(sem_acquire($sem))
{
//attach to shared memory
//make the memory 1K in size
$mem = shm_attach(SHM_COREPHP, 1024);
//attempt to get message variable, which
//won't be there the first time
if($old_message = shm_get_var($mem,
SHMVAR_MESSAGE);
{
print("Previous value: $old_message <BR>\n");
}
//create new message
$new_message = getmypid() . " here at " .
microtime();
//set new value
shm_put_var($mem, SHMVAR_MESSAGE,
$new_message);
//detach from shared memory
shm_detach($mem);
//release semaphore
sem_release($sem);
}
else
{
//we failed to acquire the semaphore
print("Failed to acquire semaphore!<BR>\n");
}
?>

boolean shm_detach(integer identifier)
Use shm_detach to free the memory associated with the identifier for a shared memory segment. This does not release the shared memory itself. Use shm_remove to do this.

value shm_get_var(integer identifier, integer key)
The shm_get_var function returns a value stored in a variable with shm_put_var.

boolean shm_put_var(integer identifier, integer key, value)
The shm_put_var function sets the value for a variable in a shared memory segment. If the variable does not exist, it will be created. The variable will last inside the shared memory until removed with shm_remove_var, or when the shared memory segment itself is destroyed with shm_remove. The value argument will be serialized with the same argument used for the serialize function. That means you may use any PHP value or variable—with one exception: at the time of this writing, objects lose their methods when serialized.

boolean shm_remove(integer identifier)
Use shm_remove to free a shared memory segment. All variables in the segment will be destroyed, so it is not strictly necessary to remove them. If you do not remove shared memory segments with this function, they may exist perpetually.

<?
/*
** Shared Memory example 2
**
** This example removes shared memory created
** by the previous shared memory example.
*/
//Define integer for semaphore key
define("SEM_COREPHP", 1970);
//Define integer for shared memory key
define("SHM_COREPHP", 1970);
//Define integer for variable key
define("SHMVAR_MESSAGE", 1970);
//Get or create the semaphore
//This semaphore can be acquired only once
$sem = sem_get(SEM_COREPHP, 1);
//acquire semaphore
if(sem_acquire($sem))
{
//attach to shared memory
//make the memory 1K in size
$mem = shm_attach(SHM_COREPHP, 1024);
//remove variable
shm_remove_var($mem, SHMVAR_MESSAGE);
//remove shared memory
shm_remove($mem);
//release semaphore
sem_release($sem);
}
else
{
//we failed to acquire the semaphore
print("Failed to acquire semaphore! <BR>\n");
}
?>

boolean shm_remove_var(integer identifier, integer key)
The shm_remove_var function frees the memory associated with a variable within a shared memory segment.

« Previous Topics
Semaphores
Ldap
Java
Next Topics »
Snmp
Wddx
Xml Expat Library


Face Book Twitter Google Plus Instagram Youtube Linkedin Myspace Pinterest Soundcloud Wikipedia

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

PHP Topics