PHP shared memory usage and signal control usage explained

Source: Internet
Author: User
Tags semaphore
This time for everyone to bring the use of PHP shared memory and signal control using a detailed, PHP shared memory usage and signal control of the use of considerations, the following is the actual case, to look at.

Shared memory

Shared memory is used primarily to be able to share some data in different processes of the same machine, such as sharing the usage of the current process in multiple PHP-FPM processes. This communication is also known as interprocess communication (inter-process communication), referred to as IPC.

PHP's built-in SHMOP extension (Shared memory Operations) provides a series of functions that share the function of the RAM (probably not many people, this document does not have a Chinese translation). On Linux, these functions are implemented directly by invoking the functions of the shm* series, while the Winodows also implements the same invocation by encapsulating the system functions.

Main functions:

shmop_close -closing shared memory blocks

Shmop_delete -Deleting shared memory blocks

Shmop_open -Create or open a shared memory block

Shmop_read -reading data from a shared memory block

shmop_size -Gets the size of the shared memory block

Shmop_write -writing data to a block of shared memory

There is also a very important function associated with this: Ftok, the unique key to the IPC (the inode of the file/folder is unique), through the inode information of the file (viewed through the stat or ls-i command on *nix). This function is also directly called the system function implementation of the same name on Linux, and some encapsulation is used on Windows.

A simple example of a count:

<?php# Create a piece of shared memory $shm_key = Ftok (FILE, ' t '); $shm _id = Shmop_open ($shm _key, ' C ', 0644, 8); # Read and write Data $count = (int) shmop_re AD ($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); # off Closed memory block, and does not delete the shared memory, just clear PHP resources shmop_close ($shm _id);

The above code does not perform a count plus 1, and the data is shared between different processes. This means that the data will not be reset unless the memory is manually deleted.

One point that needs a little attention: the second parameter of Shmop_open is a flag, similar to the second parameter of fopen, which has several previous values:

"A" read-only access;

"C" If the memory fragment does not exist, it is created, if present, can read and write;

"W" Read and write;

"N" creates a new memory fragment, and if the same key already exists, it creates a failure, which is used to safely use shared memory considerations.

In addition, because the shared memory fragment used is fixed-length, the length of the data is calculated when it is stored and read, otherwise it may fail to write or read null values.

Signal control

Now that you have used shared memory to store data, you need to consider whether there are multiple processes that write data to shared memory at the same time, and if you need to avoid conflicts. If so, it is necessary to introduce a semaphore to control it.

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

Before talking about Semaphore control, let's say another interesting thing: look at official documents you'll find that there are also functions that share memory operations (shm_*), because this is actually the same category (or from the same author) three extensions, and one is sysvmsg (queue message). The implementation of the function is slightly different, but the actual thing is basically the same. What is the difference between this and the shmop extension above? Shmop source under the README file has a simple description:

PHP already had a shared memory extension (SYSVSHM) written by Christian Cartus <cartus@atrior.de>, unfortunately th Is extension were designed with PHP @ Mind and offers high level features which be extremely bothersome for basic SH M we had in mind.

Simply put: The SYSVSHM extension provides a method that does not store the user's data intact, but instead uses the PHP variable serialization function to serialize the parameters before storing them. This leads to data stored through these methods that cannot be shared with non-PHP processes. However, this can also store a richer PHP data type, Shmop_write can only write strings in the extension above. So why doesn't sysvshm also support Windows? Because it does not introduce a header file that encapsulates the tsrm_win32.h of the shm* series function.

Example after the introduction of signal control:

<?php$id_key = Ftok (FILE, ' t '); $sem _id = Sem_get ($id _key); # Request Signal Control if (Sem_acquire ($sem _id)) {  $shm _id = shmop_ Open ($id _key, ' C ', 0644, 8);  # Read and write data  $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);  # Close memory block  shmop_close ($shm _id);  # Release Signal  sem_release ($sem _id);}

But it is actually very difficult for the local to emulate the write conflict (taking into account the computer's execution speed). In a local test, when you use the For loop operation, if you close the resource without using Shmop_close, you will get an error warning that the shared memory cannot be opened. This should be caused by the fact that the shared memory was not released during the last operation.

Believe that you have read the case of this article you have mastered the method, more exciting please pay attention to the PHP Chinese network other related articles!

Recommended reading:

JS validation input retains specified decimals

Implementation code for puppeteer simulation login Crawl page

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.