The use of shared memory in PHP

Source: Internet
Author: User
Use of shared memory in PHP
PHP has two sets of functions that use shared memory, one of which is the encapsulation of the system V IPC function, and the other is SHMOP. Neither of these requires the installation of external library files. The former can only be used under Linux, and to use it, when installing PHP to add the –ENABLE-SYSVSHM option, while the latter in Linux and Windows (Win2K system, Win98 does not support) are available, but in Windows, Only if PHP is the ISAPI runtime mode to work properly, you should add –enable-shmop when you install PHP.

The use of these two sets of functions is quite simple, the following gives a simple usage, more detailed information can refer to the PHP manual.

One, the shared memory usage of System V:
Create a block of memory
$key = 12345; Shared memory Key, note: type int
$memsize = 100; Size of shared memory, in units of byte
$perm = 0666; Shared memory access, refer to Linux permissions
$var _key = 345; Key for a variable that shares memory, note: type int
$shmid = Shm_attach ($key, $memsize, $perm); Create a shared memory
Shm_put_var ($shmid, $var _key, "abc"); Insert a shared memory variable, key is $var_key, and the value is "ABC"
Shm_detach ($shmid); Turn off shared memory
?>
Run the above PHP program can create a key 12345 of shared memory, the size of 100 bytes, there is a value of "ABC" variable. On the Linux command line-in IPCS you can see the shared memory information that was created:
--shared Memory segments--–
Key shmid owner perms bytes nattch Status
0x00003039 262144 Daemon 666 100 0
Note: 0x00003039 is 12345 of the 16 binary form.

Access the newly created shared memory and delete the PHP code for this shared memory:
Read memory block contents
$shmid = Shm_attach (12345); Access to the shared memory of key 12345
Echo Shm_get_var ($shmid, 345); Print the variable with key 345 in the shared memory, and this will show ABC
Shm_remove ($shmid); Delete this shared memory
?>
Run the above PHP code, will display the ABC, and the shared memory deleted, then run IPCS can see that the shared memory no longer exists.

Second, shmop of shared memory use:
Create a block of memory
$key = 12345; Shared-memory key
$memsize = 100; Size of shared memory, in units of byte
$perm = 0666; Shared memory access, refer to Linux permissions
$offset = 0; Shared memory offset address, 0 indicates the starting address of shared memory
$shmid = Shmop_open ($key, "C", $perm, $memsize); Creates a shared memory, and the second parameter, C, represents the creation
$shm _bytes_written = Shmop_write ($shm _id, "abc", 0); Write "ABC" to Shared memory
echo $shm _bytes_written; Print out the length of the data written to the shared memory, which will show 3
Shmop_close ($shm _id); Turn off shared memory
?>
Running this PHP will create a shared memory with a key of 12345 and a size of 100 bytes, which writes the string "ABC". We are writing a PHP that accesses this shared memory:
Read memory block
$shm _id = Shmop_open (12345, "W", 0, 0); Open the shared memory of key 12345, the second parameter w means open in read-write mode, open the existing shared memory, the third and fourth parameters must be 0
$shm _data = Shmop_read ($shm _id, 0, 3); Reads 3 bytes of data from the shared memory, the second parameter is the offset address, and 0 indicates 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.

Summarize:
1. These two sets of functions are easy to use, the only advantage of SHMOP is that it can be used under Windows, and it is recommended to use the Shm_* function under Linux, since that function is quite handy in inserting, updating, and reading variables in shared memory. Instead, SHMOP needs to plan its own storage structure for shared memory, which is slightly more complex to apply. In addition, the above example I directly use the number 12345 as the key to share memory, in fact, the more standard practice is to use the Ftok function to turn a path to the IPC key. You can refer to the PHP manual for specific procedures.
2. When using SHMOP, if the Shmop_delete () operation is not performed after use, the next write to the value of the shared memory variable will be problematic, if the length of the previous write is only overwritten with the previous, redundant content retention. Conversely, if the length of the previous write (the length of the next write), the extra content is automatically appended to the back.
3. The key and variables required for shared memory are of type int.

=================================
Description of the relevant methods:
Shm_attach
Open the build shared memory space.
Syntax: int shm_attach (int key, int [memsize], int [perm]);
return value: Integer
Function type: Operating system and environment
Description: This function is used to open or establish a shared memory space. The parameter key is the key for this section. The parameter memsize can be omitted, indicating the minimum required memory space (in bytes), the default value in the Sysvshm.init_mem configuration in Php3.ini or php.ini, or 10000 bytes if no configuration is configured. The parameter perm can also be omitted, which is the default value of 666 for the use of the memory space. The return value is the ID value of the shared memory that is available for the program to use.

Shm_detach
Aborts the shared memory space link.
Syntax: int shm_detach (int shm_identifier);
return value: Integer
Function type: Operating system and environment
Description: This function is used to abort the link to the shared memory space. The parameter shm_identifier is the shared memory ID value of the part that you want to stop.

Shm_remove
Clears the memory space.
Syntax: int shm_remove (int shm_identifier);
return value: Integer
Function type: Operating system and environment
Description: This function is used to clear all the data in the shared memory space. The parameter shm_identifier is the shared memory ID value of the part that you want to stop.

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
Description: This function can be used to increase or modify the value of variables in memory space. The parameter shm_identifier is the shared memory ID value that you want to increase the modification. The parameter variable_key is the variable name key to which you want to increase the modification. The parameter variable is the contents of a variable whose type can be a double-precision (double), an integer, a string (string), or an array.

Shm_get_var
Gets 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
Description: This function can be used to obtain the value of the variable specified 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 that you want to get. The return value is the value of the specified variable key.

Shm_remove_var
Deletes 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
Description: This function can be used to delete the value of the variable specified in the memory space. The parameter shm_identifier is the shared memory ID value that you want to remove. Parameter variable_key is the variable name key you want to delete
  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.