Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the shmop PHP extension do?

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?

like image 535
Alex Avatar asked Dec 25 '11 22:12

Alex


2 Answers

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:

  • Signals (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.
  • Sockets for data. Sockets can use a network, or can be local.
  • Pipes for data. 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.
  • Semaphores for synchronization.
  • Message queues for messaging. In PHP, the Semaphore extension offers both message queues and another set of shared memory functions (e.g. 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.
  • Network stream wrappers for data. At a lower level, these are just sockets, though they provide a different interface. They are also for specific application level protocols, whereas sockets are more general.
  • Network protocol extensions, such as cURL, for messaging & data. Like stream wrappers, these are (limitd) sockets in disguise.
  • Web service extensions, such as SOAP and XML-RPC, for remote procedure calls (RPC). Note that while these are socket based, they're for a different type of IPC (RPC rather than data).

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.

like image 179
outis Avatar answered Oct 28 '22 04:10

outis


When a running process requests memory, the system provides a slice of memory which can only be accessed by the allocated process. Sometimes you run multiple threads and want to share data amongst them.

"Sharing data" can be done by:

  • Passing data via sockets/pipes
  • Shared Memory (threads, processes)

Since passing data is not very handy in some cases, one might want to use Shared memory.

The functions in question provide functionality to handle shared memory segments in PHP.

like image 9
markusschmitz Avatar answered Oct 28 '22 06:10

markusschmitz