Home>Article>Backend Development> Use of PHP shared memory
php has two sets of functions that use shared memory, one is a package of System V IPC functions, and the other is shmop. Neither requires installing external libraries. The former can only be used under Linux, and if you want to use it, you must add the –enable-sysvshm option when installing PHP; while the latter can be used on both Linux and Windows (systems after win2k, win98 does not support it), but in In windows, php can only work properly if it is in ISAPI running mode. When installing php, you need to add –enable-shmop.
The use of these two sets of functions is quite simple. Simple usage is given below. For more detailed information, please refer to the PHP manual.
1. System V shared memory usage:
// Create memory block
$key = 12345; // Shared memory key, note: type is int
$memsize = 100; // Shared memory Size, unit byte
$perm = 0666; // Shared memory access permissions, refer to Linux permissions
$var_key = 345; // Key of a variable in shared memory, note: type is int
$shmid = shm_attach( $ key, $memsize, $perm ); // Create a shared memory
shm_put_var( $shmid, $var_key, "abc" ); // Insert a shared memory variable with the key $var_key and the value "abc"
shm_detach( $shmid ); // Turn off shared memory
?>
Running the above php program can create a shared memory with a key of 12345, a size of 100 bytes, and a variable with a value of "abc". Type ipcs on the linux command line to see the created shared memory information:
—— Shared Memory Segments ——–
key 144 daemon 666 100 0
Note: 0×00003039 is 12345 in hexadecimal form.
Access the newly created shared memory and delete the PHP code of this shared memory:
// Read the memory block content
$shmid = shm_attach( 12345 ); // Access the shared memory with key 12345
echo shm_get_var( $shmid, 345 ); // Print out the variable with key 345 in the shared memory, abc will be displayed here
shm_remove( $shmid ); // Delete the shared memory
?>
Run the above php code, ABC will be displayed and the shared memory will be deleted. At this time, if you run ipcs again, you can see that the shared memory no longer exists.
2. Shared memory usage of shmop:
// Create memory block
$key = 12345; // Shared memory key
$memsize = 100; // Size of shared memory, unit byte
$ perm = 0666; // Shared memory access permissions, refer to Linux permissions
$offset = 0; // Shared memory offset address, 0 represents the starting address of shared memory
$shmid = shmop_open($key, "c", $perm, $memsize); // Create a shared memory, the second parameter c means creation
$shm_bytes_written = shmop_write($shm_id, "abc", 0); // Write "abc" into the shared memory
echo $ shm_bytes_written; // Print out the length of data written to the shared memory, 3 will be displayed here
shmop_close($shm_id); // Close the shared memory
?>
Running this php will create a key with a key of 12345 and a size of 100 bytes The shared memory has the string "abc" written in it. We are writing a php that accesses this shared memory:
// Read the memory block
$shm_id = shmop_open(12345, "w", 0, 0); // Open the shared memory with key 12345, The second parameter w means to open in read-write mode and open the existing shared memory. The third and fourth parameters must be 0
$shm_data = shmop_read($shm_id, 0, 3); // From the shared memory Read 3 bytes of data, the second parameter is the offset address, 0 represents the starting address of the shared memory
echo $shm_data; // Print out the shared memory data returned by the previous function
shmop_delete($shm_id); / / Delete shared memory
?>
Running this php will print out abc and delete the original shared memory.
Summary:
1. Both sets of functions are simple and easy to use. The only advantage of shmop is that it can be used under windows. If under Linux, it is recommended to use the shm_* set of functions, because that set of functions is used in inserting, updating and reading shared memory. The variables are quite convenient, but shmop needs to plan the storage structure of the shared memory by itself, which is a little more complicated to apply. In addition, in the above example, I directly used the number 12345 as the key of the shared memory. In fact, a more standard approach is to use the ftok function to convert a path into an ipc key. For specific methods, please refer to the PHP manual.
2. When using shmop, if the shmop_delete() operation is not performed after the use is completed, there will be problems when writing the shared memory variable value next time. If the length of the previous write is greater than the length of the next write, Then only the previous ones will be overwritten, and the redundant content will be retained. On the contrary, if the length of the previous write is less than the length of the next write, the excess content will be automatically appended to the end.
3. The keys and variables required when sharing memory are all int types.
========================================
Introduction to related methods:
shm_attach
Open the shared memory space.
Syntax: int shm_attach(int key, int [memsize], int [perm]);
Return value: integer
Function type: operating system and environment
Content description: This function is used to open or create a shared memory space. The parameter key is the key of this part. The parameter memsize can be omitted and represents the minimum required memory space (unit is byte group). The default value is configured in php3.ini or sysvshm.init_mem in php.ini. If there is no configuration, it is 10000 bytes. The parameter perm can also be omitted to determine the usage rights of this memory space. The default value is 666. The return value is the ID value of the shared memory, which can be used by the program.
shm_detach
Abort the shared memory space link.
Syntax: int shm_detach(int shm_identifier);
Return value: integer
Function type: operating system and environment
Content description: This function is used to terminate the link with the shared memory space. The parameter shm_identifier is the shared memory ID value of the part to be stopped.
shm_remove
Clear memory space.
Syntax: int shm_remove(int shm_identifier);
Return value: integer
Function type: operating system and environment
Content description: This function is used to clear all data in the shared memory space. The parameter shm_identifier is the shared memory ID value of the part to be stopped.
shm_put_var
Add or update variables in the memory space.
Syntax: int shm_put_var(int shm_identifier, int variable_key, mixed variable);
Return value: integer
Function type: operating system and environment
Content description: This function can be used to add or modify variable values in the memory space. The parameter shm_identifier is the shared memory ID value to be added and modified. The parameter variable_key is the variable name key to be added and modified. The parameter variable is the content of the variable. The type of the variable can be a double, an integer, a string, or an array.
shm_get_var
Get the variable specified in the memory space.
Syntax: mixed shm_get_var(int shm_identifier, int variable_key);
Return value: mixed type data
Function type: operating system and environment
Content description: This function can be used to obtain the specified variable value in the memory space. The parameter shm_identifier is the shared memory ID value to be obtained. The parameter variable_key is the variable name key to be obtained. The return value is the value of the specified variable key.
shm_remove_var
Delete the specified variable in the memory space.
Syntax: int shm_remove_var(int id, int variable_key);
Return value: integer
Function type: operating system and environment
Content description: This function can be used to delete the specified variable value in the memory space. The parameter shm_identifier is the shared memory ID value to be removed. The parameter variable_key is the variable name key to be deleted