Ways to implement shared memory between PHP processes on a single server

  • 2021-06-29 10:39:30
  • OfStack

To enable the php process to read and write shared memory, developers must first support the IPC function, which is specified at the time of php compilation and installation: --enable-shmop and -- enable-sysvsem options.

IPC (Inter-process communication) is an Unix standard mechanism that provides a way for different processes on the same host to interact with each other.There are three basic IPC processing mechanisms: shared memory, semaphores, and message queues.In this article, we will focus on the use of shared memory and signals.

Using shared memory between different processing processes is a good way to achieve reciprocity between different processes.If you write a piece of information to shared memory in one process, all other processes will see the written data as well.Very convenient.With the help of shared memory in PHP, you can make different processes return different results when running the same PHP script.Or implement real-time queries on the number of simultaneous runs of PHP, and so on.

Shared memory allows two or more processes to share 1 given store.This is the fastest IPC because the data does not need to be replicated between the client and the server.The only trick to using shared memory is the synchronous access of multiple processes to a given store of 1.

How do I create a shared memory segment?The following code can help you build shared memory.

$shm_id = shmop_open($key ,  $mode ,  $perm ,  $size);

Note that each shared memory segment has a unique ID, in PHP, shmop_open returns the ID of the established shared memory segment, where we use $shm_id records it.And $key is an Key value that logically represents a shared memory segment.Different processes can share the same segment by selecting the same Key id.It is customary for us to use a hash value of a string (something like file name 1) as key id. $mode to indicate how shared memory segments are used.Since this is new, the value'c'implies create.If you have already established shared memory, use'a'for access. $The perm parameter defines permissions, in octets. See UNIX File System Help for permission definitions. $size defines the size of shared memory.Although it's a bit like fopen, you shouldn't treat it like file processing 1.You will see this later in the description.

For example:

$shm_id = shmop_open(0xff3 ,  "c" ,  0644 ,  100);

Here we have opened a shared memory segment key in the format 0xff3 rw-r r with a size of 100 bytes.

If you need an existing shared memory segment, you must call shmop_open sets parameters 3 and 4 to 0.

Under Unix, you can use a command line program, ipcs, to query the status of all IPC resources in the system.However, some systems require a superuser to execute.The following figure is the result of an ipcs run.

The system in the figure above shows four shared memory segments, noting that the fourth key value of 0x00000ff3 was created by the PHP program we just ran.Refer to the UNIX user manual for the usage of ipcs.

How do I free shared memory?

The way to free shared memory is to call the PHP directive: shmop_delete ($id)

shmop_delete($id);

$id is when you call shmop_shmop_in openReturn value of op.Another option is to use UNIX's administrative instructions:

ipcrm id, id is the ID you see with ipcs. It is not the same as $id in your program.Be careful, however, that if you delete a shared memory segment directly with ipcrm, it may cause other processes that do not know this to make unpredictable errors (often with poor results) when referencing this shared memory that no longer exists.

How to use (read and write) shared memory?

Write data to shared memory using the functions shown below

int shmop_write (int shmid ,  string data ,  int offset)

Where shmid is using shmop_Handle returned by open. $The Data variable holds the data to be stored. $offset describes where to write the first byte (starting with 0) from the start of shared memory.

The read operation is:

string shmop_read (int shmid ,  int start ,  int count)

Similarly, specify $shmid, start offset (starting with 0), total number of reads.Returns a result string.This way, you can think of a shared memory segment as an array of bytes.Read a few and write a few more, just do what you want, 10 points is convenient.

Now you should have no problem reading, writing, creating or deleting shared memory in a separate PHP process.However, it is clear that it is not possible to have just one PHP process running.If you still follow the single-process approach with multiple processes, you will encounter problems -- well-known parallel and mutually exclusive problems.For example, two processes need to read and write to the same segment of memory at the same time.When two processes write at the same time, you will get an incorrect piece of data, because this memory segment may be the contents of the last process executed, or even a mixed 4 that occurs randomly in turn as data written by two processes.This is obviously unacceptable.To solve this problem, we must introduce a mutually exclusive mechanism.Mutual exclusion is described in many operating system textbooks and is not repeated here.The easiest way to achieve mutual exclusion is to use a semaphore.Semaphores are another way of interprocessing (IPC), which is different from other IPC institutions (pipelines, FIFO, message queues).It is a counter that controls the storage of shared data by multiple processes.Similarly, you can use ipcs and ipcrm to query the status of the semaphore usage and delete it.In PHP, you can create a new semaphore using the following functions and return a handle to the semaphore.If the semaphore to which the key points already exists, sem_get returns the handle that operates the semaphore directly.

int sem_get(int key [ ,  int max_acquire [ ,  int perm]])

$max_acquire indicates that a maximum of several processes can enter the signal at the same time without waiting for it to be released (that is, the maximum number of processes that simultaneously process a resource, 1 generally has a value of 1). $perm specifies permissions.

Once you successfully have a semaphore, there are only two things you can do with it: request, release.When you perform a release operation, the system will reduce the signal value by one.If it is less than 0, it is set to 0.When you perform the requested operation, the system will add 1 to the signal value, and if the value is greater than the set maximum, the system will suspend your processing until other processes are released below the maximum value.1 Normally, the maximum value is set to 1, so that when a process gets a request, other subsequent processes can only wait for it to exit the mutex before releasing the semaphore into it and make it exclusive at the same time.Such a semaphore is often called a two-state semaphore.Of course, if the initial value is any positive number, it indicates how many shared resource units are available for shared applications.


Related articles: