Realize inter-process communication in PHP
Author: Qiu Wenyu Joined: 2004-02-22 Views: 168
This article will discuss how to use inter-process communication mechanisms in the PHP4 environment - IPC (Inter-Process-Communication). The software environment discussed in this article is Linux php4.0.4 or higher. First, we have assumed that you have installed PHP4 and UNIX, in order to make PHP4 can use shared memory and semaphore, you must activate SHMOP and SYSVSEM when compiling PHMOP4 programs.
Implementation: Add the following options when PHP settings (Configure).
--enable-shmop --enable-sysvsem
This allows your PHP system to handle related IPC functions.
What is IPC?
IPC (Inter-Process Communication) is a UNIX standard communication mechanism that provides a method that can communicate with each other between different processes of the same host. There are three basic IPC processing mechanisms: they are shared memory, semaphore, and message queues, respectively. In this article we mainly discuss the use of shared memory and semaphore. Regarding the message queue, the author will introduce it in the near future.
Use shared memory segments in PHP
Use shared memory between different processing processes is a good way to communicate between different processes. If you write a message to the shared memory in a process, then all other processes can also see the data written. Very convenient. With the help of shared memory in PHP, you can implement different processes to return different results when running the same PHP script. Or realize real-time queries of the number of PHPs and more.
Shared memory allows two or more processes to share a given storage area. This is the fastest IPC because data does not need to be copied between clients and servers. The only trick using shared memory is to access multiple processes to a given memory.
How to build a shared memory section? 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, here we use $ SHM_ID to record it. $ Key is a key value we logically represents the shared memory segment. Different processes can share the same paragraph store as long as the same KEY ID is selected. It is accustomed to use a string (similar to the same file name) a hash value as a Key ID. $ Mode indicates how the shared memory segment is used. Here is new, the value is 'c' - takes CREATE. If you are accessing the shared memory that has been established, use 'a', and take Access's meaning. The $ perm parameter defines the permissions of access, 8. Please see the UNIX file system for permission definition. $ size defines the size of shared memory. Despite a bit like FOPEN (file processing) you don't want to be the same as file processing. The following description will see a little.
E.g:
$ SHM_ID = Shmop_Open (0xFF3, "C", 0644, 100);
Here we open a shared memory segment key value 0xFF3 -RW-R-R-R-format, size is 100 bytes.
If you need to access existing shared memory segments, you must set up the 3rd, 4 parameters of 0 in call Shmop_Open.
IPC work status query
Under UNIX, you can use a command line program IPCS query system all IPC resource status. However, some system requirements require superuser to perform. The following figure is a result of a section of IPCs.
In the above picture, the system shows four shared memory segments, pay attention to the fourth key value is 0x00000ff3 is created by the PHP program we just run. For IPCS, please refer to UNIX User Manual.
How to release shared memory?
The way to release shared memory is to call PHP instructions: shmop_delete ($ ID)
SHMOP_DELETE ($ ID);
$ ID is the return value of the SHMOP_OP stored in Shmop_Open. There is also a way to use UNIX management instructions: IPCRM ID, ID is the id you see with IPCs. The $ ID in your program is different. But be careful, if you use IPCRM directly to delete the shared memory segment, it is possible to cause other processes that don't know this situation in the reference that there is no predictable error (often the result is not bad).
How to use (read and write) sharing memory?
Written data to shared memory using the functions shown below
Int Shmop_write (int Shmid, String Data, int offset)
Where SHMID is the handle returned with Shmop_Open. The $ DATA variable stores the data to be stored. $ OFFSET describes the location of the first byte from the shared memory (starting with 0).
The read operation is:
String Shmop_read (int shmid, int start, int count)
Similarly, specify $ SHMID, start offset (starting with 0), with a total read quantity. Returns the result string. This way, you can use the shared memory segment as an array of bytes. After reading a few, write a few, why do you want to do it, it is very convenient.
Multi-process problem consideration
Now, you should have no problem in reading, creation, and deleting shared memory in a separate PHP process. However, it is obvious that it is not only a PHP process in the actual operation. If you still use a single process in the case of multiple processes, you will be able to encounter problems ---- famous parallel and mutual exclusive problems. For example, there are 2 processes to read and write the same memory. When the two processes are executed simultaneously, you will get an error data, because the segment memory may be the content of the final execution, or even a mixture of data from 2 processes. The quad is not like. This is obviously unacceptable. In order to solve this problem, we must introduce a mutex mechanism. Mutual exclusion mechanisms have specially described on many operating systems, and there are not many repetitions. The simplest way to achieve mutex is to use the signal light. The amount of signal is another process communication (IPC), which is different from other IPC agencies (pipes, FIFO, message queues). It is a curler for controlling the storage of multiple processes to shared data. The same is that you can use IPCS and IPCRM to implement queries for signal light use and delete it. In PHP, you can create a new amount of semaphore with the following function and return the handle of this signal. If the amount of signal points to the key already exists, the SEM_GET directly returns the handle of the amount of operation.
INT SEM_GET (int key [, int max_acquire [, int perm]])
$ MAX_ACQUIRE indicates that up to several processes can be used to enter the signal without waiting for the signal to be released (that is, the maximum number of processes of a certain resource can be handled, which is generally one). $ PERM indicates access to access.
Once you have a semaphore, you can do only 2 kinds of: requests, release. When you perform a release operation, the system will reduce the signal value. If less than 0 is set to 0. And when you perform the request operation, the system will add this signal value. If the value is greater than the maximum value set, the system will hang your processing process until the other process is released to less than the maximum. Under normal circumstances, the maximum value is set to 1, so that when a process gets the request, other rear processes can only wait for it to exit the mutually exclusive area to release the semaphore to enter the mutual exclusion area and set to exclusive way. Such semaphores are often referred to as dual state signals. Of course, if the initial value is any positive number, it indicates how many shared resource units are available for sharing applications.
The PHP format of the release operation is as follows:
INT SEM_ACQUIER (int SEM_IDENTIFIER) Releases INT SEM_RELEASE (int SEM_IDENTIFIER)
Where SEM_IDENTIFIER is a return value (handle) that calls the SEM_GET.
An example of a simple mutual exclusion agreement
Below is a very simple mutual exclusive operation procedure. $ SEMID = SEM_GET (0xee3, 1,0666); $ shm_id = shmop_open (0xFF3, "C", 0644, 100); SEM_ACQUIRE ($ SEMID); // Application / * Enter the critical section * / Here, the shared memory is handled SEM_RELEASE ($ SEMID);
As you can see, the mutual exclusion is simple: apply to enter the critical area, operate the critical area resource (such as modifying shared memory) withdrawing the critical area and releases the signal. This ensures that it is impossible to operate the same segment shared memory in the same time slice. Because the semaphore mechanism guarantees that a time piece can only be entered by one process, other processes must wait for the process of current processing to be completed.
The critical area generally refers to a code segment that does not allow simultaneous multiple processes concurrently processed.
It should be noted that the amount of seminars it occupied by the same process must be released in the PHP. Allows the process to release other processes in the general system. Be careful with the allocation of the design resource in writing, avoiding the deadlock of A, etc. A, B, B, and the like.
Transportation
The use of IPC is very widespread. For example, a complex configuration file is saved between different processes, or the specific setup user, etc. to avoid repeated processing. I have also used shared memory technology to put a large number of PHP scripts must be referenced in shared memory, and thus significantly improve the speed of the web service, eliminating some bottlenecks. There is also a chat room, multiple broadcasting, etc. The power of IPC depends on your imagination. If this article has a little inspiring, I am very honored. I am willing to discuss this kind of computer technology. Email: Qwyaxm@163.net