Home Backend Development PHP Tutorial Detailed explanation of PHP shared memory usage and signal control usage

Detailed explanation of PHP shared memory usage and signal control usage

May 11, 2018 pm 03:45 PM
php Signal control

This time I will bring you a detailed explanation of the use of PHP shared memory and signal control. What are the precautions for the use of PHP shared memory and signal control? The following is a practical case, let's take a look.

Shared memory

The use of shared memory is mainly to share some data in different processes on the same machine, such as in multiple The usage of the current process is shared among the php-fpm process. This kind of communication is also called Inter-Process Communication, or IPC for short.

PHP’s built-in shmop extension (Shared Memory Operations) provides a series of functions for shared memory operations (maybe because not many people use it, this document has not been translated into Chinese yet). On Linux, these functions are directly implemented by calling the shm* series of functions, while on Winodows, the same calls are also implemented by encapsulating system functions.

Main functions:

shmop_close — Close the shared memory block

shmop_delete Delete shared memory block

shmop_open — Create or open a shared memory block

shmop_read — Read data from a shared memory block

shmop_size — Get the size of the shared memory block

shmop_write — Write data to the shared memory block

Related to this There is also a very important function: ftok, which creates a unique key for IPC through the inode information of the file (viewed through the stat or ls -i command on *nix) (the inode of the file/folder is unique). This function is also implemented by directly calling the system function of the same name on Linux, and some encapsulation is still used on Windows.

A simple counting example:

<?php
# 创建一块共享内存
$shm_key = ftok(FILE, &#39;t&#39;);
$shm_id = shmop_open($shm_key, &#39;c&#39;, 0644, 8);
# 读取并写入数据
$count = (int) shmop_read($shm_id, 0, 8) + 1;
shmop_write($shm_id, str_pad($count, 8, &#39;0&#39;, STR_PAD_LEFT), 0);
// echo shmop_read($shm_id, 0, 8);
# 关闭内存块,并不会删除共享内存,只是清除 PHP 的资源
shmop_close($shm_id);
Copy after login

The above code does not execute a count plus 1, and the data is shared between different processes. In other words, unless this memory is manually deleted, this data will not be reset.

There is a point that needs a little attention: the second parameter of shmop_open is a flag, similar to the second parameter of fopen, and its values ​​​​are as follows:

"a" read-only Access;

"c" If the memory segment does not exist, create it, if it exists, it can be read and written;

"w" Read and write;

"n" Create If a new memory segment with the same key already exists, the creation will fail. This is for the sake of safe use of shared memory.

In addition, since the shared memory segment used is of fixed length, the length of the data must be calculated when storing and reading, otherwise the writing may fail or a null value may be read.

Signal Control

Since shared memory is used to store data above, you need to consider whether multiple processes are writing data to the shared memory at the same time. circumstances, whether conflicts need to be avoided. If so, you need to introduce a semaphore for control.

PHP also provides a similar built-in extension sysvsem (this extension is not available in the Windows environment. The ftok function is also included in this extension in the document, but in fact ftok is provided in the standard function library, so Also available under Windows).

Before talking about semaphore control, let me talk about another interesting thing: looking at the official documentation, you will find that there are also functions for shared memory operations (shm_*), because this is actually the same category (or Three extensions from the same author), and one is sysvmsg (

queuemessage). The implementation of the functions is slightly different, but what they actually do is basically the same. What is the difference between this and the shmop extension above? The README file under the shmop source code has a simple description:

PHP already had a shared memory extension (sysvshm) written by Christian Cartus , unfortunately this extension was designed with PHP only in mind and offers high level features which are extremely bothersome for basic SHM we had in mind.

简单说来:sysvshm 扩展提供的方法并不是原封不动的存储用户的数据,而是先使用 PHP 的变量序列化函数对参数进行序列化然后再进行存储。这就导致通过这些方法存储的数据无法和非 PHP 进程共享。不过这样也能存储更丰富的 PHP 数据类型,上文的扩展中 shmop_write 只能写入字符串。那么为什么 sysvshm 同样不支持 Windows 呢?因为其并没有引入封装了 shm* 系列函数的tsrm_win32.h 的头文件。

引入信号控制之后的示例:

<?php
$id_key = ftok(FILE, 't');
$sem_id = sem_get($id_key);
# 请求信号控制权
if (sem_acquire($sem_id)) {
  $shm_id = shmop_open($id_key, 'c', 0644, 8);
  # 读取并写入数据
  $count = (int) shmop_read($shm_id, 0, 8) + 1;
  shmop_write($shm_id, str_pad($count, 8, '0', STR_PAD_LEFT), 0);
  // echo shmop_read($shm_id, 0, 8);
  # 关闭内存块
  shmop_close($shm_id);
  # 释放信号
  sem_release($sem_id);
}
Copy after login

但是本地想模拟实现写入冲突实际上是非常难的(考虑到计算机的执行速度)。在本地测试中,使用 for 循环操作时如果不使用shmop_close 关闭资源会出现无法打开共享内存的错误警告。这应该是因为正在共享内存被上一次操作占用中还没有释放导致。

相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!

推荐阅读:

JS验证输入保留指定小数

puppeteer模拟登录抓取页面的实现代码

The above is the detailed content of Detailed explanation of PHP shared memory usage and signal control usage. For more information, please follow other related articles on the PHP Chinese website!

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

Video Face Swap

Video Face Swap

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

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)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1268
29
C# Tutorial
1246
24
Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

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,

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

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 PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

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: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

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 in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

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: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

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

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

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.

Explain the match expression (PHP 8 ) and how it differs from switch. Explain the match expression (PHP 8 ) and how it differs from switch. Apr 06, 2025 am 12:03 AM

In PHP8, match expressions are a new control structure that returns different results based on the value of the expression. 1) It is similar to a switch statement, but returns a value instead of an execution statement block. 2) The match expression is strictly compared (===), which improves security. 3) It avoids possible break omissions in switch statements and enhances the simplicity and readability of the code.

See all articles