Communication between processes in PHP
PHP has several features that allow different processes communicate with each PHP but most programmers use unknown. The paper explores how to use shared memory and semaphores from PHP and a new world of possibilities becomes reality.
Communication between processes in PHP
"Preoptimization is the root of all evil (Donald Knuth)"
In this article we will explain how to manage shared memory and semaphores from PHP, it is necessary to have the php compiled with the following options:
The two extensions are going to use the extension of shared memory and semaphore extension, we aver we do with each course ..
What is IPC?
IPC (Inter-process communication) mechanism is a standard Unix processes to communicate with each other. Living processes, it is clarification, all on the same machine. IPC basically defines three different forms of communication: message queues, shared memory segments and semaphores. In this article we will discuss the use of semaphores and shared memory.
Using shared memory from PHP
Using shared memory segments is a very good way of communicating processes together. Basically a segment of memory that can be shared by multiple processes, if a process writes in the segment other processes can see this data is defined. In PHP we can control with this technique the number of running, or put data in memorua preventing other processes have to generate the same information that was already generated by other processes processes. In critical applications using cache memory as a temporary storage device it is vastly more efficient than using a file-system file
To create a shared memory segment used:
$ Shm_id = shmop_open ($ key, $ mode, $ perm, $ size);
- $ Key is the number that idnetifica the shared memory, all processes who want to access this same segment should know this number as key to access the segment.
- $ Mode is the mode of creation, "c" is used to create a segment while "to" is used to access a segment already created
- $ Perm segment defines the permissions according to the mechanism of Unix permissions
- $ Size The size of the segment defined
The function returns an id that debermos used to read / write to the segment. It is similar to a file handle as the functions used to manipulate shared memory are different.
$ Shm_id = shmop_open (0xff3, "c", 0644, 100);
In the example we create a shared memory segment of 100 bytes.
If we want to access an existing segment shmop_open use by setting to 0 the third and fourth parameters of the function. If we want to create or access a segment according to appropriate we proceed as if we were creating it, if the segment already exists simply accede to it.
There are some Unix utilities that can be called from the command line to monitor and control IPC resources, for example we use "ipcs" to check what resources are created and some parameters of the same.
-------- ------ Shared Memory Segments shmid owner perms key bytes nattch status0x00280267 0 root 644 1 1048576 30x00000000 nobody dest0x00000000 2 10 600 46084 600 46084 8 dest0x00000ff3 nobody nobody 131 644 100 0 ------ -------- Key semid Semaphore Arrays owner root perms nsems status0x00280269 0 666 14 -------- ------ Message Queues key msqid owner perms used-bytes messages
As we can see we have 4 shared memory segments created, the last with (key 0x0000ff3) is to believe in our example. We can see the properties of the segments, as the creator, permissions, size and number of processes that are using the segment in the output of the ipcs command.
Returning to PHP
If we want to delete a shared memory segment can use the following instruction from PHP:
shmop_delete ($ id);
We use the $ id is the function returned timely shmop_open to create or access the segment.
If we remove the segment from the command line you can use the 'ipcrm shmid' for example '131 ipcrm "command (in our example) would eliminate the segment we created from PHP.
WARNING: If you use ipcrm as "root" must be very careful checking that segment as the segment that will be really clear that we want to eliminate. If we eliminate other segments belonging to other processes we can produce unexpected results and generally are pretty bad ...
Reading and writing data
To write data to a memory segment shared use:
shmop_write int (int shmid, string data, int offset)
shmid is the id that returned shmop_open, data is the data (what else?) and the offset is the offset within the segment (0 to start writing at the beginning of it)
To read use:
shmop_read string (int shmid, int start, int count)
Indicate the position (offset) from which we read (0 for the start of the segment) and the number of bytes to read. Using shared memory data have no meaning beyond being strips of bytes, in most implementations is good to build classes that shared memory handling charge of providing interfaces for guaradar strings, objects or other types of data. The class is then responsible for handling lengths and abstract the concept of strip bytes in memory of the programmer.
We will have no problem creating, accessing, reading and writing a shared memory segment from a single isolated process. But we can find some problems when more than one process at the same time try to use the segment, we are about to enter the fascinating world of process concurrency and mutual exclusion problem.
Suppose we have two processes reading and writing data from the same shared memory segment. If two processes try to write data to the same memory location at the same time we get inconsistencies, this is known as the "problem of mutual exclusion" There is a lot of information and literature on the subject mainly books on operating systems. A simple way to achieve mutual exclusion using semaphores, and is the mechanism that will study below.
Traffic lights are another IPC resources, we can see information about the system semaphores ipcs and ipcrm to remove. To create a semaphore PHP is used:
sem_get int (int key [, int max_acquire [, int perm]])
The semaphore is created if necessary or accessed if already existed. The function returns a handle that is used to manipulate the semaphore. max_acquiere indicates the maximum number of processes that can acquire the semaphore without releasing it, the default is 1. The permissions are set such as constumbre.
Once you have the traffic light can do two things with: purchase and release. When we acquire a semaphore may think that we are increasing and if we try to increase it beyond the amount set will with max_acquire the process will block until another process releases the semaphore and enable the process to acquire blocked. In binary semaphore, the most common in the 90 \% of cases only one process can acquire the semaphore, other processes that want to acquire the semaphore will remain blocked until the process that has the semaphore releases it.
To acquire and use semaphores release:
sem_acquire int (int sem_identifier)
sem_release int (int sem_identifier)
Where the identifier is what we got with sem_get
A simple protocol for mutual exclusion.
We can use the following procedure to achieve mutual exclusion from PHP:
$ Semid = sem_get (0xee3,1,0666) $ shm_id = shmop_open (0xff3, "c", 0644, 100); sem_acquire ($ semid); / * If we are here we are alone! * / SHARED WRITE TO THE SEGMENT HEREsem_release ($ semid);
As we can see the mechanism is simple, the semaphore is acquired, does something and then the semaphore is released. We can be sure that no two processes simultaneously writing this because once you acquire the semaphore others are blocked until the semaphore is released. The lines between sem_release sem_acquire and are known as "critical section", ie areas where we do not want two processes simultaneously attend.
An interesting comment is that in PHP semaphore implementation process that releases the semaphore must be the same as it was purchased. A process may not release a semaphore acquired by another process. This is not common in other languages that use IPC so we must take it into account.
Importantly, we only have to worry about the mutual exclusion between writers, that more than one process read the same shared memory segment at a time no problem but if there is any process "writer" then the process must be the only one access memory. This is a known problem of concurrency known as the problem of readers-writers.
There are several applications for IPC, for some simple optimization recursoso stored in shared memory as configuration files parsed to advanced search engines, authentication systems or cache.