Home Backend Development PHP Tutorial Use of PHP shared memory

Use of PHP shared memory

Dec 01, 2016 am 10:01 AM
Linux php

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

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

See all articles