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.<?
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.
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.
PHP Related Interview Questions
|PHP Interview Questions||MySQL Interview Questions|
|PHP+MySQL Interview Questions||Drupal Interview Questions|
|MYSQL DBA Interview Questions||PHP5 Interview Questions|
|WordPress Interview Questions||Joomla Interview Questions|
|CakePHP Interview Questions||CodeIgniter Interview Questions|
|PHP7 Interview Questions|
An Introduction To Php
Variables, Operators, And Expressions
Classes And Objects
I/o And Disk Access
Time, Date, And Configuration Functions
Parsing And String Evaluation
Sorting Searching And Random Numbers
Integration With Html
Efficiency And Debugging
All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd
Wisdomjobs.com is one of the best job search sites in India.