Communication between processes in PHP
PHP has several functions that allow different PHP processes communicate with each other but most of the programmers not known their use. In this article we discuss how to use shared memory and traffic lights from PHP and a new world of possibilities becomes a 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 traffic lights from PHP, to do this it is necessary to have php compiled with the following options:
The two extensions that we will use are the extension of the shared memory and the extension of traffic lights, we aver that we do with each of them then..
What is IPC?
IPC (Inter-process communication) is a standard mechanism in Unix for communicating processes. Processes that reside, it is the explanation, all in the same machine. Basically IPC defines 3 different ways of communication: message queues, shared memory segments and traffic lights. In this article we will discuss the use of traffic lights, and shared memory.
Using shared memory from PHP
The use of shared memory segments is a very good way of communicating processes between each other. Basically it defines a segment of memory that can be shared by multiple processes, if a process writes to the segment other processes cannot see these data. In PHP we can control with this technique the number of running processes, or to put data into the memorua avoiding that other processes have to generate the same information that was already generated by other processes. In applications of cache criticize the use of memory as a means of temporary storage is widely more efficient than the use of a file on the file-system
For create a shared memory segment using:
$shm_id = shmop_open($key, $mode, $perm, $size);
- $key is the number that idnetifica the shared memory segment, all processes that want to access this same segment, they should know this number as key to access the segment.
- $mode is the mode of creation, “c” is used to create a segment, while “a” is used to access a segment already created
- $perm sets the permissions of the segment according to the mechanism of Unix permissions
- $size defines the size of the segment
The function returns an id that debermos use to read/write to the segment. It is not similar to a file handle because the functions that are used for manipulating shared memory are other.
$shm_id = shmop_open(0xff3, "c", 0644, 100);
In the example we create a shared memory segment of 100 bytes.
If you want to access a segment already existing use shmop_open setting as 0 the third and fourth parameters of the function. If you want to create or access a segment, as applicable, we proceed as if estuviesemos creandolo, if the segment exists already, it simply gets you to the same.
That we did?
There are some Unix utilities that can be called from the command line to monitor and control resources of IPC, for example we can use “ipcs” to check that resources are created and some parameters of the same.
------ Shared Memory Segments --------key shmid owner perms bytes nattch status0x00280267 0 root 644 1048576 30x00000000 1 nobody 600 46084 10 dest0x00000000 2 nobody 600 46084 8 dest0x00000ff3 131 nobody 644 100 0 ------ Semaphore Arrays --------key semid owner perms nsems status0x00280269 0 root 666 14 ------ Message Queues --------key msqid owner perms used-bytes messages
As we can see we have 4 shared memory segments created, the last (key 0x0000ff3) is the one that we create in our example. We can see the properties of the segments, such as creator, permissions, size, and number of process that are using the segment at the output of the command ipcs.
Returning to the PHP
If we want to delete a shared memory segment we can use the following instruction from PHP:
The $id that we use is the one that devolvio a timely manner the function shmop_open to create or access the segment.
If we want to remove the segment from the command line we can use the command ‘ipcrm shmid’ for example ‘ipcrm 131’ (in our example) would remove the segment that we created from PHP.
ATTENTION: If you use ipcrm as “root” we have to be very careful reviewing that segment that's the segment that we are going to clear is really that you want to delete. If we eliminate other segments belonging to other processes, we can produce unexpected results and that, in general, are pretty bad...
Reading and writing data
To write data in a shared memory segment using:
int shmop_write (int shmid, string data, int offset)
shmid is the id that devolvio shmop_open, data are data (who else?) and the offset is the offset within the segment (0 to start writing at the beginning of the same)
To read we use:
string shmop_read (int shmid, int start, int count)
We indicate the position (offset) from which we want to read (0 for the beginning of the segment) and the number of bytes to read. Using shared memory the data does not have any meaning beyond being strips of bytes, in most implementations it is good to build classes to handle shared memory encargandose provide interfaces for guaradar strings, objects, or other types of data. The class then takes care of manipulating the lengths and to abstract the concept of strip bytes in memory to the programmer.
We will not have any 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 is to use the segment, we are about to enter the fascinating world of the concurrency of processes and the problem of the exclusion of each other.
Suppose that we have two processes reading and writing data in a same shared memory segment. If two processes try to write data in the same position of the memory at the same time we can get incosistencias, this is known as the “problem of exclusion mutual” there is a lot of information and literature on the subject mostly in books on operating systems. A simple way to achieve exclusion mutual is using traffic lights, and is the mechanism that we will study below.
Traffic lights are another of the resources of CPI, we can see information about the traffic lights system with ipcs, and remove them with ipcrm. To create a semaphore since Php is used:
int sem_get (int key [, int max_acquire [, int perm]])
The semaphore is created if it is needed 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 setean as usual.
Once we have the traffic light, you can do two things: buy it and release it. When we acquire a semaphore, can we think that what we are increasing and if we try to increase it beyond the amount set with max_acquire the process is blocked until another process releases the semaphore and enable the blocked process to acquire it. At traffic lights binary, the most common at the 90\% of the cases, only one process can acquire the semaphore; other processes that want to acquire the semaphore will be blocked until the process that holds the semaphore releases it.
To acquire and release traffic lights we use:
int sem_acquire (int sem_identifier)
int sem_release (int sem_identifier)
Where the id is the one that we obtained with sem_get
A simple protocol exclusion mutual.
We can use the following protocol to achieve exclusion mutual 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! */WRITE TO THE SHARED SEGMENT HEREsem_release($semid);
As we can see the mechanism is simple, it acquires the semaphore, do something, and then frees the semaphore. We can be sure that it is impossible that two processes writing at the same time because once one acquires a semaphore, the others are blocked until the semaphore is released. The lines between sem_acquire and sem_release known as a section “criticism” that is to say the areas where we do not want two processes coincide at the same time.
An interesting comment is that in the implementation of traffic lights PHP the process that releases the semaphore must be the same as what you acquired. A process cannot release a semaphore acquired by another process. This is not common in other languages that use the CPI so that we have to take it into account.
It is important to note that we only have to worry about the exclusion of mutual between writers, that more than one process to read the same shared memory segment at the same time there is no problem but if there is a process “writer,” then the process should be the only one that accesses the memory. This is a known problem of concurrency known as the problem of readers-writers.
There are several applications to IPC, since some resources simple optimization as save in shared memory configuration files parseados to advanced search engines, systems of authentication or caching.