http://www.php.net/manual/en/intro.shmop.php
Shmop is an easy to use set of functions that allows PHP to read, write, create and delete Unix shared memory segments.
I don't understand, what exactly is the purpose of this extension? What is it used for?
Shared memory allows multiple processes to access the same data in memory. You can use it to share data among running PHP scripts.
$shm = shmop_open(0xF00, "c", 0644, 4);
$count = unpack('L', shmop_read($shm, 0, 4));
$count = reset($count);
var_dump($count);
echo "count: ", $count++, "<br/>\n";
shmop_write($shm, pack('L', $count), 0);
When the computer restarts, anything in shared memory is lost.
Different processes can access the same shared memory at the same time, which can lead to race conditions. In the example above, if two processes read the shared memory before either writes back to it, the count will be 1 less than it should be. Race conditions can be prevented by using a mutex, but that's outside the scope of this Q&A.
Shared memory is used for one type of inter-process communication, namely data passing. Some others available in PHP (depending on the platform and PHP build) are:
posix_kill
to send a signal, pcntl_signal
to set up a signal handler), a limited type of message passing. Signals aren't particularly useful in scripted pages, as each script should run for a very short time.posix_mkfifo
is used to create named pipes (aka FIFOs), and the standard file functions are used to read and write data. Unnamed (aka anonymous) pipes can be created between parent and child processes using popen
or proc_open
. Note unnamed pipes cannot be created between arbitrary processes. Note that pipes on some systems are unidirectional: a pipe handle can be used either to read or write, but not both.shm_attach
). Many other extensions for various messaging protocols are also available, including SAM, STOMP and AMQP. See "Other Services" in the PHP manual for, well, others.While sockets (and anything based on them, such as stream wrappers) and pipes can be used to pass data between processes, their abilities with more than two processes are limited. Sockets can only connect two processes; to handle more than two, multiple sockets need to be opened (which is where a client-server architecture usually comes into it). With pipes, only one process can read given data; once it has, that data won't be available to other readers, though they can read other data (which will then become unavailable to all but the reader). An arbitrary number of processes can open the same shared memory region.