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

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.
