PHP shared memory usage and signal control

Source: Internet
Author: User

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-to get the size of a shared memory block
    • shmop_write-writing data to a block of shared memory

There is also an important function associated with this: Ftok, which stat ls -i creates the IPC's unique key (the inode of the file/folder is unique) through the Inode information of the file (*nix on or command view). 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_read ( $shm _id08) + 1; Shmop_write ( $SHM _idstr_pad (  $count 8 ' 0 ' str_pad_left0 Echo Shmop_read ($shm _id, 0, 8); # Closes the memory block and does not delete the shared memory, just clears the PHP resource 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: shmop_open The second parameter 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 (which is not in the Windows environment, where functions are also included in the document, ftok but is actually provided in the ftok standard libraries, 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 three extensions of the same category (or from the same author), 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 files under the README simple description:

PHP already had a shared memory extension (SYSVSHM) written by Christian Cartus [email protected], unfortunately This extension is designed with PHP @ Mind and offers-level features which is extremely bothersome for basic SHM 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, which shmop_write can only be written to a string in the extension above. So why doesn't sysvshm also support Windows? Because it does not introduce a header file that encapsulates a shm* series of functions tsrm_win32.h .

Example after the introduction of signal control:

<?php$id _key=Ftok(__file__,' t ');$sem _id=Sem_get($id _key);# Request Signal ControlIf(Sem_acquire($sem _id)){$shm _id=Shmop_open($id _key,C,0644,8);# Read and write data$count=(Int)Shmop_read($shm _id,08) + 1shmop_write ( $SHM _idstr_pad  ( $count 8 0 ' str_pad_left0 //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 for a loop operation, you shmop_close cannot open the shared memory error warning if you do not use the close resource. This should be caused by the fact that the shared memory was not released during the last operation.

PHP shared memory usage and signal control

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.