php achieve _shm shared memory interprocess communication function of

Original Address: https://www.cnblogs.com/wt645631686/p/9146737.html

Introduced in front of a function shmop php shared memory, but also applied to the project, but there are limitations shmop that only support string type; sem After my test, is a hybrid, supports an array type, can be stored directly , direct access, less redundant format conversion step. But sem limited in size, shmop can set up a great big, big ~ ~ ~ but can also be re php.ini configuration changes.

Copy the code
? <PHP 
$ Key = 0x4337b124;   
$ shar_key = 1; 
// create a shared memory 
$ shm_id = shm_attach ($ Key, 1024, 0666); // Resource of the type 
IF ($ shm_id === false) { 
    Die ( 'Unable The Create Shared Memory segment to 'value is PHP_EOL);. 
} 
// set a value 
shm_put_var (shm_id $, $ shar_key,' Test '); 

// delete a Key 
shm_remove_var (shm_id $, $ shar_key); 

// get a value of 
$ = shm_get_var value (shm_id $, $ shar_key); 
var_dump ($ value); 

// detect whether there is a key 
var_dump (shm_has_var (shm_id $, $ shar_key)); 

// removed from the system 
shm_remove ($ shm_id); 

/ on / off and shared memory connected 
shm_detach ($ shm_id);
Copy the code

Note: $ shar_key can only be int type parameters.

Detailed description

shm_attach
Open establish a shared memory space.
Syntax: int shm_attach (int key, int [memsize], int [perm]);
Return Value: integer
function types: Operating System and Environment
Description: This function is used to open or establish a shared memory space. Parameters key to this part of the key. Memsize parameters can be omitted, showing the minimum required memory space (in bits byte group), a default value sysvshm.init_mem disposed in the php.ini php3.ini or, if no configuration was 10000 bytes. Perm parameter may be omitted, for the memory space usage rights, the default value is 666. Returns the ID value is the value of shared memory available to the program.

shm_detach
suspended shared memory space link.
Syntax: int shm_detach (int shm_identifier);
Returns: integer
function types: the operating system and environment
Description: This function is used to stop linking with the shared memory space. For shm_identifier parameter is the shared memory ID value portion is stopped.

shm_remove
clear memory space.
Syntax: int shm_remove (int shm_identifier);
Returns: integer
function types: the operating system and environment
Description: This function is used to erase all data from the shared memory space. For shm_identifier parameter is the shared memory ID value portion is stopped.

shm_put_var
added or updated memory space variables.
Syntax: int shm_put_var (int shm_identifier, int variable_key, mixed variable);
Return Value: integer
function types: Operating System and Environment
Description: This function is used to add or modify variable values in memory. To increase the parameter is modified shm_identifier shared memory ID value. To increase the parameter variable_key to modify the name of the key variables. Variable parameter is the contents of the variable, the type of a variable may be the number of times the precision (Double), integer (Integer), string (string) or an array (array).

shm_get_var
made variable memory space specified.
Syntax: mixed shm_get_var (int shm_identifier, int variable_key);
Return value: Data type mixing
function types: the operating system and environment
Description: This function can be used to obtain variable memory space specified value. Shm_identifier parameters for shared memory ID value to be achieved. Variable_key key parameter for the name of the variable to be achieved. The return value is the value of the specified variable key.

shm_remove_var
delete the memory space specified variable.
Syntax: int shm_remove_var (int id, int variable_key);
Return Value: integer
function types: Operating System and Environment
Description: This function is used to delete variable memory space specified value. Shm_identifier parameter is to be removed shared memory ID value. Variable_key parameter variable name for the key to be deleted.

Guess you like

Origin www.cnblogs.com/phpk/p/10930364.html