This article will discuss how to apply the Inter-Process Communication mechanism-IPC (Inter-Process-Communication) in the PHP4 environment ). The software environment discussed in this article is linux + php4.0.4 or later. First, assume that you have installed PHP4 and UNIX.
This article will discuss how to apply the Inter-Process Communication mechanism-IPC (Inter-Process-Communication) in the PHP4 environment ). The software environment discussed in this article is linux php4.0.4 or later. First, assume that you have installed PHP4 and UNIX. to enable php4 to use shared memory and semaphores, you must activate the shmop and sysvsem extended modules when compiling the php4 program.
Implementation Method: Add the following options when configuring configure in PHP.
-- Enable-shmop -- enable-sysvsem
This allows your PHP system to process related IPC functions.
What is IPC?
IPC (Inter-process communication) is a Unix-scale communication mechanism that provides methods for communication between different processes on the same host. There are three basic IPC processing mechanisms: Shared Memory, Semaphore, and message queue. In this article, we will discuss the application of shared memory and semaphores. For message queues, I will introduce them in the near future.
Application of shared memory segments in PHP
Application shared memory between different processing processes is a good way to achieve communication between different processes. If you write a piece of information into the shared memory in one process, you can also see the written data in all other processes. Very convenient. With the help of shared memory in PHP, you can return different results when running the same PHP script in different processes. Or real-time query of PHP running data targets at the same time.
The shared memory allows two or more processes to share a given storage zone. Since data does not need to be copied between the client and the server, this is the fastest IPC. The only trick for applications to share memory is the synchronous access of multiple processes to a given storage zone.
How to create a shared memory segment? The following code helps you create 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 created shared memory segment. here we use $ shm_id to record it. $ Key is a Key value that logically represents the shared memory segment. You can share the same bucket with the same Key id in different processes. Traditionally, we use the hash value of a string (something similar to a file name) as the key id. $ mode to specify the application method of the shared memory segment. Here, because it is a new one, the value is 'c'-the meaning of "create. If you are visiting a shared memory that has been created, use 'a', -- the meaning of access. The $ perm parameter defines the access permission in octal mode. for the permission definition, see the UNIX file system help. $ Size defines the size of the shared memory. Although it is a bit like fopen (file processing), you should not treat it like file processing. You will see a bit later.
For example:
$ Shm_id = shmop_open (0xff3, 'C', 0644,100 );
Here we open a shared memory segment key value 0xff3-rw-r-pattern with a size of 100 bytes.
If you need to visit an existing shared memory segment, you must set the 3rd and 4 parameters to 0 in the call to shmop_open.